Page 1 of 1

Cancel Page Load

Posted: Wed Apr 27, 2022 1:11 pm
by solaris
Hi

I've just downloaded and starting to play around with TWVBrowser in Delphi XE5. Thank you for your great work.

My aim is to replace TWebBrowser with this component in a few of my projects. One of the things I use a lot in TWebBrowser is the "OnBeforeNavigate" event. Particulary:

var Cancel:WordBool

setting cancel := true enables me to block a page from loading. Is there a similar feature with any of the TWVBrowser events? I've taken a look but I am unable to see anything that will do the same job.

Re: Cancel Page Load

Posted: Wed Apr 27, 2022 6:27 pm
by salvadordf
Hi,

Use the TWVBrowser.OnNavigationStarting event like this :

Code: Select all

procedure TForm1.WVBrowser1NavigationStarting(Sender: TObject; const aWebView: ICoreWebView2; const aArgs: ICoreWebView2NavigationStartingEventArgs);
var
  TempArgs : TCoreWebView2NavigationStartingEventArgs;
begin
  TempArgs := TCoreWebView2NavigationStartingEventArgs.Create(aArgs);
  // here check if TempArgs.URI should be blocked
  // if you want to block that navigation call TempArgs.Cancel
  TempArgs.Free;
end;
This is the official documentation about that event :
https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.1185.39#add_navigationstarting

Re: Cancel Page Load

Posted: Thu Apr 28, 2022 8:13 am
by solaris
Thank you for taking the time to reply.

Your answer was perfect!

Re: Cancel Page Load

Posted: Thu Apr 28, 2022 1:13 pm
by pushca
Great! I am interested in how to cancel the loading of images in this way? Using this code I don't get a link to an image and other files like css or js

Re: Cancel Page Load

Posted: Thu Apr 28, 2022 1:37 pm
by salvadordf
The TWVBrowser.OnNavigationStarting event is used to block the navigation. It stops the browser before it tries to request the web page.

The MiniBrowser demo has a menu option to block loading the images. You need to call TWVBrowser.AddWebResourceRequestedFilter and then use the TWVBrowser.OnWebResourceRequested event to block the images :

https://github.com/salvadordf/WebView4Delphi/blob/b125c1afd33d18c6dcae986831b4d9b5f53b1e83/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L445

https://github.com/salvadordf/WebView4Delphi/blob/b125c1afd33d18c6dcae986831b4d9b5f53b1e83/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L645