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.

delphi get return value from javascript function

Post Reply
User avatar
salvadordf
Posts: 4042
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: delphi get return value from javascript function

Post by salvadordf »

You can register extensions that you can call in JavaScript code and send the result in Delphi code.
The JSRTTIExtension and JSExtension demos use this method to send the results.

For more information read this post :
https://www.briskbard.com/forum/viewtop ... =436#p1874
User avatar
salvadordf
Posts: 4042
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: delphi get return value from javascript function

Post by salvadordf »

I just added more code comments to the JSExtension demo.
Please, read the new comments in uJSExtension.pas even if you use the JSRTTIExtension demo.

Code: Select all

// To test this demo follow these steps :
// ======================================
// 1. Run the demo and wait until google.com is loaded
// 2. Right-click and select the "Set the mouseover event" menu option.
// 3. Move the mouse pointer over the web page and see the HTML elements in the status bar.

// The CEF3 document describing extensions is here :
// https://bitbucket.org/chromiumembedded/cef/wiki/JavaScriptIntegration.md

// This demo has a JavaScript extension class that is registered in the
// GlobalCEFApp.OnWebKitInitialized event when the application is initializing.

// The extension in this demo is called "myextension" and it has 2 functions called "mouseover" and "sendresulttobrowser".
// When the JavaScript code uses those functions it executes the TTestExtensionHandler.Execute function in uTestExtensionHandler.pas

// The TTestExtensionHandler.Execute function is executed in the renderer process and it can use the
// TCefv8ContextRef.Current.Browser.SendProcessMessage(PID_BROWSER, msg) to send a message with the results to the browser process.

// TCefv8ContextRef.Current returns the v8 context for the frame that is currently executing JavaScript,
// TCefv8ContextRef.Current.Browser.SendProcessMessage sends a message to the right browser even
// if you have created several browsers in one app.

// That message is received in the TChromium.OnProcessMessageReceived event.
// Even if you create several TChromium objects you should have no problem because each of them will have its own
// TChromium.OnProcessMessageReceived event to receive the messages from the extension.

// When run this demo and you select the "Set the mouseover event" menu option, the Chromium1ContextMenuCommand event
// is triggered and it adds an event listener to the document's body. That listener calls one of the functions
// available in the registered extension called "myextension.mouseover".

// TChromium.OnProcessMessageReceived receives that message and shows the information in the status bar.

// If you have to debug the code executed by the extension you will need to use the debugging methods described in
// https://www.briskbard.com/index.php?lang=en&pageid=cef
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: delphi get return value from javascript function

Post by X11 »

Is it possible to transfer text without an extension, for example, through a browser console?
User avatar
salvadordf
Posts: 4042
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: delphi get return value from javascript function

Post by salvadordf »

This is the JavaScript Integration wiki in the CEF project website :
https://bitbucket.org/chromiumembedded/ ... gration.md

The CEF4Delphi demos in the JavaScript directory cover most of the things commented in that document.

I'm not a JavaScript expert and I don't know if it's possible to communicate through the console. :oops:
Post Reply