Call Delphi from Javascript in TChromium Component

7,326 views
Skip to first unread message

Mark Di Val

unread,
Nov 13, 2012, 2:13:13 AM11/13/12
to delphichrom...@googlegroups.com
Hi All,
 
Apologies if this has already be done to death somewhere else - but I can't find a way to do this.
 
I can set events in my javascript (Google Maps API v3 events) which will run a js function.
 
What I would like to be able to do is call a Delphi function or procedure passing a string parameter.
 
TIA
 
Mark

Mark Di Val

unread,
Nov 13, 2012, 6:44:19 AM11/13/12
to delphichrom...@googlegroups.com
BTW I notice that this code never gets called - seems fairly critical!
 
procedure TCustomRenderProcessHandler.OnWebKitInitialized;
begin
{$IFDEF DELPHI14_UP}
  TCefRTTIExtension.Register('app', TTestExtension);
{$ENDIF}
end; 
 
The initialization get called:
 
initialization
 CefRemoteDebuggingPort := 9000;
 CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
 CefBrowserProcessHandler := TCustomBrowserProcessHandler.Create;
end.
 
This is just the guiclient demo I have been playing with.
 
Not sure what I am missing.
 
M

Michael Brooks

unread,
Nov 17, 2012, 4:47:15 PM11/17/12
to delphichrom...@googlegroups.com
Hello Mark:
 
What I would like to be able to do is call a Delphi function or procedure passing a string parameter.
 
 
I don't believe you can call a Delphi method directly from TChromium, instead you'll need to do the following:
  1. Add an OnConsoleMessage event to your TChromium component.
  2. Add a "console.log('bla bla bla')" method to your HTML's javascript where "bla bla bla" is the text you want your OnConsoleMessage event to receive through its "Message" parameter.
It's a good idea to prefix your "bla bla bla" with a string that tells you and your Delphi application what you're intending to do.   For example, "bla bla bla" could be "log_click:http://mysite.com" which your OnConsoleMessage event would parse to know that you want to call your Delphi "LogClick" method with "http://mysite.com" as a parameter value.
 
Hope that helps.
 
Michael
   

Mark Di Val

unread,
Nov 19, 2012, 5:02:35 PM11/19/12
to delphichrom...@googlegroups.com
Hi Michael,
 
This does exactly what I need - thank you.
 
Not having had much to do with javascript or DCEF I had no idea about the console or the .log function. Interestingly, having an OnConsoleMessage handler gave me a warning about including multiple references to Google Maps in my html - so that was also useful.
 
I will also take your lead and have that javascript format the polygon data so that the handler can filter that and give the user a "save polygon" dialog.
 
I had been attempting to get a number of the COM/Extension type callback things to work along the lines of various posts on this subject but had had no luck. One of the lines that I tried used objects that didn't exist in the current version of ceflib.pas (perhaps TCefStyleHandlerOwn ? IIRC).
 
I compared files that Tortoise brought down from the trunk with files that you see when browsing the turnk and I note that they are different. Take ceflib.pas for example: I have 14049 lines whereas the browsed trunk had 11000 odd lines when I browsed on the weekend and as I say there where classes in the shorter (and I assume older version) that where not in my SVN copy. I note today (20/11/2012) that "browsing" the source (http://code.google.com/p/dcef3/source/browse/trunk/src/ceflib.pas) this file now has 14143 lines - i.e. changed since Saturday. Is it still then release 23?
 
I guess that all means that the trunk is subject to change without notice - a work in progress.
 
For the more exotic methods of passing data back to Delphi from javascript it would be nice if there was a working VCL example (demo) that uses the current version of the libraries. Lots of the little snippets that people have posted don't quite close the loop.
 
Anyway, thanks again for your help.
 
Cheers,
 
Mark

Balázs Varga

unread,
Nov 19, 2012, 5:25:32 PM11/19/12
to delphichrom...@googlegroups.com
Hi,

I would be suprised if you would not be able to call a delphi function from javascript with dcef3. In dcef1 it was "simple" with the help of Extensions, see the dcef1 cefclient demo


just navigate to client://test to see how it did work, if I'm remember correctly.

I haven't had time to play with the new dcef3, so don't know whether is it working or not, but I would be surprised if this feature would be missing from dcef3 (or at least I would be very very sad).

Balazs 
Message has been deleted

Rüdiger Hahn

unread,
Nov 20, 2012, 7:47:05 AM11/20/12
to delphichrom...@googlegroups.com
Hi,

I succeeded doing it as you started. Just create a simple class and register it with "TCefRTTIExtension.Register".

I could even use properties with all their benefits like in the following example

...
 
type
  TMyClass = class(TObject)
  private
    FValue: string;
  protected
    procedure SetValue(Value: string);
    function GetValue: string;
  public
    property Value: string read GetValue write SetValue;
  end;
 
implementation
 
procedure TMyClass.SetValue(Value: string)
begin
  if (Value <> FValue) then begin
    // set the Value and do anything you want.
    FValue := Value;
    ShowMessage('JavaScript tells us the following: ' + FValue); 
    FValue := FValue + ' This is a modification from Delphi!';
  end;
end;
 
function TMyclass.GetValue: string;
begin
  Result := 'This is the result from the getter: ' + FValue;
end; 



Register class with something like TCefRTTIExtension.Register('myclass', TMyClass);

That should it be from Delphi side.

In javascript you now just can do things like this:

myclass.Value = 'Hello World!';
alert(myclass.Value);

There should be two Popups: first a message dialog from Delphi with the text "JavaScript tells us the following: Hello World!" The second will be an alert window from JavaScript with the message "This is the result from the getter: Hello World! This is a modification from Delphi".

BTW: I had problems transferring Boolean values from Javascript to Delphi. Nothing happened. Eventually I found out that I had to convert the Boolean values to integers to make them work.

At the end I am very happy with the work of Henri Gourvest.

Ruediger


Henri Gourvest

unread,
Nov 20, 2012, 11:05:42 AM11/20/12
to delphichromiumembedded
BTW: I had problems transferring Boolean values from Javascript to Delphi. Nothing happened. Eventually I found out that I had to convert the Boolean values to integers to make them work.


I just fixed this bug
 
At the end I am very happy with the work of Henri Gourvest.

Thank you :)

--
Henri Gourvest

Rüdiger Hahn

unread,
Nov 20, 2012, 11:08:26 AM11/20/12
to delphichrom...@googlegroups.com
Wow, that was fast.

Thank you so much.

Ruediger

pierre....@libertysurf.fr

unread,
Jan 17, 2013, 2:02:29 PM1/17/13
to delphichrom...@googlegroups.com
I have found my error.
It's OK
I attach my unit and a simple html file
Regards,
Pierre

On Thursday, January 17, 2013 4:09:25 PM UTC+1, pierre....@libertysurf.fr wrote:
Hello,
I have the same problem.
Where call  "TCefRTTIExtension.Register "
I can't execute the Javascript!
Thanks,
Pierre

On Wednesday, December 19, 2012 11:00:01 PM UTC+1, Jeremy Coulter wrote:
Hi. well, I must be missing something as I cant get it to work :-(
I am using Delphi XE2, and I followed what you did below and I got an "Undefined" error when the Javascript is run.

My problem may be easy as where I am calling "TCefRTTIExtension.Register('myclass', TMyClass);". so I add this to the Implementation section? or the formcreate...or somewhere else....just to let you know I have tried both and with no success.
My JS is as follows:
<script language="JavaScript">
myclass.Value = 'Hello World!';
alert(myclass.Value);
</script>
and I am pretty sure thats ok.
and my Delphi is as per the example below.
Can anyone shed some light? I hope I have provided enough info.

Jeremy
Unit5.pas
MonJs.html

Long Mu

unread,
Apr 24, 2013, 5:03:48 PM4/24/13
to delphichrom...@googlegroups.com
How to work in Delphi7 ???

Anthony

unread,
May 19, 2013, 7:12:09 PM5/19/13
to delphichrom...@googlegroups.com, ruedige...@googlemail.com
but any ide why i got AV when i change ShowMessage('JavaScript tells us the following: ' + FValue);
with other command

example::
form1.button1.click;
or
call other function from here?

any thing other showmessage will got AV error

thanks

Moharram Bagheri

unread,
Jul 2, 2015, 1:24:42 AM7/2/15
to delphichrom...@googlegroups.com
Hi

i use Register function of TCefRTTIExtension Class to register an appName, to call my delphi app from JS running in the chromium.

the code is well and i have no problem,

but after calling the delphi function more and more the VM Size of application grows up and the memory consumption increases.

is there a bug with CEF, or i am mistaking in my steps to setup the procedure?

Thanks
Reply all
Reply to author
Forward
0 new messages