最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Overwriting XMLHttpRequest.responseText - Stack Overflow

programmeradmin0浏览0评论

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.

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.

I get no errors in the console, I've left ments 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'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.

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.

I get no errors in the console, I've left ments 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);
Share Improve this question asked May 28, 2014 at 1:02 user2984236user2984236 1671 silver badge8 bronze badges 12
  • 1 This sounds a bit fishy to say the least. – NinGen ShinRa Commented May 28, 2014 at 1:04
  • 1 It's not fishy, it's client-side. Any effects would only affect me – user2984236 Commented May 28, 2014 at 1:06
  • Well, unless you inject it somewhere else. – NinGen ShinRa Commented May 28, 2014 at 1:09
  • I guess but I assure you my intentions are pure. – user2984236 Commented May 28, 2014 at 1:13
  • 1 Here's a demo that you can work from: jsfiddle/yA2GD I grabbed the default "getter" function, kept a reference to it, then assigned a new getter and a setter. The setter now sets a ._custom_text property, and the new getter returns the ._custom_text if it exists, or otherwise calls the original getter to get the actual value. You can tweak it to change its behavior. – cookie monster Commented May 28, 2014 at 1:24
 |  Show 7 more ments

3 Answers 3

Reset to default 5

I know this is 3 years too late, but I Have included it here for anyone else that es across this thread. 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.

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

XMLHttpRequest.responseText is ReadOnly. This means there is no setter and therefore you can not modify its value. There is no workaround except you override XMLHttpRequest itself.

Specification

Edit Test for the suggestion to use Object.defineProperty to override 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:

var request = new XMLHttpRequest();
delete request.responseText;
request.responseText = 'test';
request.responseText; // returns 'test'
发布评论

评论列表(0)

  1. 暂无评论