我如何修改/“欺骗”运行时的标准浏览器 JS DOM 对象 (Window.location)?

发布于 2024-11-13 08:40:38 字数 738 浏览 3 评论 0原文

我想从 Web 浏览器中动态更改一些标准 JS DOM 对象。

例如,当我执行:

var site = location;

我想在运行时为浏览器的“window.location”对象指定一个新值,而不是“正确”值(用于访问请求页面的 URL),或者通过类似调试器如果需要的话,可以通过接口或什至以编程方式。

尽管 Firebug 宣称能够通过其“DOM Inspector”执行类似的操作,但每当我在通过其调试器暂停 Javascript 时尝试修改任何 DOM 值时,它都会忽略我输入的新值。经过一些研究后,根据此错误报告,这似乎是一个已知问题:http://code.google.com/p/fbug/issues/detail?id=1707

理论上,我可以编写一个程序来简单地打开一个 HTTP 套接字并模拟浏览器“用户代理”,但这对于我的目的来说似乎很麻烦。当我问的时候,有谁知道一个好的 Java/C# 库,其中包含模拟 HTTP 标头并解析接收到的 HTML/JS 的函数/对象?我长期以来一直梦想着这样一个库的存在,但我尝试过的大多数库(Java 的 Apache HttpClient、C# 的 System.Net.HttpWebRequest)都太低级,无法在最少的规划和短时间内做出任何有价值的事情的时间。

预先感谢您提供的建议和建议!

I'd like to dynamically change some of the standard JS DOM objects from within a web browser.

For instance, when I execute:

var site = location;

I want to specify a new value for my browser's "window.location" object other than the "correct" one (the URL used to access the requested page) at run time, either through a debugger-like interface or even programmatically if need be.

Although Firebug advertises the capability to do something similar via its "DOM Inspector," whenever I try to modify any of the DOM values while I've paused the Javascript via its debugger, it simply ignores the new value I enter. After doing some research, it seems that this is a known issue according to this bug report: http://code.google.com/p/fbug/issues/detail?id=1707 .

Theoretically, I could write a program to simply open up an HTTP socket and emulate a browser "user agent," but this seems like a lot of trouble for my purposes. While I'm asking, does anyone know a good Java/C# library with functions/objects that emulate HTTP headers and parse the received HTML/JS? I've long dreamt about the existence of such a library but most of the ones I've tried (Java's Apache HttpClient, C#'s System.Net.HttpWebRequest) are far too low-level to make anything worthwhile with minimal planning and a short period of time.

Thanks in advance for recommendations and advice you can provide!

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

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

发布评论

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

评论(1

记忆之渊 2024-11-20 08:40:38

不确定我的理解是否正确,但如果您想更改加载的 URL,可以通过设置 window.location.href 来实现。
如果您的目的是替换 DOM 内置对象,那么您会很难过,大多数内置对象(宿主对象)都不是常规的 JavaScript 对象,并且它们的行为没有明确定义。某些浏览器可能允许您替换和/或扩展某些对象,而在其他浏览器中它们根本不可替换/可扩展。

如果你想使用 JavaScript 来“编写浏览器脚本”,你绝对应该看看 node.js ,它是 http 模块。还有一个名为 html5 的第三方模块,它可以模拟 Node.js 中的 DOM,甚至允许使用 jQuery。

Not sure if I understand you correctly, but if you want to change the loaded URL you can do that by setting window.location.href.
If your intent is to replace DOM buildins then you will be sad to hear, that most build-in objects (host objects) aren't regular JavaScript objects and their behaviour is not clearly defined. Some browsers may allow you to replace and/or extend some objects while in other browsers they won't be replaceable/extendable at all.

If you want to "script a browser" using JavaScript, you should definitly have a look at node.js and it's http module. There's also a thirdparty module called html5 that simulates the DOM in node.js and even allows the usage of jQuery.

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