欺骗xmlhttprequest(greasemonkey)

时间:2015-06-28 12:07:33

标签: javascript ajax xmlhttprequest greasemonkey

我从greasemonkey阻止XMLHttpRequest,但是当它没有响应时页面出错。所以我尝试了欺骗,好像响应已经收到了。不可原谅的是,XMLHttpRequest似乎只有字段只读。所以我创建了一个假对象:

fakeresponse = "for (;;);"+JSON.stringify(fakeresponse);


var xhr2 = {};
xhr2.readyState = 0;
if(xhr.onreadystatechange!==undefined) xhr.onreadystatechange.call(xhr2);
xhr2.readyState = 1;
if(xhr.onreadystatechange!==undefined) xhr.onreadystatechange.call(xhr2);
xhr2.readyState = 2;
if(xhr.onreadystatechange!==undefined) xhr.onreadystatechange.call(xhr2);
xhr2.readyState = 3;
if(xhr.onreadystatechange!==undefined) xhr.onreadystatechange.call(xhr2);
xhr2.response = fakeresponse;
xhr2.responseText = fakeresponse;
xhr2.responseXML = fakeresponse;
xhr2.status = 200;
xhr2.readyState = 4;

if(xhr.onreadystatechange!==undefined) xhr.onreadystatechange.call(xhr2);

对象是:

Object {response: "for (;;);{"__ar":1,"payload":{"actions":[{"..."},"bootloadable":{},"ixData":{},"lid":"0"}", responseText: "for (;;);...", responseXML: "for (;;);....", status: 200, readyState: 4}

但没有任何反应..有没有其他方法我可以模拟这个或者我是否需要深入调试一下?页面也使用库而不是纯xhr对象,这可能是个问题吗?

1 个答案:

答案 0 :(得分:0)

(function(xhr, value){
Object.defineProperty(xhr, "response", {
  get: function() {
    return value;
  }
});

Object.defineProperty(xhr, "responseText", {
  get: function() {
    return value;
  }
});

Object.defineProperty(xhr, "responseXML", {
  get: function() {
    return value;
  }
});
Object.defineProperty(xhr, "status", {
  get: function() {
    return 200;
  }
});
})(xhr, fakeresponse);

Object.defineProperty(xhr, "readyState", {
  get: function() {
    return 4;
  }
});

if(xhr.onreadystatechange!==undefined) xhr.onreadystatechange();