0

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.

Community
  • 1
  • 1
Edge
  • 2,456
  • 6
  • 32
  • 57
  • `oldReady = this.onreadystatechange; // IS NULL IN NEWER CHROMES` ... it's null on older chromes as well if the code using xhr doesn't set xhr.onreadystatechange (which is the old school of using xhr anyway) - the new way of using xhr is described [here](http://www.html5rocks.com/en/tutorials/file/xhr2/) in this 4.5 year old article - as long as you don't want to use `json` response type, it's compatible even with IE 10 (but no earlier) – Jaromanda X Dec 12 '15 at 12:16
  • The webpage JS is using onreadystatechange - it has a value. – Edge Dec 12 '15 at 12:26
  • is it assigning it before or after `.open`? – Jaromanda X Dec 12 '15 at 12:27
  • I'm unsure, as I believe it's set by jQuery. Normally I overwrite open and send via the XMLHttpRequest.prototype, but I can't do that for onreadystatechange. What are my options? – Edge Dec 12 '15 at 12:29
  • if you're using jQuery, there are jQuery methods to manipulate ajax data "in flight" as it were - look at the [jquery.ajaxsetup](https://api.jquery.com/jquery.ajaxsetup/) documentation – Jaromanda X Dec 12 '15 at 12:29
  • Just checked in current Chrome ... if `.onreadystatechange` is assigned before `.open` is called, the above works as expected, or, without errors anyway. Much code I've seen calls `.open` before assigning `.onreadystatechange` - so, that code is particularly useless in that case – Jaromanda X Dec 12 '15 at 12:36
  • Why didn't you use the second and **tested** method shown in that answer? The answer does state the first method has limitations, clearly you've hit one of those limitations – Jaromanda X Dec 12 '15 at 12:39
  • The second method they showed had some issues with my current code. Anyhow, I may just opt to overwrite the website content scripts code that does the processing of the AJAX response. Many thanks for your input :) – Edge Dec 12 '15 at 12:47

0 Answers0