Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: XE8 WebBrowser event OnShouldStartLoadWithRequest not firing?


This question is not answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 4 - Last Post: May 28, 2015 1:44 AM Last Post By: Andrew Baylis
Incus J

Posts: 5
Registered: 7/16/09
XE8 WebBrowser event OnShouldStartLoadWithRequest not firing?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 22, 2015 6:49 AM
Can anyone advise me on the correct usage of the event OnShouldStartLoadWithRequest in the FMX WebBrowser component on Windows and OSX?

The documentation indicates this event Occurs before loading a local file or a URL address …but the event never seems to fire.
I have a form containing an FMX WebBrowser, and a button which loads a web page. My Target is set to 32-bit Windows.

Here's my code:
procedure TForm1.Test(Sender:TObject; const URL:string);
begin
  //if URL.EndsWith('something') then WebBrowser1.Stop; //Prevent navigation occurring somehow.
  ShowMessage('Intercepted a navigation request to '+URL);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  s:string;
begin
  s:='www.xkcd.com';
  WebBrowser1.OnShouldStartLoadWithRequest:=Test;
  WebBrowser1.URL:=s;
  //WebBrowser1.ShouldStartLoading(s);
end;

I've tried using both .URL and .Navigate for the initial page load, and also clicking links on the currently loaded webpage. URLs load OK, but the procedure Test is never called. However if I uncomment the line WebBrowser1.ShouldStartLoading(s); then Test is immediately called, so the event handler is being assigned OK, but is not called on navigation for some reason.

I'm using the trial version of XE8, so I'm unable to step into the FMX source to see what is happening.

Purpose: What I'd like to achieve is to intercept any browser navigation just before it occurs, test the requested URL, and depending on its content, prevent the navigation occurring (ignore the request).
Andrew Baylis

Posts: 27
Registered: 7/19/00
Re: XE8 WebBrowser event OnShouldStartLoadWithRequest not firing?
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 23, 2015 12:59 AM   in response to: Incus J in response to: Incus J
On 22/05/2015 11:49 PM, Incus J wrote:
Can anyone advise me on the correct usage of the event OnShouldStartLoadWithRequest in the FMX WebBrowser component on Windows and OSX?

The documentation indicates this event Occurs before loading a local file or a URL address …but the event never seems to fire.
I have a form containing an FMX WebBrowser, and a button which loads a web page. My Target is set to 32-bit Windows.

Here's my code:
procedure TForm1.Test(Sender:TObject; const URL:string);
begin
   //if URL.EndsWith('something') then WebBrowser1.Stop; //Prevent navigation occurring somehow.
   ShowMessage('Intercepted a navigation request to '+URL);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
   s:string;
begin
   s:='www.xkcd.com';
   WebBrowser1.OnShouldStartLoadWithRequest:=Test;
   WebBrowser1.URL:=s;
   //WebBrowser1.ShouldStartLoading(s);
end;

I've tried using both .URL and .Navigate for the initial page load, and also clicking links on the currently loaded webpage. URLs load OK, but the procedure Test is never called. However if I uncomment the line WebBrowser1.ShouldStartLoading(s); then Test is immediately called, so the event handler is being assigned OK, but is not called on navigation for some reason.

I'm using the trial version of XE8, so I'm unable to step into the FMX source to see what is happening.

Purpose: What I'd like to achieve is to intercept any browser navigation just before it occurs, test the requested URL, and depending on its content, prevent the navigation occurring (ignore the request).
Unfortunately, this event doesn't seem to be called in any navigation
(despite the help doc). If you have the source, you could patch
procedure TWindowsWebBrowserService.WebBrowserBeforeNavigate2 to call
ShouldStartLoading.
ad lib

Posts: 1
Registered: 3/2/03
Re: XE8 WebBrowser event OnShouldStartLoadWithRequest not firing?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2015 3:31 AM   in response to: Andrew Baylis in response to: Andrew Baylis
(deleted)
Incus J

Posts: 5
Registered: 7/16/09
Re: XE8 WebBrowser event OnShouldStartLoadWithRequest not firing?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2015 5:18 AM   in response to: Andrew Baylis in response to: Andrew Baylis
Andrew Baylis wrote:
Unfortunately, this event doesn't seem to be called in any navigation
(despite the help doc). If you have the source, you could patch
procedure TWindowsWebBrowserService.WebBrowserBeforeNavigate2 to call
ShouldStartLoading.

Thanks Andrew. I guess I'd also need to patch the equivalent procedure for the OSX service (e.g. TOSXWebBrowserService...? ). It looks like this component has potential though.

Over on the VCL TWebBrowser, OnBeforeNavigate2 has a very useful Cancel parameter to prevent the pending navigation from occurring. I wonder if I could somehow create a new FMX 'TWebBrowserPlus' component that derives from FMX TWebBrowser, and implement OnBeforeNavigate(Sender:TObject; URL:string; var Cancel:boolean); or similar for both Windows and OSX targets?
Andrew Baylis

Posts: 27
Registered: 7/19/00
Re: XE8 WebBrowser event OnShouldStartLoadWithRequest not firing?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 28, 2015 1:44 AM   in response to: Incus J in response to: Incus J
On 26/05/2015 10:18 PM, Incus J wrote:
Andrew Baylis wrote:
Unfortunately, this event doesn't seem to be called in any navigation
(despite the help doc). If you have the source, you could patch
procedure TWindowsWebBrowserService.WebBrowserBeforeNavigate2 to call
ShouldStartLoading.

Thanks Andrew. I guess I'd also need to patch the equivalent procedure for the OSX service (e.g. TOSXWebBrowserService...? ). It looks like this component has potential though.

Over on the VCL TWebBrowser, OnBeforeNavigate2 has a very useful Cancel parameter to prevent the pending navigation from occurring. I wonder if I could somehow create a new FMX 'TWebBrowserPlus' component that derives from FMX TWebBrowser, and implement OnBeforeNavigate(Sender:TObject; URL:string; var Cancel:boolean); or similar for both Windows and OSX targets?
Sounds a good idea. I am surprised that EMB use a method which doesn't
allow a cancel flag to be passed back. Certainly there are webkit
notifications that allow you to check a URL and cancel before loading
(Android and iOS).
I'd offer to help, but at present am rather snowed under...maybe late
June if you want someone to collaborate with the project...

Cheers,
Andrew
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02