4

I'm using DCEF3, revision 24038bd3a600, and I should want to communicate between Javascript code in browser and Delphi code of application. I know how it can be accomplished from Delphi code and Javascript, by using framework's method 'ExecuteJavascript', but what about the reverse (from Javascript to Delphi/application code) ? I haven't found such a situation in demos/examples (GUIclient, specifically...).

henry60
  • 463
  • 12
  • 24
  • 1
    you have to register a V8 extension (JavaScript code that calls native functions); you can see an example in [this answer](http://stackoverflow.com/a/6895111/833188) – Sga Jul 09 '14 at 08:36
  • Ok, I have done so in a older version (DCEF, not DCEF3) but now in DCEF3 it seems not working. Are You sure it's still possible to do so ? – henry60 Jul 09 '14 at 10:24
  • 1
    it seems so, `ceflib.pas` has a reference to `cef_register_extension`. [Here](https://code.google.com/p/dcef3/issues/detail?id=20&q=extension) you can find an example – Sga Jul 09 '14 at 10:47
  • 1
    It's not working, CefRegisterExtension always return false and extension doesn't work in web page. – henry60 Jul 10 '14 at 08:31
  • 1
    can you test `guiclient`? There is a test in it for extensions, see at the end of [this source code](https://code.google.com/p/dcef3/source/browse/demos/guiclient/main.pas) – Sga Jul 10 '14 at 08:37
  • ok, now is working, but only with ShowMessage as command executed in Delphi application. With other commands or functions it doesn't work. Any idea about that ? – henry60 Jul 11 '14 at 09:19
  • sorry, can't test Delphi environment – Sga Jul 11 '14 at 10:11

2 Answers2

3

There is a guiclient demo if official source code to do this. Look at main.pas file.

The code below is a class extension :

class function TTestExtension.hello: string;
begin
  Result := 'Hello from Delphi';
end;

The code below register the extension class :

TCefRTTIExtension.Register('app', TTestExtension);

The code below call your native code from a HTML page :

<script>
alert ( app.hello() );
</script>

The code below call your native code from embedded browser :

crm.Browser.MainFrame.ExecuteJavaScript('alert ( app.hello() );', 'about:blank', 0);
Stéphane B.
  • 3,260
  • 2
  • 30
  • 35
2

A quite easy workaround is to catch the browser's OnJSDialog / OnConsoleMessage event, do an Alert/log in JS when there's something to execute. Tell the delphi part in the alert's message what to do. You need to interpret it as a string (maybe with a scripting library or direct parsing). No direct call of delphi code is possible with it, but I guess it's safer this way anyway.

kgz
  • 527
  • 2
  • 10