42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
(function () {
|
|
"use strict";
|
|
var NativeXHR = window.XMLHttpRequest;
|
|
|
|
function apiPath(url) {
|
|
var match = String(url || "").match(/\/index\/[^?#]*/);
|
|
return match ? match[0] : "";
|
|
}
|
|
|
|
function BridgeXHR() {
|
|
this._xhr = new NativeXHR();
|
|
this._isGameApi = false;
|
|
}
|
|
|
|
BridgeXHR.prototype.open = function (method, url) {
|
|
var path = apiPath(url);
|
|
this._isGameApi = path.indexOf("/index/") === 0;
|
|
if (this._isGameApi) {
|
|
var args = Array.prototype.slice.call(arguments);
|
|
args[0] = "POST";
|
|
args[1] = "/api/game.php?path=" + encodeURIComponent(path);
|
|
return this._xhr.open.apply(this._xhr, args);
|
|
}
|
|
return this._xhr.open.apply(this._xhr, arguments);
|
|
};
|
|
|
|
["send", "abort", "setRequestHeader", "getResponseHeader", "getAllResponseHeaders", "addEventListener", "removeEventListener", "overrideMimeType"].forEach(function (name) {
|
|
BridgeXHR.prototype[name] = function () {
|
|
return this._xhr[name].apply(this._xhr, arguments);
|
|
};
|
|
});
|
|
|
|
["readyState", "response", "responseText", "responseType", "responseURL", "responseXML", "status", "statusText", "timeout", "upload", "withCredentials", "onreadystatechange", "onload", "onerror", "ontimeout", "onabort", "onprogress", "onloadend", "onloadstart"].forEach(function (name) {
|
|
Object.defineProperty(BridgeXHR.prototype, name, {
|
|
get: function () { return this._xhr[name]; },
|
|
set: function (value) { this._xhr[name] = value; }
|
|
});
|
|
});
|
|
|
|
window.XMLHttpRequest = BridgeXHR;
|
|
})();
|