forked from ShiftSpace/gmrequest
/
GMRequest.js
47 lines (43 loc) · 1.35 KB
/
GMRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
if(typeof GM_log != 'undefined')
{
Browser.Request = function() {
return $try(function() {
return new GM.Request();
}, function() {
return new XMLHttpRequest();
}, function() {
return new ActiveXObject('MSXML2.XMLHTTP');
});
};
var GM = {};
GM.Request = new Class({
name: "GM.Request",
initialize: function(force) {
if(!GM_log && !force) throw Error();
this.headers = {};
},
__onreadystatechange__: function(responseDetails) {
this.status = responseDetails.status;
this.statusText = responseDetails.statusText;
this.responseHeaders = responseDetails.responseHeaders;
this.responseText = responseDetails.responseText;
this.readyState = responseDetails.readyState;
if(this.onreadystatechange && $type(this.onreadystatechange) == 'function') this.onreadystatechange();
},
open: function(method, url) {
this.method = method;
this.url = url;
},
setRequestHeader: function(key, value) { this.headers[key] = value; },
getResponseHeader: function(key) { return this.responseHeaders[key]; },
send: function(data) {
GM_xmlhttpRequest({
method: this.method,
url: this.url,
headers: this.headers,
data: data,
onreadystatechange: this.__onreadystatechange__.bind(this)
});
}
});
};