How to detect navigation in TWebBrowser as opposed to clicks on URLs?

I am using TWebBrowser to display some HTML content (locally). The application is divided in half, the upper half has 5 tabs, and the lower half is a web browser. Browser content changes frequently, for example, when you switch to another tab.

Displayed content includes URLs that I need to prevent and stop every click. I am trying to use the OnBeforeNavigate2 event, which detects every clicked link, but also detects when I programmatically call Navigate . Thus, it does not work anywhere.

I tried to wrap a call to Navigate , so I always call something like ...

 procedure TForm1.Nav(const S: String); begin FLoading:= True; try Web.Navigate(S); Sleep(50); finally FLoading:= False; end; end; 

(The dream was to just try and give it a split second to wait)

And then capturing the call ...

 procedure TForm1.WebBeforeNavigate2(ASender: TObject; const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool); begin if not FLoading then Cancel:= True; end; 

No luck trying this trick. How can I detect when a page or user is moving, and not when the browser is prompted to navigate?

+4
source share
1 answer

Please note that the following is undocumented. Some people say , and it works for me (on Windows 7 64-bit, with Internet Explorer 10) to check the magic constant of the Flags parameter. If the Flags parameter of the OnBeforeNavigate2 event is 64, it was the user who OnBeforeNavigate2 link. If the Flags parameter is 0, it was the IWebBrowser2::Navigate method that caused the navigation. In the code, it will be:

 procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool); begin if (Flags and navHyperlink) = navHyperlink then ShowMessage('User clicked a link...') else ShowMessage('IWebBrowser2::Navigate has been invoked...'); end; 

I would not be surprised if this is just an unintentionally undocumented flag value, because this part of MSDN is terrible.

+4
source

Source: https://habr.com/ru/post/1494282/


All Articles