Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Retrun JS result Tchromium

subin
Posts: 20
Joined: Thu Sep 05, 2024 5:15 am

Retrun JS result Tchromium

Post by subin »

I am calling this
fBrowser.ExecuteJavaScript('document.getElementById("AutoLogoutWarnExternalDiv").innerHTML;',
'about:blank', 0);

in a function. I need to get the result of this and return the result. The fbrowser which is an instance of TChromium, is created run time. It will be
helpful if you provide a solution.

thanks
Subin
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Retrun JS result Tchromium

Post by salvadordf »

There are very similar examples in the DOMVisitor demo
subin
Posts: 20
Joined: Thu Sep 05, 2024 5:15 am

Re: Retrun JS result Tchromium

Post by subin »

hi Thanks for the reply.

Chromium_OnJsdialog or Chromium_ProcessMessageReceived event is used to get the result of JS code.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Retrun JS result Tchromium

Post by salvadordf »

It's TDOMVisitorFrm.Chromium1ProcessMessageReceived

Read this for all the details :
https://stackoverflow.com/questions/64303829/how-to-invoke-the-delphi-function-from-javascript-using-cef4delphi/64304191#64304191
subin
Posts: 20
Joined: Thu Sep 05, 2024 5:15 am

Re: Retrun JS result Tchromium

Post by subin »

Hi,
Thanks for your reply :D .

I am using
fBrowser.ExecuteJavaScript('document.getElementById("AutoLogoutWarnExternalDiv").innerHTML;', 'about:blank', 0);
fBrowser.OnProcessMessageReceived := Chromium_ProcessMessageReceived;
where fbrowser is an instance of tchromium. However I am not getting the JS result in Chromium_ProcessMessageReceived.
The example uses frame.ExecuteJavaScript(TempJSCode, 'about:blank', 0);
Please let me know the difference between them.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Retrun JS result Tchromium

Post by salvadordf »

TChromium.ExecuteJavaScript and frame.ExecuteJavaScript are basically the same.

Use the code in the demos\Delphi_VCL\JavaScript\JSExtension demo.

It uses a "JavaScript extension" to send information from JavaScript to Delphi.
Read the code comments in that demo.

Sorry for the confusion about the DOMVisitor demo.
subin
Posts: 20
Joined: Thu Sep 05, 2024 5:15 am

Re: Retrun JS result Tchromium

Post by subin »

Thank you

If i pass a parameter document.getElementById... in ExecuteJavascript, can't I access message.ArgumentList.GetString(0) in ProcessMessageReceived event? IF i pass alert("Hi") in ExecuteJavascript, I can get the popup message.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Retrun JS result Tchromium

Post by salvadordf »

Read the code comments in the JSExtension demo and also the documentation about about Chromium's architecture :
http://www.chromium.org/developers/design-documents/multi-process-architecture

All Chromium based browsers use several processes to show web pages and it's not possible to access information between processes.

CEF can send process messages, register JavaScript extensions, etc.

All those tools allows us to send information between JavaScript in the render process and Delphi in the main application process.

See how the JSExtension declares two custom functions here :
https://github.com/salvadordf/CEF4Delphi/blob/a42f3dfd5fd30db99514cc54ad0a604681a3758d/demos/Delphi_VCL/JavaScript/JSExtension/uJSExtension.pas#L243

Here you have the JavaScript code that executes one of those functions :
https://github.com/salvadordf/CEF4Delphi/blob/a42f3dfd5fd30db99514cc54ad0a604681a3758d/demos/Delphi_VCL/JavaScript/JSExtension/uJSExtension.pas#L357

Here is the code that handles the custom functions in the render process in order to send the information to the main process :
https://github.com/salvadordf/CEF4Delphi/blob/a42f3dfd5fd30db99514cc54ad0a604681a3758d/demos/Delphi_VCL/JavaScript/JSExtension/uTestExtensionHandler.pas#L55

And here is the code that receives the information in the main application process :
https://github.com/salvadordf/CEF4Delphi/blob/a42f3dfd5fd30db99514cc54ad0a604681a3758d/demos/Delphi_VCL/JavaScript/JSExtension/uJSExtension.pas#L418

If you prefer a much easier way to get results from JavaScript consider using WebView4Delphi.
You can call TWVBrowserBase.ExecuteScriptWithResult and receive the result in TWVBrowserBase.OnExecuteScriptCompleted.
subin
Posts: 20
Joined: Thu Sep 05, 2024 5:15 am

Re: Retrun JS result Tchromium

Post by subin »

Thanks for your reply. My application is dll based application. When I use GlobalCEFApp.StartMainProcess in my code, I am getting error message binaries are missing. I coped the binaries from the demo folder to my application. When running my application I am getting error "Wron CEF binaries Use the 64 bit CEF binaries with 64 bits application only.". My application is a 32 bit application. Please help
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Retrun JS result Tchromium

Post by salvadordf »

The CEF API changes very frequently and CEF4Delphi checks that your application is trying to load the right binaries version.
Each CEF4Delphi release uses different CEF binaries.

Download the CEF binaries for Windows 32 bits using the link in the "README.md" file inside the CEF4Delphi package you installed in Delphi.

Then copy the contents of the Release and Resources directories to the same directory where your application is located.

The final layout looks like this :
Image

Read this for more information :
https://github.com/salvadordf/CEF4Delphi/wiki/Before-you-run-the-demos
Post Reply