简体   繁体   English

如何区分用户点击链接和执行自动重定向的页面?

[英]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: 在我的WinForms应用程序中有一个C# WebBrowser控件 ,并且知道Navigating事件 ,我从来没有想出一个漂亮而优雅的解决方案:

  1. If a user actively navigates to another URL, I want to allow it. 如果用户主动导航到另一个URL,我想允许它。
  2. If the page redirects "on its own" to another URL, I want to cancel it. 如果页面“自己”重定向到另一个URL,我想取消它。

For case 1 there are some cases I can think of: 对于案例1 ,我可以想到一些案例:

  • User clicks an a tag and the href attribute is evaluated to load another URL. 用户点击a标签和href属性进行评估,以加载另一个URL。
  • User clicks on an element with an onclick javascript event handler which calls a function that uses window.location to load another URL. 用户使用onclick javascript事件处理程序单击元素,该处理程序调用使用window.location加载另一个URL的函数。

For case 2 I can imagine of: 对于案例2,我可以想象:

  • The loaded page contains an iframe tag that loads an URL inside the IFrame. 加载的页面包含一个iframe标记,用于在IFrame中加载URL。 This fires the Navigating event. 这会触发Navigating事件。
  • There is some JavaScript timer that is started on page load and when it fires, it uses window.location to load another URL. 有一些JavaScript计时器在页面加载时启动,当它触发时,它使用window.location加载另一个URL。
  • The loaded page contains a meta refresh header tag to load another URL after some seconds. 加载的页面包含meta refresh标头标记,以在几秒钟后加载另一个URL。

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? 如何在Navigating事件(或任何其他机制)内部检测重定向是由用户显式触发还是由页面隐式触发?

Some more information 更多信息

  • The WebBrowser is being used inside a windows based CMS backend application. WebBrowser正在基于Windows的CMS后端应用程序中使用。
  • I therefore have full control over the content loaded inside the WebBrowser control. 因此,我可以完全控制WebBrowser控件中加载的内容。
  • Meaning that I can manipulate the complete HTML string before being sent to the browser, if required. 这意味着如果需要,我可以在发送到浏览器之前操作完整的HTML字符串。
  • If it is more applicable, I also would love to get JavaScript-only solutions which I could inject into the HTML being loaded. 如果它更适用,我也很乐意获得只能使用JavaScript的解决方案,我可以将其注入正在加载的HTML中。

(Please note that I do believe this is not a duplicate of this SO posting ) (请注意,我相信这不是此SO帖子的副本)

My take on this is capture user clicks on the web browser control. 我对此的看法是捕获用户对Web浏览器控件的点击。 Have it set a flag that indicates that the user clicked on the web browser. 让它设置一个标志,指示用户点击了Web浏览器。 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. 如果没有(或之后)重定向,请确保在n秒后重置标志。

It seems you are trying to achieve anti-ads/popup/redirect pattern. 您似乎正在尝试实现反广告/弹出/重定向模式。

From web browser perspective.. clicking <a href="some.url"> is not different from javascript window.location = "some.url"; 从Web浏览器的角度来看..点击<a href="some.url"> window.location = "some.url";与javascript window.location = "some.url";没有什么不同window.location = "some.url"; or 302 redirect response. 302重定向响应。 there are no explicit signals, no such convenience methods. 没有明确的信号,没有这样的便利方法。

The WebBrowser control is just a proxy to IE component. WebBrowser控件只是IE组件的代理。 You can't intercept browser's engine or even disable/enable javascript as it's part of internet security option. 你不能拦截浏览器的引擎,甚至禁用/启用javascript,因为它是互联网安全选项的一部分。

You have to create special logic to prevent every possible cases of redirection. 您必须创建特殊逻辑以防止每种可能的重定向情况。

eg. 例如。 verify HTML string then restrict some javascript pattern, header or iframe with Regex.Replace before render. 验证HTML字符串,然后在渲染之前使用Regex.Replace限制一些javascript模式,标题或iframe。

var scriptEx = new Regex("<script (.*?)</script>");
var iframeEx = new Regex("<iframe (.*?)</iframe>");

or intercept Navigating URL and cancel unsafe url, etc. 或拦截导航URL并取消不安全的URL等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过单击asp.net C#中gridview中的链接按钮,从管理页面重定向到用户页面 - Redirect from admin page to user page by clicking a link button in gridview in asp.net c# 如何区分用户输入,但允许应用程序本身更改文本 - How to distinguish between user input, but allow the application itself to change text 如何区分用户控件在窗体上加载和运行时加载 - How to distinguish between User Control load on form and load when runtime 如何通过单击链接滚动到页面中的某个位置 - how to scroll to a position in a page by clicking on link 使用watin自动点击带有Javascript的链接构建 - Automatic clicking on Link build with Javascript by using watin C#如何确保两个对象之间的自动双重链接 - C# How to ensure an automatic double link between two objects 如何区分Socket对象 - How to distinguish between Socket Objects 如何区分过帐表格? - How to distinguish between posted forms? 区分用户检查和程序检查 - Distinguish between User Checked and Programmatically checked IN 注销链接 如何将用户重定向到用户之前所在的同一区域 - IN Logout Link How TO Redirect the user to same area where user previous in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM