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.

Cancel Page Load

Post Reply
solaris
Posts: 3
Joined: Wed Apr 27, 2022 1:01 pm

Cancel Page Load

Post 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.
User avatar
salvadordf
Posts: 4563
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Cancel Page Load

Post 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
solaris
Posts: 3
Joined: Wed Apr 27, 2022 1:01 pm

Re: Cancel Page Load

Post by solaris »

Thank you for taking the time to reply.

Your answer was perfect!
pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

Re: Cancel Page Load

Post 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
User avatar
salvadordf
Posts: 4563
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Cancel Page Load

Post 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
Post Reply