dcef3
dcef3 copied to clipboard
question: call window.external.MyMethod(); from JS
hi. Can i ask for your help? i need register method MyMethod in any one demo project and call method in JS: window.external.MyMethod();
can you show me how can i do this in this component?
Hi, I have success do call delphi code from js, here is the way:
open the guiclient demo, you will notice the following code at the main.pas
initialization
CefRemoteDebuggingPort := 9000;
CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn.Create;
the point is CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
this line will tell CEF to execute the TCustomRenderProcessHandler.OnWebKitInitialized
function, in this function:
procedure TCustomRenderProcessHandler.OnWebKitInitialized;
begin
{$IFDEF DELPHI14_UP}
TCefRTTIExtension.Register('app', TTestExtension);
{$ENDIF}
end;
will register a js extension, named app, with the implementation by TTestExtension
TTestExtension = class
class function hello: string;
class procedure mouseover(const data: string);
end;
{ TTestExtension }
class procedure TTestExtension.mouseover(const data: string);
var
msg: ICefProcessMessage;
begin
msg := TCefProcessMessageRef.New('mouseover');
msg.ArgumentList.SetString(0, data);
TCefv8ContextRef.Current.Browser.SendProcessMessage(PID_BROWSER, msg);
end;
class function TTestExtension.hello: string;
begin
Result := 'Hello from Delphi';
end;
so, you can all app.hello in the js code which will return 'Hello from Delphi'.
But please notice that, all the code executes in the render process, not in the main process, so, in the implementation code, do not directly call TForm
or the main process's functions, or will led to exception, you need to send message by call SendProcessMessage
of the browser demonstrated by the code above with mouseover
function.
Thanks for all the authors of this project, like @hgourvest , @boomsya who make this great project, it is more stable than CEF4delphi, thank you.