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...).
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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);
回答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.