覆盖 Web 浏览器控件上的 window.location

发布于 2024-10-01 02:22:34 字数 169 浏览 0 评论 0原文

这似乎是一个奇怪的问题,但是有没有办法覆盖 window.location 而不使浏览器控件导航到它?我遇到的问题是我在控件中注入 html 代码,而 window.location 是 about:blank - 导致某些 javascript 代码出现错误。试图让他们认为自己所在的 URL 不是 about:blank

This might seem a weird question, but is there a way to override the window.location without making the browser control navigate to it? The problem I am having is that i am injecting html code in the control, and the window.location is about:blank - causing errors on some javascript code. Trying to make them think they are in a URL other than about:blank

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感悟人生的甜 2024-10-08 02:22:34

document.documentMode >= 9 (IE9+) 时,我已经成功使用 Object.defineProperty()

为了在 IE9+ 模式下获取 WebBrowser 控件,我首先加载以下 URL:

about:<!doctype html><meta http-equiv="X-UA-Compatible" content="IE=edge">

这在最新文档模式下给出了一个几乎为空的文档。

然后,为了重新定义 window.location,我通过 eval() 或 document.write() 执行一些脚本:

Object.defineProperty(window, 'location', {value: {href: 'fubar'}});

完整的序列如下:

  • 加载控件。
  • 等待 WebBrowser.ReadyState == 4DocumentComplete 事件。
  • 调用document.open()(重要)。
  • 评估或编写重新定义位置的脚本。
  • 编写 HTML 内容。
  • 调用 document.close() (确保调用 onload)。

注意:我使用 ActiveX WebBrowser 控件,而不是 .NET 组件该控件的包装器。它在 .NET 中的工作方式可能相同。

I've had success with Object.defineProperty() when document.documentMode >= 9 (IE9+).

To get the WebBrowser control in IE9+ mode, I first load the following URL:

about:<!doctype html><meta http-equiv="X-UA-Compatible" content="IE=edge">

This gives a mostly empty document in the latest document mode.

Then to redefine window.location, I execute some script via eval() or document.write():

Object.defineProperty(window, 'location', {value: {href: 'fubar'}});

The complete sequence is like this:

  • Load the control.
  • Wait for WebBrowser.ReadyState == 4 or the DocumentComplete event.
  • Call document.open() (important).
  • Eval or write the script redefining location.
  • Write the HTML content.
  • Call document.close() (ensures onload gets called).

Note: I use the ActiveX WebBrowser control, and not the .NET component which is a wrapper around that control. It will probably work the same in .NET.

卖梦商人 2024-10-08 02:22:34

HTML5 草案包括 window .history.pushState+window.onpopstate,但这不受 Trident (MSHTML) 支持,并且不允许跨域导航,更不用说 URL 方案了。

一些 JavaScript 实现还支持用户定义的 getter 和 setter,因此您可以执行 window.__defineGetter__('location', function() { return fakeLocation }),但我再次相信这不会与IE的控件一起工作。

回应评论者:你在做什么?

HTML5 drafts include window.history.pushState+window.onpopstate, but this is not supported by Trident (MSHTML), and doesn't allow for navigating across domains, nevermind URL schemes.

Some JavaScript implementations also support user-defined getters and setters, so you could maybe do window.__defineGetter__('location', function() { return fakeLocation }), but yet again I believe that won't work with IE's control.

Echoing the commenters: what are you doing?

我一直都在从未离去 2024-10-08 02:22:34

无法替换 window.location 属性。 window.location 是一个不可配置的属性,这意味着它无法修改。

我可以使用的唯一混乱的解决方法是在执行脚本文本之前进行查找和替换(用空字符串或其他内容替换 window.location 行)。

It is not possible to replace the window.location property. window.location is a non-configurable property, which means that it cannot be modified.

The only messy workaround that I can use is to do a find and replace (replace the window.location line with an empty string or something) on the script text before executing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文