-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsl.ajax.js
305 lines (259 loc) · 10.2 KB
/
jsl.ajax.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// ==UserScript==
// @name JSL - AJAX plugin
// @namespace http://userscripts.org/users/23652
// @description An AJAX plugin for JSL
// @include *
// @version 1.1.0
//@require https://raw.github.com/joesimmons/jsl/master/versions/jsl-1.3.1.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
/* CHANGELOG
1.1.0 (3/28/2014)
- added JSL.ajaxClear() to clear all current and pending requests
1.0.21 (10/6/2013)
- fixed bug with .clear()
1.0.2 (10/6/2013)
- added a new option: async
false (default) ==> synchronous, but non-freezing requests (sequential)
waits for previous request to finish before starting a new one
true ==> sends all requests immediately when they are added to the queue
- fixed delay issue.
the next request would get run on the 'onprogress' & 'onreadystatechange' events
instead of when they actually load fully
- added a .clear() method
it may be called on any ajax instance like JSL.ajax(...).clear()
it can even simply be called as JSL.ajax().clear()
it will clear ALL requests at the moment
1.0.1 (10/3/2013)
- fixed small bug with passing a url array
- fixed bug not allowing HEAD requests to be recognized
1.0.0 (10/1/2013)
- created
*/
(function (undefined) {
'use strict'; // use strict mode in ECMAScript-5
var queue = [], // the request queue
blank = function () {}, // blank function to use as default callback
xhrInProgress = false, // boolean to know if we should load the next request
xhrCleared = false; // boolean to know if the xhr has been cleared and if
// we should execute any of the callbacks
var core = {
// object
'hasOwnProperty' : Object.prototype.hasOwnProperty
};
function copyObject(o) {
var key, value, newO = {};
for (key in o) {
value = o[key];
if (core.hasOwnProperty.call(o, key) && value != null) {
newO[key] = value;
}
}
return newO;
}
function toDataString(o) {
var key, value, dataString = '';
for (key in o) {
value = o[key];
if (core.hasOwnProperty.call(o, key) && value != null) {
dataString += key + '=' + encodeURIComponent(value) + '&';
}
}
return dataString.slice(0, -1);
}
function xhr() {
var req = queue[0], // get the object which is first in the queue
xhrObj = {}, key;
function handleEvents(type, resp, event) {
var event = event || {}, newResp, context;
req.delay = req.delay > 15 ? req.delay : 15; // don't want to mess up callbacks
if (xhrCleared === true) {
return;
}
if (req[type] !== blank) {
// define a new response object to give to the user
newResp = {
lengthComputable : resp.lengthComputable || event.lengthComputable || null,
loaded : resp.loaded || event.loaded || null,
readyState : resp.readyState,
responseHeaders : resp.responseHeaders ||
( typeof resp.getAllResponseHeaders === 'function' ? resp.getAllResponseHeaders() : null) || '',
responseText : resp.responseText,
status : resp.status,
statusText : resp.statusText,
total : resp.total || event.total || null,
url : resp.finalUrl || req.url,
};
// allow new requests to be run if our request is done
if (type === 'onerror' || type === 'onload') {
xhrInProgress = false;
// run the next in queue, if any
window.setTimeout(xhr, req.delay);
}
// run the callback
context = req.context || newResp;
req[type].call(context, newResp);
}
}
if ( req && (xhrInProgress === false || req.async === true) && queue.length > 0) {
// make it so no other requests get run while we
// run this one, if async isn't enabled
xhrInProgress = true;
// remove the first item in the queue if it is going to be run
queue.shift();
if (typeof GM_xmlhttpRequest === 'function') {
if (req.method.toUpperCase() === 'GET' && req.data !== '') {
req.url += '?' + req.data;
req.data = '';
}
GM_xmlhttpRequest({
'data' : req.data,
'headers' : req.headers,
'method' : req.method,
'onerror' : function (resp) {
handleEvents('onerror', resp);
},
'onload' : function (resp) {
handleEvents('onload', resp);
},
'onreadystatechange' : function (resp) {
handleEvents('onreadystatechange', resp);
},
'onprogress' : function (resp) {
handleEvents('onprogress', resp);
},
'url' : req.url,
});
} else if (typeof XMLHttpRequest === 'function' || typeof ActiveXObject === 'function') {
xhrObj = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
// set events
xhrObj.onload = function (resp) {
handleEvents('onload', xhrObj);
};
xhrObj.onerror = function (resp) {
handleEvents('onerror', xhrObj);
};
xhrObj.onprogress = function (resp) {
handleEvents('onprogress', xhrObj, resp);
};
if (req.mimeType !== '') {
xhrObj.overrideMimeType(req.mimeType);
}
// add headers
for (key in req.headers) {
xhrObj.setRequestHeader( key, req.headers[key] );
}
xhrObj.open(req.method, req.url, true);
xhrObj.send( (req.data || null) );
}
}
}
function init(url, settings) {
var urls = [],
realSettings = { // defaults
async : false,
data : '',
headers : {},
method : 'GET',
mimeType : '',
onload : blank,
onerror : blank,
onprogress : blank,
onreadystatechange : blank,
delay : 0
},
key, value;
if (typeof url === 'string') {
urls.push(url);
} else if (JSL.typeOf(url) === 'array') {
urls = urls.concat(url);
}
if (JSL.typeOf(settings) === 'object') {
for (key in settings) {
value = settings[key];
switch (key) {
case 'async': {
if (typeof value === 'boolean') {
realSettings[key] = value;
}
break;
}
case 'context': {
if (value != null) {
realSettings[key] = value;
}
break;
}
case 'data': {
if (typeof value === 'string') {
realSettings[key] = value;
} else if (JSL.typeOf(value) === 'object') {
realSettings[key] = toDataString(value);
}
break;
}
case 'delay': {
if (typeof value === 'number' && value > 0) {
realSettings[key] = value;
}
break;
}
case 'headers': {
if (JSL.typeOf(value) === 'object') {
realSettings[key] = toDataString(value);
}
break;
}
case 'method': {
if ( typeof value === 'string' && /get|post|head/i.test(value) ) {
realSettings[key] = value.toUpperCase();
}
break;
}
case 'mimeType': {
if (typeof value === 'string') {
realSettings[key] = value;
}
break;
}
case 'onload': case 'onerror': case 'onreadystatechange': case 'onprogress': {
if (typeof value === 'function') {
realSettings[key] = value;
}
break;
}
}
}
}
// add an object to the queue for each url
if (urls.length > 0) {
JSL.each(urls, function (url) {
var newO = copyObject(realSettings);
newO.url = url;
queue.push(newO);
});
// enable ajax if it was cleared earlier
xhrCleared = false;
// run the xhr function
// it will determine whether or not a request needs to be sent
xhr();
}
}
init.prototype = {
constructor: init,
clear : function() {
queue.length = 0;
xhrInProgress = false;
xhrCleared = true;
},
get length() {
return queue.length;
}
};
JSL.extend({
ajax : function (url, settings) {
return new init(url, settings);
},
ajaxClear : init.prototype.clear
});
}());