The code from this answer no longer works in Chrome, as the oldReady
variable is null, and therefore I can assume onreadystatechange
is unacceptable in code.
In particular, the code I have found not to work anymore is as follows:
(function() {
var open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
var oldReady;
if (async) {
oldReady = this.onreadystatechange; // IS NULL IN NEWER CHROMES
// override onReadyStateChange
this.onreadystatechange = function() {
if (this.readyState == 4) {
// this.responseText is the ajax result
// create a dummay ajax object so we can modify responseText
var self = this;
var dummy = {};
["statusText", "status", "readyState", "responseType"].forEach(function(item) {
dummy[item] = self[item];
});
dummy.responseText = '{"msg": "Hello"}';
return oldReady.call(dummy);
} else {
// call original onreadystatechange handler
return oldReady.apply(this, arguments);
}
}
}
// call original open method
return open.apply(this, arguments);
}
})();
The above code is supposed to overwrite the responseText returned bt an XMLHttpRequest object, and therefore is useful in userscripts and the like. I'm still in need of a working solution.