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.

How to catch more queries "ExecuteScript"

Post Reply
polass
Posts: 13
Joined: Sun Jun 12, 2022 6:59 pm

How to catch more queries "ExecuteScript"

Post by polass »

Hi, I'm just starting with WebView4Delphi, maybe a stupid question:
after loading a web page I call ExecuteScript to get the text of the page source.

Code: Select all

procedure TfrmBrowser.wbBrowser_NavigationCompleted (Sender: TObject; const aWebView: ICoreWebView2; const aArgs: ICoreWebView2NavigationCompletedEventArgs);
begin
  Sleep(1000);
  (* to display the complete page *)
  wbBrowser.ExecuteScript('document.body.outerText');
end;
and in the next procedure I get back the result

Code: Select all

procedure TfrmBrowser.wbBrowser_ExecuteScriptCompleted  (Sender: TObject; aErrorCode: HRESULT; const aResultObjectAsJson: wvstring; aExecutionID: Integer);
begin
  if AResultObjectAsJson <> 'null' then
  begin
    mem1.lines.add(UTF8Encode(aResultObjectAsJson));
  end;
end;
My question: is it possible to send multiple "ExecuteScript" commands at the same time? For example

Code: Select all

procedure TfrmBrowser.wbBrowser_NavigationCompleted (Sender: TObject; const aWebView: ICoreWebView2; const aArgs: ICoreWebView2NavigationCompletedEventArgs);
begin
  Sleep(1000);
  (* to display the complete page *)
  wbBrowser.ExecuteScript(**** first command ****);
  wbBrowser.ExecuteScript(**** another command ****);
end;
How do I identify which ExecuteScript arrived in wbBrowser_ExecuteScriptCompleted to the aResultObjectAsJson variable? I believe the "aExecutionID" variable is used for this, but I have no idea how.
Thank you very much for your ideas/answers
polass
Posts: 13
Joined: Sun Jun 12, 2022 6:59 pm

Re: How to catch more queries "ExecuteScript"

Post by polass »

God, is it that easy? :shock:

Code: Select all

procedure TfrmBrowser.wbBrowser_NavigationCompleted (Sender: TObject; const aWebView: ICoreWebView2; const aArgs: ICoreWebView2NavigationCompletedEventArgs);
begin
  Sleep(1000);
  (* to display the complete page *)
  wbBrowser.ExecuteScript(**** first command ****, 1000);
  wbBrowser.ExecuteScript(**** another command , 2000);
end;
and obtaining the results

Code: Select all

procedure TfrmBrowser.wbBrowser_ExecuteScriptCompleted  (Sender: TObject; aErrorCode: HRESULT; const aResultObjectAsJson: wvstring; aExecutionID: Integer);
begin
  if AResultObjectAsJson <> 'null' then
  begin
	if aExecutionID = 1000 then ....
	else if aExecutionID = 2000 then .... 
  end;
end;
am I doing this right? I guess so, I have a question, can the results come in a different order?
Thanks for the reply, hopefully it will help other llamas like me
User avatar
salvadordf
Posts: 4563
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to catch more queries "ExecuteScript"

Post by salvadordf »

Hi,

Yes. The ExecutionID parameter is used like that. The order is decided by Edge/Chromium based on the code that is executed.

Just one tip : avoid the sleep calls because they block your application. It's recommended to use a simple TTimer instead.
polass
Posts: 13
Joined: Sun Jun 12, 2022 6:59 pm

Re: How to catch more queries "ExecuteScript"

Post by polass »

Just one tip : avoid the sleep calls because they block your application. It's recommended to use a simple TTimer instead.
Thanks so much for the quick reply!
add "SLEEP" : does it block only the execution of my program or also the execution of the script? I assume only my program.
Thanks.
User avatar
salvadordf
Posts: 4563
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to catch more queries "ExecuteScript"

Post by salvadordf »

It stops the execution of your application but I don't know if the code inside the WebView2 runtime was designed to keep working when the host application is not responding.
Post Reply