覆盖 XMLHttpRequest.responseText

use*_*236 0 javascript

我会尽力解释我的问题,但说实话,我自己也有点困惑,所以我无法想象这对你们来说会容易得多。

是的,我正在为我经常访问的网站的用户脚本创建一个脚本。我想做的是劫持任何ajax请求,我做得很好,然后修改responseText。

我似乎无法写入responseText,我可以很好地阅读它并且它显示了很好的响应,但无论我如何尝试,我都无法更改它的值。

我在控制台中没有收到任何错误,我在代码中留下了注释以显示记录的内容。

我本来打算放弃它,但了解我自己,我错过了一些愚蠢而明显的东西,只是看不到它。

提前致谢。

(function(send) { 
    XMLHttpRequest.prototype.send = function(data) { 
        this.addEventListener('readystatechange', function() { 
            if(typeof data == 'string'){
                if(data.indexOf('room.details_1') > -1){
                    if(this.readyState == 4 && this.status == 200){
                        console.log('Before: ' + JSON.parse(this.responseText).body.user.profile.username); // Shows NameNumber1
                        var temp = JSON.parse(this.responseText);
                        temp.body.user.profile.username = 'NameNumber2';
                        this.responseText = JSON.stringify(temp);
                        console.log('Temp: ' + temp.body.user.profile.username); // Shows NameNumber2
                        console.log('After: ' + JSON.parse(this.responseText).body.user.profile.username); // Shows NameNumber1 <-- This is the problem.
                        console.log(this); // Shows the XMLHttpRequest object, with the original responseText rather than the modified one.
                    }
                }
            }
        }, false);
        send.call(this, data);
    }; 
})(XMLHttpRequest.prototype.send);
Run Code Online (Sandbox Code Playgroud)

hog*_*gkm 5

我知道这已经晚了 3 年,但我已将其包含在此处,以供遇到此线程的其他人使用。我以前做过这个...这是直接从我的脚本复制并粘贴的内容。您应该能够将responseText更改为可写。

Object.defineProperty(this, "responseText", {writable: true});
this.responseText = '{"success":true}';
Run Code Online (Sandbox Code Playgroud)