将代码附加到方法

发布于 2024-10-08 04:35:04 字数 1321 浏览 2 评论 0原文

我试图将一些代码附加到 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 技术交流群。

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

发布评论

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

评论(1

雪若未夕 2024-10-15 04:35:04

我尝试了这个,这本质上是对您的代码的重写,并且效果很好。这是我的整个油脂猴脚本。

// ==UserScript==
// @name           sof
// @namespace      taylor.kelly.sof
// @description    Stack overflow testing
// @include        http://localhost:8080/tests/sof.html
// ==/UserScript==

var XMLHttpRequest = unsafeWindow.XMLHttpRequest;
var startTracing = function () {
    // backup original "open" function reference
    var open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        alert("open: " + method + "," + url + "," + async + "," + user + "," + password );
        open.apply(this, arguments);
    }
}

startTracing();

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.

// ==UserScript==
// @name           sof
// @namespace      taylor.kelly.sof
// @description    Stack overflow testing
// @include        http://localhost:8080/tests/sof.html
// ==/UserScript==

var XMLHttpRequest = unsafeWindow.XMLHttpRequest;
var startTracing = function () {
    // backup original "open" function reference
    var open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        alert("open: " + method + "," + url + "," + async + "," + user + "," + password );
        open.apply(this, arguments);
    }
}

startTracing();

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)

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