如何点击一个链接,做一个自动重定向的页面,用户区分? [英] How to distinguish between a user clicking a link and the page doing an automatic redirect?

查看:218
本文介绍了如何点击一个链接,做一个自动重定向的页面,用户区分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个C# web浏览器我的WinForms应用程序内部控制,并成为意识到的 导航事件,我从未与一个漂亮和优雅的解决方案如下想出了:

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. 如果用户主动导航到另一个网址,我想允许它。

  2. 如果页面重定向自行到另一个URL,我想取消它。

  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.

有关案例1 有一些情况下,我能想到的:

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


  • 用户点击 A 标记和的href 属性进行评估,以加载另一个URL。

  • 与元素上的用户点击的onclick JavaScript事件处理程序调用它使用一个函数 window.location的来加载另一个URL。

  • 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.

对于案例2 我可以想像的:


  • 加载页面包含加载iframe内URL的 IFRAME 标记。这触发导航事件。

  • 有是在页面加载启动一些JavaScript计时器,火灾时,它使用 window.location的来加载另一个URL。

  • 加载的页面包含的 元刷新头标记在几秒钟后加载另一个URL。

  • 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.

所以我的问题是:

如何到<$ C $内检测C>导航事件(或其他机构)是否重定向用户或隐式的页面明确触发?

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?

一些更多的信息


  • web浏览器正在一个Windows内部使用基于CMS后台的应用程序。

  • 因此,我有过在WebBrowser控件内加载的内容的完全控制。

  • 意思就是说我可以操控完整的HTML字符串之前被发送到浏览器,如果需要的话。

  • 如果是比较适用的,我也很想让JavaScript的唯一的解决办法,我可以注入HTML加载。

  • 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.

(请注意,我不相信这不是的这个SO发布

推荐答案

据。看来你是努力实现反广告/弹出/重定向模式

It seems you are trying to achieve anti-ads/popup/redirect pattern.

从Web浏览器的角度来看..点击< A HREF =一些的.url> 不是从JavaScript不同 window.location的=some.url; 302重定向响应。有没有明确的信号,没有这样的便利方法。

From web browser perspective.. clicking <a href="some.url"> is not different from javascript window.location = "some.url"; or 302 redirect response. there are no explicit signals, no such convenience methods.

WebBrowser控件只是一个代理IE组件。你不能拦截浏览器的引擎,甚至禁用/启用JavaScript,因为它是互联网安全选项的一部分。

The WebBrowser control is just a proxy to IE component. You can't intercept browser's engine or even disable/enable javascript as it's part of internet security option.

您必须创建特殊的逻辑,以防止重定向的每一个可能的情况。

You have to create special logic to prevent every possible cases of redirection.

如。
验证HTML字符串然后限制一些JavaScript格局,头或iframe与Regex.Replace之前渲染。

eg. verify HTML string then restrict some javascript pattern, header or iframe with Regex.Replace before render.

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

或拦截导航网址并取消不安全的网址等。

or intercept Navigating URL and cancel unsafe url, etc.

这篇关于如何点击一个链接,做一个自动重定向的页面,用户区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆