forked from damoclark/gmxhr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (117 loc) · 3.4 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* xmlHttpRequest API wrapper for GM_xmlhttpRequest
*
* This module allows you to provide a drop-in replacement for xmlHttpRequest
* using instead, the Greasemonkey (or Tampermonkey) privileged GM_xmlhttpRequest
* function, which is available under the Greasemonkey or Tampermonkey Web
* Extensions for Firefox and Chromium-based browsers.
*
* Can be used to allow jQuery to perform ajax calls using GM_xmlhttpRequest
*
* e.g.
*
* $.ajax({
* url: '/p/',
* xhr: function(){return new gmxhr();},
* type: 'POST',
* success: function(val){
* ....
* }
* });
*
* Contributions:
* (c) 2016,2017 Damien Clark (https://gist.github.com/damoclark/f01b957797b7dd2c33d6)
* (c) 2011 Martin Monperrus (https://gist.github.com/monperrus/999065)
*
* Original version:
* (c) 2010 Ryan Greenberg (http://ryangreenberg.com/archives/2010/03/greasemonkey_jquery.php)
*
* Licence: GPL-3.0+
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* xmlHttpRequest API wrapper for GM_xmlhttpRequest
*
* @returns {gmxhr} An instance with a compatible API to xmlHttpRequest
*/
function gmxhr() {
this.type = null;
this.url = null;
this.async = null;
this.username = null;
this.password = null;
this.status = null;
this.headers = {};
this.readyState = null;
}
gmxhr.prototype.abort = function() {
this.readyState = 0;
};
gmxhr.prototype.getAllResponseHeaders = function(name) {
if (this.readyState!=4) return '';
return this.responseHeaders;
};
gmxhr.prototype.getResponseHeader = function(header) {
var value = null;
if (this.responseHeaders) {
var regex = new RegExp('^'+header+": (.*)$","igm");
var match = regex.exec(this.responseHeaders);
var result = [];
while (match != null) {
result.push(match[1]);
match = regex.exec(this.responseHeaders);
}
if (result.length>0){
value=result.join(", ");
}
}
return value;
};
gmxhr.prototype.open = function(type, url, async, username, password) {
this.type = type ? type : null;
this.url = url ? url : null;
this.async = async ? async : null;
this.username = username ? username : null;
this.password = password ? password : null;
this.readyState = 1;
};
gmxhr.prototype.setRequestHeader = function(name, value) {
this.headers[name] = value;
};
gmxhr.prototype.send = function(data) {
this.data = data;
var that = this;
// http://wiki.greasespot.net/GM_xmlhttpRequest
GM_xmlhttpRequest({
method: this.type,
url: this.url,
headers: this.headers,
data: this.data,
onload: function(rsp) {
// Populate wrapper object with returned data
// including the Greasemonkey specific "responseHeaders"
for (var k in rsp) {
that[k] = rsp[k];
}
// now we call onreadystatechange
that.onreadystatechange();
},
onerror: function(rsp) {
for (var k in rsp) {
that[k] = rsp[k];
}
// now we call onreadystatechange
that.onreadystatechange();
}
});
};
module.exports = gmxhr ;