Skip to content

Instantly share code, notes, and snippets.

@w35l3y
Last active August 29, 2015 14:22
Show Gist options
  • Save w35l3y/f824897032ae38af9595 to your computer and use it in GitHub Desktop.
Save w35l3y/f824897032ae38af9595 to your computer and use it in GitHub Desktop.
XMLHttpRequest to GM_xmlhttpRequest
// Made from scratch based on https://github.com/ShiftSpace/gmrequest/blob/master/GMRequest.js
XMLHttpRequest = function () {
var _params = {
context : this,
headers : {},
onreadystatechange : function (detail) {
this.status = detail.status;
this.statusText = detail.statusText;
this.responseHeaders = detail.responseHeaders;
this.responseText = detail.responseText;
this.readyState = detail.readyState;
this.onreadystatechange && this.onreadystatechange();
}.bind(this)
},
_exec,
_send = function (data, binary) {
var t = {
data : data,
binary : binary,
};
for (var key in _params) {
t[key] = _params[key];
}
for (var key in this) {
if (!(key in t)) {
if (this[key] instanceof Function) {
t[key] = function (xhr) {
this[key].call(this, xhr);
}.bind(this);
} else {
t[key] = this[key];
}
}
}
_exec = GM_xmlhttpRequest(t);
}.bind(this);
this.setRequestHeader = function (key, value) {
_params.headers[key] = value;
};
this.getResponseHeader = function (key) {
return this.responseHeaders[key];
};
this.getAllResponseHeaders = function () {
return this.responseHeaders;
};
this.overrideMimeType = function (value) {
_params.overrideMimeType = value;
};
this.open = function (_method, _url, _async, _user, _passw) {
this.abort();
_params.method = _method.toUpperCase();
_params.url = _url;
_params.synchronous = !_async;
_params.user = _user;
_params.password = _passw;
};
this.abort = function () {
_exec && _exec.abort();
}
this.send = function (data) {
_send(data, false);
};
this.sendAsBinary = function (data) {
_send(data, true);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment