Attaching listeners is quite easy (only in older versions of CEF):
procedure MouseDownCallback(const Event: ICefDomEvent);
begin
ShowMessage('Mouse down on '+Event.Target.Name);
end;
procedure AttachMouseDownListenerProc(const Doc: ICefDomDocument);
begin
Doc.Body.AddEventListenerProc('mousedown', True, MouseDownCallback);
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
ChromiumComponent.Browser.MainFrame.VisitDomProc(AttachMouseDownListenerProc);
end;
Regarding the extended functions for getting JavaScript results directly: the trunk doesn't contain them (yet?). Seems to be work in progress.
Edit:
Getting rid of polling via extensions:
It is indeed possible for your JavaScript code to call back into your Delphi code using extensions. Furthermore you can send values from JavaScript to Delphi - this could be used to transfer results without the need to poll.
First in your initialization
section register the extension, which creates a JavaScript object later to be used when calling back:
procedure RegisterExtension;
var
Code:string;
begin
Code :=
'var cef;'+
'if (!cef)'+
' cef = {};'+
'if (!cef.test)'+
' cef.test = {};'+
'(function() {'+
' cef.test.__defineGetter__(''test_param'', function() {'+
' native function GetTestParam();'+
' return GetTestParam();'+
' });'+
' cef.test.__defineSetter__(''test_param'', function(b) {'+
' native function SetTestParam();'+
' if(b) SetTestParam(b);'+
' });'+
' cef.test.test_object = function() {'+
' native function GetTestObject();'+
' return GetTestObject();'+
' };'+
'})();';
CefRegisterExtension('example/v8', Code, TMyHandler.Create as ICefv8Handler);
end;
initialization
RegisterExtension;
TMyHandler
's Execute
will be invoked later. TMyHandler
is defined as
TMyHandler = class(TCefv8HandlerOwn)
protected
function Execute(const name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var exception: ustring): Boolean; override;
end;
The implementation for demonstration purposes is simple for now:
function TMyHandler.Execute(const name: ustring; const obj: ICefv8Value; const arguments: TCefv8ValueArray; var retval: ICefv8Value; var exception: ustring): Boolean;
begin
ShowMessage('Execute!');
end;
Now to test calling into Delphi from JavaScript simply do:
ChromiumComponent.Browser.MainFrame.ExecuteJavaScript('cef.test.test_object().GetMessage();', 'about:blank', 0);
This should display the message box saying "Execute!".
I pulled the demo script from a sample named cefclient which you can find in the \demos\cefclient folder in the component root dir. The extension sample code is a bit hidden and mingled with other demo code. But of special interest for us is the implementation of TExtension.Execute
(the equivalent to my TMyHandler.Execute
). There you can find how to determine which function is being called and how to pass parameters. (Link to the code.)