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.
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.
Cancel Page Load
- salvadordf
- Posts: 4563
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Cancel Page Load
Hi,
Use the TWVBrowser.OnNavigationStarting event like this :
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
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;
https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.1185.39#add_navigationstarting
Re: Cancel Page Load
Thank you for taking the time to reply.
Your answer was perfect!
Your answer was perfect!
Re: Cancel Page Load
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
- salvadordf
- Posts: 4563
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Cancel Page Load
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
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