@whitesev/utils 2.9.11 → 2.9.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -176
- package/dist/index.amd.js +159 -101
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +159 -101
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +159 -101
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +159 -101
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +159 -101
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +159 -101
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/Dictionary.d.ts +2 -0
- package/dist/types/src/Utils.d.ts +5 -2
- package/dist/types/src/types/Httpx.d.ts +1344 -1344
- package/dist/types/src/types/Log.d.ts +19 -19
- package/dist/types/src/types/Progress.d.ts +20 -20
- package/dist/types/src/types/React.d.ts +119 -119
- package/dist/types/src/types/TryCatch.d.ts +9 -9
- package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
- package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
- package/dist/types/src/types/Vue2.d.ts +166 -166
- package/dist/types/src/types/WindowApi.d.ts +14 -14
- package/dist/types/src/types/ajaxHooker.d.ts +155 -155
- package/dist/types/src/types/env.d.ts +7 -7
- package/dist/types/src/types/global.d.ts +31 -31
- package/package.json +26 -24
- package/src/ColorConversion.ts +118 -118
- package/src/CommonUtil.ts +301 -285
- package/src/DOMUtils.ts +251 -251
- package/src/Dictionary.ts +205 -199
- package/src/GBKEncoder.ts +108 -108
- package/src/Hooks.ts +73 -73
- package/src/Httpx.ts +1457 -1457
- package/src/LockFunction.ts +65 -62
- package/src/Log.ts +233 -233
- package/src/ModuleRaid.js +378 -360
- package/src/Progress.ts +108 -108
- package/src/TryCatch.ts +86 -86
- package/src/Utils.ts +3860 -3852
- package/src/UtilsCommon.ts +14 -14
- package/src/UtilsGMCookie.ts +273 -273
- package/src/UtilsGMMenu.ts +460 -460
- package/src/Vue.ts +233 -233
- package/src/WindowApi.ts +59 -59
- package/src/ajaxHooker/ajaxHooker.js +606 -538
- package/src/ajaxHooker/ajaxHooker1.2.4.js +440 -438
- package/src/indexedDB.ts +497 -497
- package/src/types/Httpx.d.ts +1344 -1344
- package/src/types/Log.d.ts +19 -19
- package/src/types/Progress.d.ts +20 -20
- package/src/types/React.d.ts +119 -119
- package/src/types/TryCatch.d.ts +9 -9
- package/src/types/UtilsGMCookie.d.ts +93 -93
- package/src/types/UtilsGMMenu.d.ts +77 -77
- package/src/types/Vue2.d.ts +166 -166
- package/src/types/WindowApi.d.ts +14 -14
- package/src/types/ajaxHooker.d.ts +155 -155
- package/src/types/env.d.ts +7 -7
- package/src/types/global.d.ts +31 -31
|
@@ -6,442 +6,444 @@
|
|
|
6
6
|
// ==/UserScript==
|
|
7
7
|
|
|
8
8
|
export const AjaxHooker1_2_4 = function () {
|
|
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
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
9
|
+
return (function () {
|
|
10
|
+
const win = window.unsafeWindow || document.defaultView || window;
|
|
11
|
+
const hookFns = [];
|
|
12
|
+
const realXhr = win.XMLHttpRequest;
|
|
13
|
+
const resProto = win.Response.prototype;
|
|
14
|
+
const toString = Object.prototype.toString;
|
|
15
|
+
const realFetch = win.fetch;
|
|
16
|
+
const xhrResponses = ["response", "responseText", "responseXML"];
|
|
17
|
+
const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
|
|
18
|
+
const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
|
|
19
|
+
let filter;
|
|
20
|
+
function emptyFn() {}
|
|
21
|
+
function errorFn(err) {
|
|
22
|
+
console.error(err);
|
|
23
|
+
}
|
|
24
|
+
function defineProp(obj, prop, getter, setter) {
|
|
25
|
+
Object.defineProperty(obj, prop, {
|
|
26
|
+
configurable: true,
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: getter,
|
|
29
|
+
set: setter,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function readonly(obj, prop, value = obj[prop]) {
|
|
33
|
+
defineProp(obj, prop, () => value, emptyFn);
|
|
34
|
+
}
|
|
35
|
+
function writable(obj, prop, value = obj[prop]) {
|
|
36
|
+
Object.defineProperty(obj, prop, {
|
|
37
|
+
configurable: true,
|
|
38
|
+
enumerable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: value,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function toFilterObj(obj) {
|
|
44
|
+
return {
|
|
45
|
+
type: obj.type,
|
|
46
|
+
url: obj.url,
|
|
47
|
+
method: obj.method && obj.method.toUpperCase(),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function shouldFilter(type, url, method) {
|
|
51
|
+
return (
|
|
52
|
+
filter &&
|
|
53
|
+
!filter.find(
|
|
54
|
+
(obj) =>
|
|
55
|
+
(!obj.type || obj.type === type) &&
|
|
56
|
+
(!obj.url ||
|
|
57
|
+
(toString.call(obj.url) === "[object String]"
|
|
58
|
+
? url.includes(obj.url)
|
|
59
|
+
: obj.url.test(url))) &&
|
|
60
|
+
(!obj.method || obj.method === method.toUpperCase()),
|
|
61
|
+
)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
function lookupGetter(obj, prop) {
|
|
65
|
+
let getter;
|
|
66
|
+
let proto = obj;
|
|
67
|
+
while (proto) {
|
|
68
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, prop);
|
|
69
|
+
getter = descriptor && descriptor.get;
|
|
70
|
+
if (getter) break;
|
|
71
|
+
proto = Object.getPrototypeOf(proto);
|
|
72
|
+
}
|
|
73
|
+
return getter ? getter.bind(obj) : emptyFn;
|
|
74
|
+
}
|
|
75
|
+
function waitForHookFns(request) {
|
|
76
|
+
return Promise.all(
|
|
77
|
+
hookFns.map((fn) =>
|
|
78
|
+
Promise.resolve(fn(request)).then(emptyFn, errorFn),
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
function waitForRequestKeys(request, requestClone) {
|
|
83
|
+
return Promise.all(
|
|
84
|
+
["url", "method", "abort", "headers", "data"].map((key) => {
|
|
85
|
+
return Promise.resolve(request[key]).then(
|
|
86
|
+
(val) => (request[key] = val),
|
|
87
|
+
() => (request[key] = requestClone[key]),
|
|
88
|
+
);
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
function fakeEventSIP() {
|
|
93
|
+
this.ajaxHooker_stopped = true;
|
|
94
|
+
}
|
|
95
|
+
function xhrDelegateEvent(e) {
|
|
96
|
+
const xhr = e.target;
|
|
97
|
+
e.stopImmediatePropagation = fakeEventSIP;
|
|
98
|
+
xhr.__ajaxHooker.hookedEvents[e.type].forEach(
|
|
99
|
+
(fn) => !e.ajaxHooker_stopped && fn.call(xhr, e),
|
|
100
|
+
);
|
|
101
|
+
const onEvent = xhr.__ajaxHooker.hookedEvents["on" + e.type];
|
|
102
|
+
typeof onEvent === "function" && onEvent.call(xhr, e);
|
|
103
|
+
}
|
|
104
|
+
function xhrReadyStateChange(e) {
|
|
105
|
+
if (e.target.readyState === 4) {
|
|
106
|
+
e.target.dispatchEvent(
|
|
107
|
+
new CustomEvent("ajaxHooker_responseReady", { detail: e }),
|
|
108
|
+
);
|
|
109
|
+
} else {
|
|
110
|
+
e.target.__ajaxHooker.delegateEvent(e);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function xhrLoadAndLoadend(e) {
|
|
114
|
+
e.target.__ajaxHooker.delegateEvent(e);
|
|
115
|
+
}
|
|
116
|
+
function fakeXhrOpen(method, url, ...args) {
|
|
117
|
+
const ah = this.__ajaxHooker;
|
|
118
|
+
ah.url = url.toString();
|
|
119
|
+
ah.method = method.toUpperCase();
|
|
120
|
+
ah.openArgs = args;
|
|
121
|
+
ah.headers = {};
|
|
122
|
+
return ah.originalMethods.open(method, url, ...args);
|
|
123
|
+
}
|
|
124
|
+
function fakeXhr() {
|
|
125
|
+
const xhr = new realXhr();
|
|
126
|
+
let ah = xhr.__ajaxHooker;
|
|
127
|
+
if (!ah) {
|
|
128
|
+
ah = xhr.__ajaxHooker = {
|
|
129
|
+
headers: {},
|
|
130
|
+
hookedEvents: {
|
|
131
|
+
readystatechange: new Set(),
|
|
132
|
+
load: new Set(),
|
|
133
|
+
loadend: new Set(),
|
|
134
|
+
},
|
|
135
|
+
delegateEvent: xhrDelegateEvent,
|
|
136
|
+
originalGetters: {},
|
|
137
|
+
originalMethods: {},
|
|
138
|
+
};
|
|
139
|
+
xhr.addEventListener("readystatechange", xhrReadyStateChange);
|
|
140
|
+
xhr.addEventListener("load", xhrLoadAndLoadend);
|
|
141
|
+
xhr.addEventListener("loadend", xhrLoadAndLoadend);
|
|
142
|
+
for (const key of xhrResponses) {
|
|
143
|
+
ah.originalGetters[key] = lookupGetter(xhr, key);
|
|
144
|
+
}
|
|
145
|
+
for (const method of [
|
|
146
|
+
"open",
|
|
147
|
+
"setRequestHeader",
|
|
148
|
+
"addEventListener",
|
|
149
|
+
"removeEventListener",
|
|
150
|
+
]) {
|
|
151
|
+
ah.originalMethods[method] = xhr[method].bind(xhr);
|
|
152
|
+
}
|
|
153
|
+
xhr.open = fakeXhrOpen;
|
|
154
|
+
xhr.setRequestHeader = (header, value) => {
|
|
155
|
+
ah.originalMethods.setRequestHeader(header, value);
|
|
156
|
+
if (xhr.readyState === 1) {
|
|
157
|
+
if (ah.headers[header]) {
|
|
158
|
+
ah.headers[header] += ", " + value;
|
|
159
|
+
} else {
|
|
160
|
+
ah.headers[header] = value;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
xhr.addEventListener = function (...args) {
|
|
165
|
+
if (xhrAsyncEvents.includes(args[0])) {
|
|
166
|
+
ah.hookedEvents[args[0]].add(args[1]);
|
|
167
|
+
} else {
|
|
168
|
+
ah.originalMethods.addEventListener(...args);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
xhr.removeEventListener = function (...args) {
|
|
172
|
+
if (xhrAsyncEvents.includes(args[0])) {
|
|
173
|
+
ah.hookedEvents[args[0]].delete(args[1]);
|
|
174
|
+
} else {
|
|
175
|
+
ah.originalMethods.removeEventListener(...args);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
xhrAsyncEvents.forEach((evt) => {
|
|
179
|
+
const onEvt = "on" + evt;
|
|
180
|
+
defineProp(
|
|
181
|
+
xhr,
|
|
182
|
+
onEvt,
|
|
183
|
+
() => {
|
|
184
|
+
return ah.hookedEvents[onEvt] || null;
|
|
185
|
+
},
|
|
186
|
+
(val) => {
|
|
187
|
+
ah.hookedEvents[onEvt] = typeof val === "function" ? val : null;
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const realSend = xhr.send.bind(xhr);
|
|
193
|
+
xhr.send = function (data) {
|
|
194
|
+
if (xhr.readyState !== 1) return realSend(data);
|
|
195
|
+
ah.delegateEvent = xhrDelegateEvent;
|
|
196
|
+
xhrResponses.forEach((prop) => {
|
|
197
|
+
delete xhr[prop]; // delete descriptor
|
|
198
|
+
});
|
|
199
|
+
if (shouldFilter("xhr", ah.url, ah.method)) {
|
|
200
|
+
xhr.addEventListener("ajaxHooker_responseReady", (e) => {
|
|
201
|
+
ah.delegateEvent(e.detail);
|
|
202
|
+
});
|
|
203
|
+
return realSend(data);
|
|
204
|
+
}
|
|
205
|
+
try {
|
|
206
|
+
const request = {
|
|
207
|
+
type: "xhr",
|
|
208
|
+
url: ah.url,
|
|
209
|
+
method: ah.method,
|
|
210
|
+
abort: false,
|
|
211
|
+
headers: ah.headers,
|
|
212
|
+
data: data,
|
|
213
|
+
response: null,
|
|
214
|
+
};
|
|
215
|
+
const requestClone = { ...request };
|
|
216
|
+
waitForHookFns(request).then(() => {
|
|
217
|
+
waitForRequestKeys(request, requestClone).then(() => {
|
|
218
|
+
if (request.abort) return;
|
|
219
|
+
ah.originalMethods.open(
|
|
220
|
+
request.method,
|
|
221
|
+
request.url,
|
|
222
|
+
...ah.openArgs,
|
|
223
|
+
);
|
|
224
|
+
for (const header in request.headers) {
|
|
225
|
+
ah.originalMethods.setRequestHeader(
|
|
226
|
+
header,
|
|
227
|
+
request.headers[header],
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
data = request.data;
|
|
231
|
+
xhr.addEventListener("ajaxHooker_responseReady", (e) => {
|
|
232
|
+
try {
|
|
233
|
+
if (typeof request.response === "function") {
|
|
234
|
+
const arg = {
|
|
235
|
+
finalUrl: xhr.responseURL,
|
|
236
|
+
status: xhr.status,
|
|
237
|
+
responseHeaders: {},
|
|
238
|
+
};
|
|
239
|
+
for (const line of xhr
|
|
240
|
+
.getAllResponseHeaders()
|
|
241
|
+
.trim()
|
|
242
|
+
.split(/[\r\n]+/)) {
|
|
243
|
+
const parts = line.split(/:\s*/);
|
|
244
|
+
if (parts.length === 2) {
|
|
245
|
+
const lheader = parts[0].toLowerCase();
|
|
246
|
+
if (arg.responseHeaders[lheader]) {
|
|
247
|
+
arg.responseHeaders[lheader] += ", " + parts[1];
|
|
248
|
+
} else {
|
|
249
|
+
arg.responseHeaders[lheader] = parts[1];
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
xhrResponses.forEach((prop) => {
|
|
254
|
+
defineProp(
|
|
255
|
+
arg,
|
|
256
|
+
prop,
|
|
257
|
+
() => {
|
|
258
|
+
return (arg[prop] = ah.originalGetters[prop]());
|
|
259
|
+
},
|
|
260
|
+
(val) => {
|
|
261
|
+
delete arg[prop];
|
|
262
|
+
arg[prop] = val;
|
|
263
|
+
},
|
|
264
|
+
);
|
|
265
|
+
defineProp(xhr, prop, () => {
|
|
266
|
+
const val = ah.originalGetters[prop]();
|
|
267
|
+
xhr.dispatchEvent(
|
|
268
|
+
new CustomEvent("ajaxHooker_readResponse", {
|
|
269
|
+
detail: { prop, val },
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
return val;
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
xhr.addEventListener("ajaxHooker_readResponse", (e) => {
|
|
276
|
+
arg[e.detail.prop] = e.detail.val;
|
|
277
|
+
});
|
|
278
|
+
const resPromise = Promise.resolve(
|
|
279
|
+
request.response(arg),
|
|
280
|
+
).then(() => {
|
|
281
|
+
const task = [];
|
|
282
|
+
xhrResponses.forEach((prop) => {
|
|
283
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
284
|
+
arg,
|
|
285
|
+
prop,
|
|
286
|
+
);
|
|
287
|
+
if (descriptor && "value" in descriptor) {
|
|
288
|
+
task.push(
|
|
289
|
+
Promise.resolve(descriptor.value).then((val) => {
|
|
290
|
+
arg[prop] = val;
|
|
291
|
+
defineProp(xhr, prop, () => {
|
|
292
|
+
xhr.dispatchEvent(
|
|
293
|
+
new CustomEvent("ajaxHooker_readResponse", {
|
|
294
|
+
detail: { prop, val },
|
|
295
|
+
}),
|
|
296
|
+
);
|
|
297
|
+
return val;
|
|
298
|
+
});
|
|
299
|
+
}, emptyFn),
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
return Promise.all(task);
|
|
304
|
+
}, errorFn);
|
|
305
|
+
const eventsClone = {};
|
|
306
|
+
xhrAsyncEvents.forEach((type) => {
|
|
307
|
+
eventsClone[type] = new Set([...ah.hookedEvents[type]]);
|
|
308
|
+
eventsClone["on" + type] = ah.hookedEvents["on" + type];
|
|
309
|
+
});
|
|
310
|
+
ah.delegateEvent = (event) =>
|
|
311
|
+
resPromise.then(() => {
|
|
312
|
+
event.stopImmediatePropagation = fakeEventSIP;
|
|
313
|
+
eventsClone[event.type].forEach(
|
|
314
|
+
(fn) =>
|
|
315
|
+
!event.ajaxHooker_stopped && fn.call(xhr, event),
|
|
316
|
+
);
|
|
317
|
+
const onEvent = eventsClone["on" + event.type];
|
|
318
|
+
typeof onEvent === "function" &&
|
|
319
|
+
onEvent.call(xhr, event);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
} catch (err) {
|
|
323
|
+
console.error(err);
|
|
324
|
+
}
|
|
325
|
+
ah.delegateEvent(e.detail);
|
|
326
|
+
});
|
|
327
|
+
realSend(data);
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
} catch (err) {
|
|
331
|
+
console.error(err);
|
|
332
|
+
realSend(data);
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
return xhr;
|
|
336
|
+
}
|
|
337
|
+
function hookFetchResponse(response, arg, callback) {
|
|
338
|
+
fetchResponses.forEach((prop) => {
|
|
339
|
+
response[prop] = () =>
|
|
340
|
+
new Promise((resolve, reject) => {
|
|
341
|
+
resProto[prop].call(response).then((res) => {
|
|
342
|
+
if (prop in arg) {
|
|
343
|
+
resolve(arg[prop]);
|
|
344
|
+
} else {
|
|
345
|
+
try {
|
|
346
|
+
arg[prop] = res;
|
|
347
|
+
Promise.resolve(callback(arg)).then(() => {
|
|
348
|
+
if (prop in arg) {
|
|
349
|
+
Promise.resolve(arg[prop]).then(
|
|
350
|
+
(val) => resolve((arg[prop] = val)),
|
|
351
|
+
() => resolve(res),
|
|
352
|
+
);
|
|
353
|
+
} else {
|
|
354
|
+
resolve(res);
|
|
355
|
+
}
|
|
356
|
+
}, errorFn);
|
|
357
|
+
} catch (err) {
|
|
358
|
+
console.error(err);
|
|
359
|
+
resolve(res);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}, reject);
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
function fakeFetch(url, init) {
|
|
367
|
+
if (url && typeof url.toString === "function") {
|
|
368
|
+
url = url.toString();
|
|
369
|
+
init = init || {};
|
|
370
|
+
init.method = init.method || "GET";
|
|
371
|
+
init.headers = init.headers || {};
|
|
372
|
+
if (shouldFilter("fetch", url, init.method))
|
|
373
|
+
return realFetch.call(win, url, init);
|
|
374
|
+
const request = {
|
|
375
|
+
type: "fetch",
|
|
376
|
+
url: url,
|
|
377
|
+
method: init.method.toUpperCase(),
|
|
378
|
+
abort: false,
|
|
379
|
+
headers: {},
|
|
380
|
+
data: init.body,
|
|
381
|
+
response: null,
|
|
382
|
+
};
|
|
383
|
+
if (toString.call(init.headers) === "[object Headers]") {
|
|
384
|
+
for (const [key, val] of init.headers) {
|
|
385
|
+
request.headers[key] = val;
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
request.headers = { ...init.headers };
|
|
389
|
+
}
|
|
390
|
+
const requestClone = { ...request };
|
|
391
|
+
return new Promise((resolve, reject) => {
|
|
392
|
+
try {
|
|
393
|
+
waitForHookFns(request).then(() => {
|
|
394
|
+
waitForRequestKeys(request, requestClone).then(() => {
|
|
395
|
+
if (request.abort) return reject("aborted");
|
|
396
|
+
url = request.url;
|
|
397
|
+
init.method = request.method;
|
|
398
|
+
init.headers = request.headers;
|
|
399
|
+
init.body = request.data;
|
|
400
|
+
realFetch.call(win, url, init).then((response) => {
|
|
401
|
+
if (typeof request.response === "function") {
|
|
402
|
+
const arg = {
|
|
403
|
+
finalUrl: response.url,
|
|
404
|
+
status: response.status,
|
|
405
|
+
responseHeaders: {},
|
|
406
|
+
};
|
|
407
|
+
for (const [key, val] of response.headers) {
|
|
408
|
+
arg.responseHeaders[key] = val;
|
|
409
|
+
}
|
|
410
|
+
hookFetchResponse(response, arg, request.response);
|
|
411
|
+
response.clone = () => {
|
|
412
|
+
const resClone = resProto.clone.call(response);
|
|
413
|
+
hookFetchResponse(resClone, arg, request.response);
|
|
414
|
+
return resClone;
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
resolve(response);
|
|
418
|
+
}, reject);
|
|
419
|
+
});
|
|
420
|
+
});
|
|
421
|
+
} catch (err) {
|
|
422
|
+
console.error(err);
|
|
423
|
+
return realFetch.call(win, url, init);
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
} else {
|
|
427
|
+
return realFetch.call(win, url, init);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
win.XMLHttpRequest = fakeXhr;
|
|
431
|
+
Object.keys(realXhr).forEach((key) => (fakeXhr[key] = realXhr[key]));
|
|
432
|
+
fakeXhr.prototype = realXhr.prototype;
|
|
433
|
+
win.fetch = fakeFetch;
|
|
434
|
+
return {
|
|
435
|
+
hook: (fn) => hookFns.push(fn),
|
|
436
|
+
filter: (arr) => {
|
|
437
|
+
filter = Array.isArray(arr) && arr.map(toFilterObj);
|
|
438
|
+
},
|
|
439
|
+
protect: () => {
|
|
440
|
+
readonly(win, "XMLHttpRequest", fakeXhr);
|
|
441
|
+
readonly(win, "fetch", fakeFetch);
|
|
442
|
+
},
|
|
443
|
+
unhook: () => {
|
|
444
|
+
writable(win, "XMLHttpRequest", realXhr);
|
|
445
|
+
writable(win, "fetch", realFetch);
|
|
446
|
+
},
|
|
447
|
+
};
|
|
448
|
+
})();
|
|
447
449
|
};
|