Page 1 of 1

ExecuteScript - get the complete source website

Posted: Tue Jun 28, 2022 6:38 am
by polass
please help a beginner - how to use ExecuteScript to get the complete page source -
something like browser does with

Code: Select all

view-source: https//****
If I call ExecuteScript

Code: Select all

wbBrowser.ExecuteScript('document.documentElement.outerHTML')
I only get what is displayed on the page. However, there are web pages that display data in a table and currently only display a portion of the records (e.g. 1-10 out of 1000).
By calling

Code: Select all

wbBrowser.ExecuteScript('document.documentElement.outerHTML')
i only get the current 1-10, but the browser will display the complete data using

Code: Select all

view-source: https//****
.
I hope I have described it clearly, sorry for the my English. Thanks.

Re: ExecuteScript - get the complete source website

Posted: Tue Jun 28, 2022 7:55 am
by salvadordf
Hi,

Run the MiniBrowser demo and see if the "Save resource as..." menu option has what you need.

Re: ExecuteScript - get the complete source website

Posted: Tue Jun 28, 2022 9:01 am
by polass
salvadordf wrote: Tue Jun 28, 2022 7:55 am Hi,
Run the MiniBrowser demo and see if the "Save resource as..." menu option has what you need.
yes, that's it! great!

my "knowledge" led me to the fact that in the demo in the procedure

Code: Select all

WVBrowser1WebResourceResponseViewGetContentCompleted
saves the complete page source to the STREAM variable

Code: Select all

FResourceContents
please complete the answer:
in my program my code is not passing at all the procedure ** WVBrowser1WebResourceResponseViewGetContentCompleted ** i can't figure out why?

Thanks

Re: ExecuteScript - get the complete source website

Posted: Tue Jun 28, 2022 9:45 am
by salvadordf
Use the TWVBrowser.OnWebResourceResponseReceived event, create a TCoreWebView2WebResourceResponseView instance and call TCoreWebView2WebResourceResponseView.GetContent

Then you will receive the TWVBrowser.OnWebResourceResponseViewGetContentCompleted event.

See the code in TMiniBrowserFrm.WVBrowser1WebResourceResponseReceived.

Re: ExecuteScript - get the complete source website

Posted: Tue Jun 28, 2022 9:56 am
by polass
polass wrote: Tue Jun 28, 2022 9:01 am
salvadordf wrote: Tue Jun 28, 2022 7:55 am Hi,
Run the MiniBrowser demo and see if the "Save resource as..." menu option has what you need.
yes, that's it! great!

my "knowledge" led me to the fact that in the demo in the procedure

Code: Select all

WVBrowser1WebResourceResponseViewGetContentCompleted
saves the complete page source to the STREAM variable

Code: Select all

FResourceContents
please complete the answer:
in my program my code is not passing at all the procedure ** WVBrowser1WebResourceResponseViewGetContentCompleted ** i can't figure out why?

Thanks
I added out of desperation

Code: Select all

WVBrowser1WebResourceResponseReceived
including the code (which I don't understand at all),
the program doesn't go into any of the procedures ....
please help.

Re: ExecuteScript - get the complete source website

Posted: Tue Jun 28, 2022 10:11 am
by polass
salvadordf wrote: Tue Jun 28, 2022 9:45 am Use the TWVBrowser.OnWebResourceResponseReceived event, create a TCoreWebView2WebResourceResponseView instance and call TCoreWebView2WebResourceResponseView.GetContent

Then you will receive the TWVBrowser.OnWebResourceResponseViewGetContentCompleted event.

See the code in TMiniBrowserFrm.WVBrowser1WebResourceResponseReceived.
what am I doing wrong? I don't get the step program into procedure **OnWebResourceResponseReceived ** at all (I have here a test "memo" entry and a stopwatch, nothing)

Code: Select all

procedure TfrmBrowser.wbBrowserWebResourceResponseReceived(Sender: TObject;
    const aWebView: ICoreWebView2; const aArgs:
    ICoreWebView2WebResourceResponseReceivedEventArgs);
var
  TempArgs     : TCoreWebView2WebResourceResponseReceivedEventArgs;
  TempResponse : TCoreWebView2WebResourceResponseView;
  TempHeaders  : TCoreWebView2HttpResponseHeaders;
  TempIterator : TCoreWebView2HttpHeadersCollectionIterator;
  TempName     : wvstring;
  TempValue    : wvstring;
  TempHandler  : ICoreWebView2WebResourceResponseViewGetContentCompletedHandler;
begin
  debug_memo('Resource start');
  if FGetHeaders then
    try
      debug_memo('Resource work');
      FHeaders.Clear;
      FGetHeaders  := False;
      TempArgs     := TCoreWebView2WebResourceResponseReceivedEventArgs.Create(aArgs);
      TempResponse := TCoreWebView2WebResourceResponseView.Create(TempArgs.Response);
      TempHandler  := TCoreWebView2WebResourceResponseViewGetContentCompletedHandler.Create(wbBrowser);
      TempHeaders  := TCoreWebView2HttpResponseHeaders.Create(TempResponse.Headers);
      TempIterator := TCoreWebView2HttpHeadersCollectionIterator.Create(TempHeaders.Iterator);

      // GetContent will trigger the TWVBrowserBase.OnWebResourceResponseViewGetContentCompleted
      // event with the contents of this resource, which in this case corresponds to the HTML contents.
      TempResponse.GetContent(TempHandler);

      while TempIterator.HasCurrentHeader do
        begin
          if TempIterator.GetCurrentHeader(TempName, TempValue) then
            FHeaders.Add(TempName + ': ' + TempValue);

          TempIterator.MoveNext;
        end;
    finally
      FreeAndNil(TempIterator);
      FreeAndNil(TempHeaders);
      FreeAndNil(TempResponse);
      FreeAndNil(TempArgs);
      TempHandler := nil;
    end;
end;

Re: ExecuteScript - get the complete source website

Posted: Tue Jun 28, 2022 10:51 am
by polass
I am very sorry, my mistake .... for some reason the procedure ** wbBrowserWebResourceResponseReceived ** was not assigned to the browser ...
Thanks again !