Another Prog Another Prog - 1 month ago 18
C# Question

With Chromium Embedded is there a way to communicate to the program, from Javascript?

If you have a chromium embedded web browser widget in an application, is there a way to notify the application that something has occurred at any point in time? for example let's say an item is resized or a button is clicked and it is a javascript based page loaded in the chromium embedded widget. How do you notify the C++ or Delphi application (or any application) via code and send a message or callback to it? I am looking for something like a sendMessage feature, to communicate back to the application that something has occurred.

Examples of how to do this can be in C++, .Net, or Delphi, or any language... as I could simply port the code over to my Delphi language.

In other words, how do you communicate from javascript, to the application that has the chromium embedded web browser in it? When the page is done loading, you can communicate through that page load end event that occurs... however javascript code tends to run even after the page is loading, especially if the user uses a mouse to drag an item on the page or click a button.. So how do you communicate back to the C++/Delphi application any time you want, that something has happened?

FYI full access to the html/java code is available, i.e. I can add my own code in the javascript.. any code I want. So if there is something like sendMessageBackToApp then I would like to add it.

If this feature is not available, it certainly would be useful. Communication between the application and the javascipt/html is essential for the application to be truly powerful.

Answer

There are two ways to communicate with native code in CEF.

  1. "Asynchronous JavaScript Bindings" - this approach is described here (there is a chapter with the same name): https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage.md

  2. You can also create JS function objects which are actually call your C++ code in the renderer process. You can read more about it here: https://bitbucket.org/chromiumembedded/cef/wiki/JavaScriptIntegration.md

The second way is more low-level. You'll have to communicate with the browser process using CEF's IPC mechanism.

Comments