Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish between a user clicking a link and the page doing an automatic redirect?

Having a C# WebBrowser control inside my WinForms application, and being aware of the Navigating event, I never came up with a nice and elegant solution to the following:

  1. If a user actively navigates to another URL, I want to allow it.
  2. If the page redirects "on its own" to another URL, I want to cancel it.

For case 1 there are some cases I can think of:

  • User clicks an a tag and the href attribute is evaluated to load another URL.
  • User clicks on an element with an onclick javascript event handler which calls a function that uses window.location to load another URL.

For case 2 I can imagine of:

  • The loaded page contains an iframe tag that loads an URL inside the IFrame. This fires the Navigating event.
  • There is some JavaScript timer that is started on page load and when it fires, it uses window.location to load another URL.
  • The loaded page contains a meta refresh header tag to load another URL after some seconds.

So my question is:

How to detect inside the Navigating event (or any other mechanism) whether a redirect is triggered explicitly by the user or implicitly by the page?

Some more information

  • The WebBrowser is being used inside a windows based CMS backend application.
  • I therefore have full control over the content loaded inside the WebBrowser control.
  • Meaning that I can manipulate the complete HTML string before being sent to the browser, if required.
  • If it is more applicable, I also would love to get JavaScript-only solutions which I could inject into the HTML being loaded.

(Please note that I do believe this is not a duplicate of this SO posting)

like image 332
Uwe Keim Avatar asked Nov 03 '22 07:11

Uwe Keim


1 Answers

My take on this is capture user clicks on the web browser control. Have it set a flag that indicates that the user clicked on the web browser. If the flag is true, then allow redirection, if it isn't true don't allow it. Make sure to reset the flag after n number of seconds if no (or after) redirection is made.

like image 186
Andrei dela Cruz Avatar answered Nov 09 '22 09:11

Andrei dela Cruz