Skip to content

Feature Request - Add Javascript CefSharp.PostMessage #2775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
amaitland opened this issue May 13, 2019 · 8 comments · Fixed by #2799
Closed
3 tasks done

Feature Request - Add Javascript CefSharp.PostMessage #2775

amaitland opened this issue May 13, 2019 · 8 comments · Fixed by #2799
Assignees
Milestone

Comments

@amaitland
Copy link
Member

amaitland commented May 13, 2019

There are lots of cases when you don't need to explicitly bind an object, you just wish to pass some message data back to the browser. This will be added to every V8Context, so will be immediately available for javascript execution.

  • Add .Net event
  • Add CefSharp.PostMessage javascript method.
  • Add detailed example to this issue
CefSharp.PostMessage(message);

I'm thinking we won't impose a strict message structure, will use the current conversion from Javascript->C# so whatever data types you can transfer now will be supported.

@amaitland amaitland added this to the 75.0.0 milestone May 13, 2019
@amaitland amaitland self-assigned this May 30, 2019
@amaitland amaitland reopened this Jun 3, 2019
@amaitland
Copy link
Member Author

amaitland commented Jun 3, 2019

Use Cases:

  • For simple cases there's no need to bind an object to get communication up and running
  • An easy way to handle events/button clicks etc

Details:

  • Accepts only a single param, this can be a primitive type like int, string or a complex object
  • IJavascriptCallback is supported (javascript function pointer)
  • You can use dynamic in c# for complex objects or use the built in Model Binder via the JavascriptMessageReceivedEventArgs .ConvertMessageTo<T>() method to convert to a complex object
//In Javascript you call CefSharp.PostMessage or cefSharp.PostMessage
//Either case is supported
//You can post a primitive type like int, string or a complex type like below
CefSharp.PostMessage({ "Type": "Update", Data: { "Property": 123 }, "Callback": responseFunc });
browser.JavascriptMessageReceived += OnBrowserJavascriptMessageReceived;

private void OnBrowserJavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
{
	//Complext objects are initially expresses as IDicionary
	//You can use dynamic to access properties (the IDicionary is an ExpandoObject)
	//dynamic msg = e.Message;
	//Alternatively you can use the built in Model Binder to convert to a custom model
	var msg = e.ConvertMessageTo<PostMessageExample>();
	var callback = (IJavascriptCallback)msg.Callback;
	var type = msg.Type;
	var property = msg.Data.Property;

	//Call a javascript function with your response data
	callback.ExecuteAsync(type);
}

@amaitland
Copy link
Member Author

The How do you handle a Javascript event in C#? of the FAQ has been updated to use PostMessage.

@woth2k3

This comment was marked as off-topic.

@amaitland
Copy link
Member Author

Could you please give me a hint about the reason of this error?

@woth2k3 Please no images of code, see https://github.com/cefsharp/CefSharp/wiki/Questions-&-Support#no-images-of-code

I'd suggest you try casting msg.Callback to IJavascriptCallback before attempting to call ExecuteAsync. If that doesn't work then I'd need to see the PostMessage javascript.

@woth2k3
Copy link

woth2k3 commented May 17, 2020

Sorry for the image. I've edited the message and made it as it's supposed to be.

Worked like a charm with casting. Many thanks!

@gf21T

This comment has been minimized.

@amaitland
Copy link
Member Author

@gf21T You don't appear to be using CefSharp.PostMessage, I'd suggest you ask for help on stackoverflow.

@BuslikDrev
Copy link

Предлагаю добавить ещё в таком стиле написания функцию "CefSharp.PostMessage" => "CefSharp.postMessage"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants