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.

How to know Javascript executed and return values

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

Re: How to know Javascript executed and return values

Post by salvadordf »

Hi,

Follow these steps :
  • Register a JavaScript extension as described in the JSRTTIExtension or JSExtension demos.
  • Modify your JavaScript code to call the extension function when your JavaScript code has completed its work.
  • Send a process message in the JavaScript extension function to send a process message to the browser process.
  • Use the TChromium.OnProcessMessageReceived event to receive the message from the extension.
  • Since TChromium.OnProcessMessageReceived is executed in a different thread, I would send a Windows message from that event to the main form to notify your application that your JavaScript code has ended.
To see this in action build the JSRTTIExtension demo and run it. When the first web page is fully loaded, click the right mouse button and select "Set mouseover event". Then move the mouse and you will see some HTML elements in the status bar.

The JSRTTIExtension and JSExtension demos don't send a Windows message from TChromium.OnProcessMessageReceived to the main form to keep it as simple as possible but it's recommended to do it. Specially if you need to modify any VCL component when the browser receives the process message.

Read the code comments in those demos for more information and read the CEF3 document describing extensions here :
https://bitbucket.org/chromiumembedded/ ... gration.md
User avatar
salvadordf
Posts: 4040
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to know Javascript executed and return values

Post by salvadordf »

I would split the functions that need the GetElementById result in 2 and remove the Application.ProcessMessages part.

Let's say a function called CustomFunction calls GetElementById.
I would create a new CustomFunction1 with first section of the CustomFunction code up to and including the call to GetElementById.
Then I would also create another function called CustomFunction2 with the rest of the CustomFunction code.

The execution sequence would be like this :
  • CustomFunction1 is called.
  • GetElementById is called inside the CustomFunction1 function but it only executes the JavaScript code.
  • The JavaScript code is executed and it calls a JS extension function.
  • The JS extension function sends a process message to the main browser process with the result.
  • The browser process receives the process message with the result and calls CustomFunction2.
Basically, it would be like this :
CustomFunction1 -> (JS code calling your extension and sending the result in a process message) -> CustomFunction2
Post Reply