How do I get the result from executing JavaScript in an embedded Chromium control?

3k views Asked by At

How can I execute JavaScript and get its result in DCEF3? I would appreciate if someone could write a simple example of how this can be done.

I can execute JavaScript with the code below, but it doesn't provide the result.

Browser.MainFrame.ExecuteJavaScript('app.doit(''foo'')', '', 0);
2

There are 2 answers

0
delphirules On

Here is my workaroud : use 'console.log()' JS command. The idea is to run the desired JS code and log the result to the console ; after this, you can intercept the console message and get the result.

Example :

var a = 1; 
var b = 2;
var result = a + b;
console.log(result);
return result;

It's not the ideal but worked for me.

0
Ondřej Smola On

From python implementation wiki:

This function executes asynchronously so there is no way to get the returned value. Calling javascript from native code synchronously is not possible in CEF 3. It is also not possible doing it synchronously the other way around ie. js->native.