将代码附加到方法
我试图将一些代码附加到 XMLHttpRequest 对象的方法中,但有些东西不起作用。相关代码:
XMLHttpRequest.prototype.oldOpen = XMLHttpRequest.prototype.open;
var newOpen = function(method, url, async, user, password) {
alert( "[debug info]" );
this.oldOpen(method, url, async, user, password);
}
XMLHttpRequest.prototype.open = newOpen;
当调用 open() 时,警报响起,但原始函数没有被调用。这是为什么呢?
作为参考,它是 GreaseMonkey 脚本的一部分,用于监听 XHR 流量。完整脚本如下:
var XMLHttpRequest = unsafeWindow.XMLHttpRequest;
var startTracing = function () {
XMLHttpRequest.prototype.uniqueID = function() {
// each XMLHttpRequest gets assigned a unique ID and memorizes it
// in the "uniqueIDMemo" property
if (!this.uniqueIDMemo) {
this.uniqueIDMemo = Math.floor(Math.random() * 1000);
}
return this.uniqueIDMemo;
}
// backup original "open" function reference
XMLHttpRequest.prototype.oldOpen = XMLHttpRequest.prototype.open;
var oOpen = XMLHttpRequest.prototype.open;
var newOpen = function(method, url, async, user, password) {
alert("open: " + method + "," + url + "," + async + "," + user + "," + password );
this.oldOpen(method, url, async, user, password);
}
XMLHttpRequest.prototype.open = newOpen;
}
startTracing();
I'm trying to append some code to a method of an XMLHttpRequest object, but something is not working. The relevant code:
XMLHttpRequest.prototype.oldOpen = XMLHttpRequest.prototype.open;
var newOpen = function(method, url, async, user, password) {
alert( "[debug info]" );
this.oldOpen(method, url, async, user, password);
}
XMLHttpRequest.prototype.open = newOpen;
When open() is called, the alert goes off, but the original function is not called. Why is this?
For reference, it's part of a GreaseMonkey script supposed to listen to XHR traffic. The full script follows:
var XMLHttpRequest = unsafeWindow.XMLHttpRequest;
var startTracing = function () {
XMLHttpRequest.prototype.uniqueID = function() {
// each XMLHttpRequest gets assigned a unique ID and memorizes it
// in the "uniqueIDMemo" property
if (!this.uniqueIDMemo) {
this.uniqueIDMemo = Math.floor(Math.random() * 1000);
}
return this.uniqueIDMemo;
}
// backup original "open" function reference
XMLHttpRequest.prototype.oldOpen = XMLHttpRequest.prototype.open;
var oOpen = XMLHttpRequest.prototype.open;
var newOpen = function(method, url, async, user, password) {
alert("open: " + method + "," + url + "," + async + "," + user + "," + password );
this.oldOpen(method, url, async, user, password);
}
XMLHttpRequest.prototype.open = newOpen;
}
startTracing();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了这个,这本质上是对您的代码的重写,并且效果很好。这是我的整个油脂猴脚本。
Greasemonkey 脚本加载时存在计时问题。您可能需要等待 window.onload 在测试页面上启动 XHTTPRequest。
我还建议您的 uniqueId 方案可能会发生一些冲突。将 new Date().getTime() 与 Math.random 结合使用会将其减少到接近 0。类似于:
new Date().getTime() + '' + Math.floor(Math.random() * 1000)
I tried this, which is essentially a rework of your code, and it worked just fine. Here is my whole greasemonkey script.
There is a timing issue when greasemonkey scripts load. You may have to wait for window.onload to fire up your XHTTPRequest on your test page.
May I also suggest that your uniqueId scheme will probably have some collisions. Using new Date().getTime() in conjunction with Math.random would reduce those to nearly 0. Something like:
new Date().getTime() + '' + Math.floor(Math.random() * 1000)