简体   繁体   English

覆盖XMLHttpRequest.responseText

[英]Overwriting XMLHttpRequest.responseText

I'll do my best to explain my problem but if I'm honest, I'm a little confused myself so I can't imagine it'll be much easier for you guys. 我会尽力解释我的问题,但是老实说,我有点困惑,所以我无法想象这对你们会容易得多。

Right, I'm creating a script for a userscript for a website I frequent. 正确,我正在为我经常访问的网站的用户脚本创建脚本。 What I'm trying to do is hijack any ajax requests, which I'm doing fine and then modify the responseText. 我想做的是劫持任何ajax请求,我做得很好,然后修改responseText。

I can't seem to write to responseText, I can read it fine and it shows the response fine but I can't change it's value no matter what I try. 我似乎无法写出responseText,我可以很好地阅读它,并且它可以很好地显示响应,但是无论我尝试什么,我都无法更改其值。

I get no errors in the console, I've left comments in my code to show what does log. 我在控制台中没有看到任何错误,我在代码中留下了注释以显示要记录的内容。

I was just going to scrap it but knowing me, I've missed something stupidly obvious and just can't see it. 我只是想把它报废,但了解我,我已经错过了一些显而易见的事情,只是看不见。

Thanks in advance. 提前致谢。

(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);

I know this is 3 years too late, but I Have included it here for anyone else that comes across this thread. 我知道这已经晚了3年,但是我已经将它包含在此供其他遇到此线程的人使用。 I've done this before... Here is a copy and paste directly from my script. 我之前已经完成过此操作……这是直接从我的脚本中复制并粘贴的内容。 You should be able to just change the responseText to be writable. 您应该能够将responseText更改为可写。

Object.defineProperty(this, "responseText", {writable: true});
this.responseText = '{"success":true}';

XMLHttpRequest.responseText is ReadOnly. XMLHttpRequest.responseText为ReadOnly。 This means there is no setter and therefore you can not modify its value. 这意味着没有设置器,因此您不能修改其值。 There is no workaround except you override XMLHttpRequest itself. 除了替代XMLHttpRequest本身,没有其他解决方法。

Specification 规格

Edit Test for the suggestion to use Object.defineProperty to override responseText : 编辑测试的建议,以使用Object.defineProperty覆盖responseText

var xhr = new XMLHttpRequest();
Object.defineProperty( xhr, "responseText", { value: "test" });
xhr.responseText // returns ""

So this won't work either 所以这也不行

this worked for me in chrome: 这对我来说适合chrome:

var request = new XMLHttpRequest();
delete request.responseText;
request.responseText = 'test';
request.responseText; // returns 'test'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM