2

I'm working on a game engine which can work either under nw.js or under a browser. In a browser, XMLHttpRequest returns status = 200 for a successful request. However, in nw.js it returns 0 (because there's no timeout when you load a local file, duh).

My game engine uses external libraries which are, apart from minified, unable to be edited by myself (or any other user of my game engine, for that matter) due to obvious reasons. My idea is to override "onreadystatechange" and, if I receive a status = 0, change it with status = 200 if running under nw.js.

I've tried this, but status keeps being zero:

(function(xhr) {
    var send = xhr.send;
    xhr.send = function(data) {
        var rsc = this.onreadystatechange;
        if (rsc) {
            this.onreadystatechange = function() {
                if(this.readyState == 4)
                    if(this.status == 0)
                        this.status = 200;
                return rsc.apply(this, arguments);
            };
        }
        return send.apply(this, arguments);
    };
})(XMLHttpRequest.prototype);

This is taken from Rob W's excellent answer found here. I've also read in the MDN that the status variable is read-only. Is there any way to override that? Thanks in advance :).

Community
  • 1
  • 1
DARKGuy
  • 843
  • 1
  • 12
  • 34

1 Answers1

0

I had very similar issue, I had to change status code to 200 when actual response returned any error (any code bigger than over 300). Fortunately great answer for a similar question gave me an idea how to change status code.

(function() {
    var oldXMLHttpRequest = XMLHttpRequest;
    // define constructor for my proxy object
    XMLHttpRequest = function() {
        var actual = new oldXMLHttpRequest();
        var self = this;
        this.onreadystatechange = null;
        // this is the actual handler on the real XMLHttpRequest object
        actual.onreadystatechange = function() {
            self.status  = this.status;
            if (this.readyState == 4 ) {
                if (this.status >= 300) {
                    self.status = 200;
                } 
            }
            if (self.onreadystatechange) {
                return self.onreadystatechange();
            }
        };
        // add all proxy getters, except 'status'
        [ "statusText", "responseType", "response","responseText",
         "readyState", "responseXML", "upload"].forEach(function(item) {
            Object.defineProperty(self, item, {
                get: function() {return actual[item];}
            });
        });

        // add all proxy getters/setters
        ["ontimeout, timeout", "withCredentials", "onload", "onerror", "onprogress"].forEach(function(item) {
            Object.defineProperty(self, item, {
                get: function() {return actual[item];},
                set: function(val) {actual[item] = val;}
            });
        });

        // add all pure proxy pass-through methods
        ["addEventListener", "send", "open", "abort", "getAllResponseHeaders",
         "getResponseHeader", "overrideMimeType", "setRequestHeader"].forEach(function(item) {
            Object.defineProperty(self, item, {
                value: function() {return actual[item].apply(actual, arguments);}
            });
        });
    }
})();                
Community
  • 1
  • 1
Mikhail Chibel
  • 1,865
  • 1
  • 22
  • 34