Skip to content

Feature Request - Add Javascript CefSharp.PostMessage #2775

Closed

Description

@amaitland

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.

Activity

  1. added this to the 75.0.0 milestone on May 13, 2019
  2. self-assigned this
    on May 30, 2019
  3. amaitland commented on Jun 3, 2019

    @amaitland
    MemberAuthor

    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);
    }
  4. amaitland commented on Mar 12, 2020

    @amaitland
    MemberAuthor

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

  5. woth2k3 commented on May 17, 2020

    @woth2k3
  6. amaitland commented on May 17, 2020

    @amaitland
    MemberAuthor

    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.

  7. woth2k3 commented on May 17, 2020

    @woth2k3

    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!

  8. gf21T commented on Mar 9, 2021

    @gf21T
  9. amaitland commented on Mar 9, 2021

    @amaitland
    MemberAuthor

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

  10. BuslikDrev commented on Apr 29, 2023

    @BuslikDrev

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

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions