[英]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
事件 ,我从来没有想出一个漂亮而优雅的解决方案:
For case 1 there are some cases I can think of: 对于案例1 ,我可以想到一些案例:
a
tag and the href
attribute is evaluated to load another URL. 用户点击a
标签和href
属性进行评估,以加载另一个URL。 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,我可以想象:
iframe
tag that loads an URL inside the IFrame. 加载的页面包含一个iframe
标记,用于在IFrame中加载URL。 This fires the Navigating
event. 这会触发Navigating
事件。 window.location
to load another URL. 有一些JavaScript计时器在页面加载时启动,当它触发时,它使用window.location
加载另一个URL。 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 更多信息
WebBrowser
is being used inside a windows based CMS backend application. WebBrowser
正在基于Windows的CMS后端应用程序中使用。 (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.