@whitesev/utils 2.9.12 → 2.10.0
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 +206 -117
- 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 +206 -117
- 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 +206 -117
- 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 +206 -117
- 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 +206 -117
- 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 +206 -117
- 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 +9 -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 -301
- 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 +3907 -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
|
@@ -8,542 +8,610 @@
|
|
|
8
8
|
// ==/UserScript==
|
|
9
9
|
|
|
10
10
|
export const ajaxHooker = function () {
|
|
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
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
11
|
+
"use strict";
|
|
12
|
+
const version = "1.4.8";
|
|
13
|
+
const hookInst = {
|
|
14
|
+
hookFns: [],
|
|
15
|
+
filters: [],
|
|
16
|
+
};
|
|
17
|
+
const win = window.unsafeWindow || document.defaultView || window;
|
|
18
|
+
let winAh = win.__ajaxHooker;
|
|
19
|
+
const resProto = win.Response.prototype;
|
|
20
|
+
const xhrResponses = ["response", "responseText", "responseXML"];
|
|
21
|
+
const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
|
|
22
|
+
const xhrExtraProps = ["responseType", "timeout", "withCredentials"];
|
|
23
|
+
const fetchExtraProps = [
|
|
24
|
+
"cache",
|
|
25
|
+
"credentials",
|
|
26
|
+
"integrity",
|
|
27
|
+
"keepalive",
|
|
28
|
+
"mode",
|
|
29
|
+
"priority",
|
|
30
|
+
"redirect",
|
|
31
|
+
"referrer",
|
|
32
|
+
"referrerPolicy",
|
|
33
|
+
"signal",
|
|
34
|
+
];
|
|
35
|
+
const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
|
|
36
|
+
const getType = {}.toString.call.bind({}.toString);
|
|
37
|
+
const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
|
|
38
|
+
const emptyFn = () => {};
|
|
39
|
+
const errorFn = (e) => console.error(e);
|
|
40
|
+
function isThenable(obj) {
|
|
41
|
+
return (
|
|
42
|
+
obj &&
|
|
43
|
+
["object", "function"].includes(typeof obj) &&
|
|
44
|
+
typeof obj.then === "function"
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
function catchError(fn, ...args) {
|
|
48
|
+
try {
|
|
49
|
+
const result = fn(...args);
|
|
50
|
+
if (isThenable(result)) return result.then(null, errorFn);
|
|
51
|
+
return result;
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error(err);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function defineProp(obj, prop, getter, setter) {
|
|
57
|
+
Object.defineProperty(obj, prop, {
|
|
58
|
+
configurable: true,
|
|
59
|
+
enumerable: true,
|
|
60
|
+
get: getter,
|
|
61
|
+
set: setter,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function readonly(obj, prop, value = obj[prop]) {
|
|
65
|
+
defineProp(obj, prop, () => value, emptyFn);
|
|
66
|
+
}
|
|
67
|
+
function writable(obj, prop, value = obj[prop]) {
|
|
68
|
+
Object.defineProperty(obj, prop, {
|
|
69
|
+
configurable: true,
|
|
70
|
+
enumerable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: value,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function parseHeaders(obj) {
|
|
76
|
+
const headers = {};
|
|
77
|
+
switch (getType(obj)) {
|
|
78
|
+
case "[object String]":
|
|
79
|
+
for (const line of obj.trim().split(/[\r\n]+/)) {
|
|
80
|
+
const [header, value] = line.split(/(?<=^[^:]+)\s*:\s*/);
|
|
81
|
+
if (!value) continue;
|
|
82
|
+
const lheader = header.toLowerCase();
|
|
83
|
+
headers[lheader] =
|
|
84
|
+
lheader in headers ? `${headers[lheader]}, ${value}` : value;
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
case "[object Headers]":
|
|
88
|
+
for (const [key, val] of obj) {
|
|
89
|
+
headers[key] = val;
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
case "[object Object]":
|
|
93
|
+
return { ...obj };
|
|
94
|
+
}
|
|
95
|
+
return headers;
|
|
96
|
+
}
|
|
97
|
+
function stopImmediatePropagation() {
|
|
98
|
+
this.ajaxHooker_isStopped = true;
|
|
99
|
+
}
|
|
100
|
+
class SyncThenable {
|
|
101
|
+
then(fn) {
|
|
102
|
+
fn && fn();
|
|
103
|
+
return new SyncThenable();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
class AHRequest {
|
|
107
|
+
constructor(request) {
|
|
108
|
+
this.request = request;
|
|
109
|
+
this.requestClone = { ...this.request };
|
|
110
|
+
}
|
|
111
|
+
_recoverRequestKey(key) {
|
|
112
|
+
if (key in this.requestClone) this.request[key] = this.requestClone[key];
|
|
113
|
+
else delete this.request[key];
|
|
114
|
+
}
|
|
115
|
+
shouldFilter(filters) {
|
|
116
|
+
const { type, url, method, async } = this.request;
|
|
117
|
+
return (
|
|
118
|
+
filters.length &&
|
|
119
|
+
!filters.find((obj) => {
|
|
120
|
+
switch (true) {
|
|
121
|
+
case obj.type && obj.type !== type:
|
|
122
|
+
case getType(obj.url) === "[object String]" &&
|
|
123
|
+
!url.includes(obj.url):
|
|
124
|
+
case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
|
|
125
|
+
case obj.method &&
|
|
126
|
+
obj.method.toUpperCase() !== method.toUpperCase():
|
|
127
|
+
case "async" in obj && obj.async !== async:
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
})
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
waitForRequestKeys() {
|
|
135
|
+
if (!this.request.async) {
|
|
136
|
+
win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
|
|
137
|
+
if (this.shouldFilter(filters)) return;
|
|
138
|
+
hookFns.forEach((fn) => {
|
|
139
|
+
if (getType(fn) === "[object Function]")
|
|
140
|
+
catchError(fn, this.request);
|
|
141
|
+
});
|
|
142
|
+
for (const key in this.request) {
|
|
143
|
+
if (isThenable(this.request[key])) this._recoverRequestKey(key);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
return new SyncThenable();
|
|
147
|
+
}
|
|
148
|
+
const promises = [];
|
|
149
|
+
const ignoreKeys = new Set(["type", "async", "response"]);
|
|
150
|
+
win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
|
|
151
|
+
if (this.shouldFilter(filters)) return;
|
|
152
|
+
promises.push(
|
|
153
|
+
Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
|
|
154
|
+
() => {
|
|
155
|
+
const requestKeys = [];
|
|
156
|
+
for (const key in this.request)
|
|
157
|
+
!ignoreKeys.has(key) && requestKeys.push(key);
|
|
158
|
+
return Promise.all(
|
|
159
|
+
requestKeys.map((key) =>
|
|
160
|
+
Promise.resolve(this.request[key]).then(
|
|
161
|
+
(val) => (this.request[key] = val),
|
|
162
|
+
() => this._recoverRequestKey(key),
|
|
163
|
+
),
|
|
164
|
+
),
|
|
165
|
+
);
|
|
166
|
+
},
|
|
167
|
+
),
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
return Promise.all(promises);
|
|
171
|
+
}
|
|
172
|
+
waitForResponseKeys(response) {
|
|
173
|
+
const responseKeys =
|
|
174
|
+
this.request.type === "xhr" ? xhrResponses : fetchResponses;
|
|
175
|
+
if (!this.request.async) {
|
|
176
|
+
if (getType(this.request.response) === "[object Function]") {
|
|
177
|
+
catchError(this.request.response, response);
|
|
178
|
+
responseKeys.forEach((key) => {
|
|
179
|
+
if (
|
|
180
|
+
"get" in getDescriptor(response, key) ||
|
|
181
|
+
isThenable(response[key])
|
|
182
|
+
) {
|
|
183
|
+
delete response[key];
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return new SyncThenable();
|
|
188
|
+
}
|
|
189
|
+
return Promise.resolve(catchError(this.request.response, response)).then(
|
|
190
|
+
() =>
|
|
191
|
+
Promise.all(
|
|
192
|
+
responseKeys.map((key) => {
|
|
193
|
+
const descriptor = getDescriptor(response, key);
|
|
194
|
+
if (descriptor && "value" in descriptor) {
|
|
195
|
+
return Promise.resolve(descriptor.value).then(
|
|
196
|
+
(val) => (response[key] = val),
|
|
197
|
+
() => delete response[key],
|
|
198
|
+
);
|
|
199
|
+
} else {
|
|
200
|
+
delete response[key];
|
|
201
|
+
}
|
|
202
|
+
}),
|
|
203
|
+
),
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const proxyHandler = {
|
|
208
|
+
get(target, prop) {
|
|
209
|
+
const descriptor = getDescriptor(target, prop);
|
|
210
|
+
if (
|
|
211
|
+
descriptor &&
|
|
212
|
+
!descriptor.configurable &&
|
|
213
|
+
!descriptor.writable &&
|
|
214
|
+
!descriptor.get
|
|
215
|
+
)
|
|
216
|
+
return target[prop];
|
|
217
|
+
const ah = target.__ajaxHooker;
|
|
218
|
+
if (ah && ah.proxyProps) {
|
|
219
|
+
if (prop in ah.proxyProps) {
|
|
220
|
+
const pDescriptor = ah.proxyProps[prop];
|
|
221
|
+
if ("get" in pDescriptor) return pDescriptor.get();
|
|
222
|
+
if (typeof pDescriptor.value === "function")
|
|
223
|
+
return pDescriptor.value.bind(ah);
|
|
224
|
+
return pDescriptor.value;
|
|
225
|
+
}
|
|
226
|
+
if (typeof target[prop] === "function")
|
|
227
|
+
return target[prop].bind(target);
|
|
228
|
+
}
|
|
229
|
+
return target[prop];
|
|
230
|
+
},
|
|
231
|
+
set(target, prop, value) {
|
|
232
|
+
const descriptor = getDescriptor(target, prop);
|
|
233
|
+
if (
|
|
234
|
+
descriptor &&
|
|
235
|
+
!descriptor.configurable &&
|
|
236
|
+
!descriptor.writable &&
|
|
237
|
+
!descriptor.set
|
|
238
|
+
)
|
|
239
|
+
return true;
|
|
240
|
+
const ah = target.__ajaxHooker;
|
|
241
|
+
if (ah && ah.proxyProps && prop in ah.proxyProps) {
|
|
242
|
+
const pDescriptor = ah.proxyProps[prop];
|
|
243
|
+
pDescriptor.set ? pDescriptor.set(value) : (pDescriptor.value = value);
|
|
244
|
+
} else {
|
|
245
|
+
target[prop] = value;
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
class XhrHooker {
|
|
251
|
+
constructor(xhr) {
|
|
252
|
+
const ah = this;
|
|
253
|
+
Object.assign(ah, {
|
|
254
|
+
originalXhr: xhr,
|
|
255
|
+
proxyXhr: new Proxy(xhr, proxyHandler),
|
|
256
|
+
resThenable: new SyncThenable(),
|
|
257
|
+
proxyProps: {},
|
|
258
|
+
proxyEvents: {},
|
|
259
|
+
});
|
|
260
|
+
xhr.addEventListener("readystatechange", (e) => {
|
|
261
|
+
if (
|
|
262
|
+
ah.proxyXhr.readyState === 4 &&
|
|
263
|
+
ah.request &&
|
|
264
|
+
typeof ah.request.response === "function"
|
|
265
|
+
) {
|
|
266
|
+
const response = {
|
|
267
|
+
finalUrl: ah.proxyXhr.responseURL,
|
|
268
|
+
status: ah.proxyXhr.status,
|
|
269
|
+
responseHeaders: parseHeaders(ah.proxyXhr.getAllResponseHeaders()),
|
|
270
|
+
};
|
|
271
|
+
const tempValues = {};
|
|
272
|
+
for (const key of xhrResponses) {
|
|
273
|
+
try {
|
|
274
|
+
tempValues[key] = ah.originalXhr[key];
|
|
275
|
+
} catch (err) {}
|
|
276
|
+
defineProp(
|
|
277
|
+
response,
|
|
278
|
+
key,
|
|
279
|
+
() => {
|
|
280
|
+
return (response[key] = tempValues[key]);
|
|
281
|
+
},
|
|
282
|
+
(val) => {
|
|
283
|
+
delete response[key];
|
|
284
|
+
response[key] = val;
|
|
285
|
+
},
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
ah.resThenable = new AHRequest(ah.request)
|
|
289
|
+
.waitForResponseKeys(response)
|
|
290
|
+
.then(() => {
|
|
291
|
+
for (const key of xhrResponses) {
|
|
292
|
+
ah.proxyProps[key] = {
|
|
293
|
+
get: () => {
|
|
294
|
+
if (!(key in response)) response[key] = tempValues[key];
|
|
295
|
+
return response[key];
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
ah.dispatchEvent(e);
|
|
302
|
+
});
|
|
303
|
+
xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
|
|
304
|
+
xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
|
|
305
|
+
for (const evt of xhrAsyncEvents) {
|
|
306
|
+
const onEvt = "on" + evt;
|
|
307
|
+
ah.proxyProps[onEvt] = {
|
|
308
|
+
get: () => ah.proxyEvents[onEvt] || null,
|
|
309
|
+
set: (val) => ah.addEvent(onEvt, val),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
for (const method of [
|
|
313
|
+
"setRequestHeader",
|
|
314
|
+
"addEventListener",
|
|
315
|
+
"removeEventListener",
|
|
316
|
+
"open",
|
|
317
|
+
"send",
|
|
318
|
+
]) {
|
|
319
|
+
ah.proxyProps[method] = { value: ah[method] };
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
toJSON() {} // Converting circular structure to JSON
|
|
323
|
+
addEvent(type, event) {
|
|
324
|
+
if (type.startsWith("on")) {
|
|
325
|
+
this.proxyEvents[type] = typeof event === "function" ? event : null;
|
|
326
|
+
} else {
|
|
327
|
+
if (typeof event === "object" && event !== null)
|
|
328
|
+
event = event.handleEvent;
|
|
329
|
+
if (typeof event !== "function") return;
|
|
330
|
+
this.proxyEvents[type] = this.proxyEvents[type] || new Set();
|
|
331
|
+
this.proxyEvents[type].add(event);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
removeEvent(type, event) {
|
|
335
|
+
if (type.startsWith("on")) {
|
|
336
|
+
this.proxyEvents[type] = null;
|
|
337
|
+
} else {
|
|
338
|
+
if (typeof event === "object" && event !== null)
|
|
339
|
+
event = event.handleEvent;
|
|
340
|
+
this.proxyEvents[type] && this.proxyEvents[type].delete(event);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
dispatchEvent(e) {
|
|
344
|
+
e.stopImmediatePropagation = stopImmediatePropagation;
|
|
345
|
+
defineProp(e, "target", () => this.proxyXhr);
|
|
346
|
+
defineProp(e, "currentTarget", () => this.proxyXhr);
|
|
347
|
+
defineProp(e, "srcElement", () => this.proxyXhr);
|
|
348
|
+
this.proxyEvents[e.type] &&
|
|
349
|
+
this.proxyEvents[e.type].forEach((fn) => {
|
|
350
|
+
this.resThenable.then(
|
|
351
|
+
() => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e),
|
|
352
|
+
);
|
|
353
|
+
});
|
|
354
|
+
if (e.ajaxHooker_isStopped) return;
|
|
355
|
+
const onEvent = this.proxyEvents["on" + e.type];
|
|
356
|
+
onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
|
|
357
|
+
}
|
|
358
|
+
setRequestHeader(header, value) {
|
|
359
|
+
this.originalXhr.setRequestHeader(header, value);
|
|
360
|
+
if (!this.request) return;
|
|
361
|
+
const headers = this.request.headers;
|
|
362
|
+
headers[header] =
|
|
363
|
+
header in headers ? `${headers[header]}, ${value}` : value;
|
|
364
|
+
}
|
|
365
|
+
addEventListener(...args) {
|
|
366
|
+
if (xhrAsyncEvents.includes(args[0])) {
|
|
367
|
+
this.addEvent(args[0], args[1]);
|
|
368
|
+
} else {
|
|
369
|
+
this.originalXhr.addEventListener(...args);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
removeEventListener(...args) {
|
|
373
|
+
if (xhrAsyncEvents.includes(args[0])) {
|
|
374
|
+
this.removeEvent(args[0], args[1]);
|
|
375
|
+
} else {
|
|
376
|
+
this.originalXhr.removeEventListener(...args);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
open(method, url, async = true, ...args) {
|
|
380
|
+
this.request = {
|
|
381
|
+
type: "xhr",
|
|
382
|
+
url: url.toString(),
|
|
383
|
+
method: method.toUpperCase(),
|
|
384
|
+
abort: false,
|
|
385
|
+
headers: {},
|
|
386
|
+
data: null,
|
|
387
|
+
response: null,
|
|
388
|
+
async: !!async,
|
|
389
|
+
};
|
|
390
|
+
this.openArgs = args;
|
|
391
|
+
this.resThenable = new SyncThenable();
|
|
392
|
+
[
|
|
393
|
+
"responseURL",
|
|
394
|
+
"readyState",
|
|
395
|
+
"status",
|
|
396
|
+
"statusText",
|
|
397
|
+
...xhrResponses,
|
|
398
|
+
].forEach((key) => {
|
|
399
|
+
delete this.proxyProps[key];
|
|
400
|
+
});
|
|
401
|
+
return this.originalXhr.open(method, url, async, ...args);
|
|
402
|
+
}
|
|
403
|
+
send(data) {
|
|
404
|
+
const ah = this;
|
|
405
|
+
const xhr = ah.originalXhr;
|
|
406
|
+
const request = ah.request;
|
|
407
|
+
if (!request) return xhr.send(data);
|
|
408
|
+
request.data = data;
|
|
409
|
+
new AHRequest(request).waitForRequestKeys().then(() => {
|
|
410
|
+
if (request.abort) {
|
|
411
|
+
if (typeof request.response === "function") {
|
|
412
|
+
Object.assign(ah.proxyProps, {
|
|
413
|
+
responseURL: { value: request.url },
|
|
414
|
+
readyState: { value: 4 },
|
|
415
|
+
status: { value: 200 },
|
|
416
|
+
statusText: { value: "OK" },
|
|
417
|
+
});
|
|
418
|
+
xhrAsyncEvents.forEach((evt) => xhr.dispatchEvent(new Event(evt)));
|
|
419
|
+
}
|
|
420
|
+
} else {
|
|
421
|
+
xhr.open(request.method, request.url, request.async, ...ah.openArgs);
|
|
422
|
+
for (const header in request.headers) {
|
|
423
|
+
xhr.setRequestHeader(header, request.headers[header]);
|
|
424
|
+
}
|
|
425
|
+
for (const prop of xhrExtraProps) {
|
|
426
|
+
if (prop in request) xhr[prop] = request[prop];
|
|
427
|
+
}
|
|
428
|
+
xhr.send(request.data);
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
function fakeXHR() {
|
|
434
|
+
const xhr = new winAh.realXHR();
|
|
435
|
+
if ("__ajaxHooker" in xhr)
|
|
436
|
+
console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
|
|
437
|
+
xhr.__ajaxHooker = new XhrHooker(xhr);
|
|
438
|
+
return xhr.__ajaxHooker.proxyXhr;
|
|
439
|
+
}
|
|
440
|
+
fakeXHR.prototype = win.XMLHttpRequest.prototype;
|
|
441
|
+
Object.keys(win.XMLHttpRequest).forEach(
|
|
442
|
+
(key) => (fakeXHR[key] = win.XMLHttpRequest[key]),
|
|
443
|
+
);
|
|
444
|
+
function fakeFetch(url, options = {}) {
|
|
445
|
+
if (!url) return winAh.realFetch.call(win, url, options);
|
|
446
|
+
return new Promise(async (resolve, reject) => {
|
|
447
|
+
const init = {};
|
|
448
|
+
if (getType(url) === "[object Request]") {
|
|
449
|
+
init.method = url.method;
|
|
450
|
+
init.headers = url.headers;
|
|
451
|
+
if (url.body) init.body = await url.arrayBuffer();
|
|
452
|
+
for (const prop of fetchExtraProps) init[prop] = url[prop];
|
|
453
|
+
url = url.url;
|
|
454
|
+
}
|
|
455
|
+
url = url.toString();
|
|
456
|
+
Object.assign(init, options);
|
|
457
|
+
init.method = init.method || "GET";
|
|
458
|
+
init.headers = init.headers || {};
|
|
459
|
+
const request = {
|
|
460
|
+
type: "fetch",
|
|
461
|
+
url: url,
|
|
462
|
+
method: init.method.toUpperCase(),
|
|
463
|
+
abort: false,
|
|
464
|
+
headers: parseHeaders(init.headers),
|
|
465
|
+
data: init.body,
|
|
466
|
+
response: null,
|
|
467
|
+
async: true,
|
|
468
|
+
};
|
|
469
|
+
const req = new AHRequest(request);
|
|
470
|
+
await req.waitForRequestKeys();
|
|
471
|
+
if (request.abort) {
|
|
472
|
+
if (typeof request.response === "function") {
|
|
473
|
+
const response = {
|
|
474
|
+
finalUrl: request.url,
|
|
475
|
+
status: 200,
|
|
476
|
+
responseHeaders: {},
|
|
477
|
+
};
|
|
478
|
+
await req.waitForResponseKeys(response);
|
|
479
|
+
const key = fetchResponses.find((k) => k in response);
|
|
480
|
+
let val = response[key];
|
|
481
|
+
if (key === "json" && typeof val === "object") {
|
|
482
|
+
val = catchError(JSON.stringify.bind(JSON), val);
|
|
483
|
+
}
|
|
484
|
+
const res = new Response(val, {
|
|
485
|
+
status: 200,
|
|
486
|
+
statusText: "OK",
|
|
487
|
+
});
|
|
488
|
+
defineProp(res, "type", () => "basic");
|
|
489
|
+
defineProp(res, "url", () => request.url);
|
|
490
|
+
resolve(res);
|
|
491
|
+
} else {
|
|
492
|
+
reject(new DOMException("aborted", "AbortError"));
|
|
493
|
+
}
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
init.method = request.method;
|
|
497
|
+
init.headers = request.headers;
|
|
498
|
+
init.body = request.data;
|
|
499
|
+
for (const prop of fetchExtraProps) {
|
|
500
|
+
if (prop in request) init[prop] = request[prop];
|
|
501
|
+
}
|
|
502
|
+
winAh.realFetch.call(win, request.url, init).then((res) => {
|
|
503
|
+
if (typeof request.response === "function") {
|
|
504
|
+
const response = {
|
|
505
|
+
finalUrl: res.url,
|
|
506
|
+
status: res.status,
|
|
507
|
+
responseHeaders: parseHeaders(res.headers),
|
|
508
|
+
};
|
|
509
|
+
if (res.ok) {
|
|
510
|
+
fetchResponses.forEach(
|
|
511
|
+
(key) =>
|
|
512
|
+
(res[key] = function () {
|
|
513
|
+
if (key in response) return Promise.resolve(response[key]);
|
|
514
|
+
return resProto[key].call(this).then((val) => {
|
|
515
|
+
response[key] = val;
|
|
516
|
+
return req
|
|
517
|
+
.waitForResponseKeys(response)
|
|
518
|
+
.then(() => (key in response ? response[key] : val));
|
|
519
|
+
});
|
|
520
|
+
}),
|
|
521
|
+
);
|
|
522
|
+
} else {
|
|
523
|
+
catchError(request.response, response);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
resolve(res);
|
|
527
|
+
}, reject);
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
function fakeFetchClone() {
|
|
531
|
+
const descriptors = Object.getOwnPropertyDescriptors(this);
|
|
532
|
+
const res = winAh.realFetchClone.call(this);
|
|
533
|
+
Object.defineProperties(res, descriptors);
|
|
534
|
+
return res;
|
|
535
|
+
}
|
|
536
|
+
winAh = win.__ajaxHooker = winAh || {
|
|
537
|
+
version,
|
|
538
|
+
fakeXHR,
|
|
539
|
+
fakeFetch,
|
|
540
|
+
fakeFetchClone,
|
|
541
|
+
realXHR: win.XMLHttpRequest,
|
|
542
|
+
realFetch: win.fetch,
|
|
543
|
+
realFetchClone: resProto.clone,
|
|
544
|
+
hookInsts: new Set(),
|
|
545
|
+
};
|
|
546
|
+
if (winAh.version !== version)
|
|
547
|
+
console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
|
|
548
|
+
win.XMLHttpRequest = winAh.fakeXHR;
|
|
549
|
+
win.fetch = winAh.fakeFetch;
|
|
550
|
+
resProto.clone = winAh.fakeFetchClone;
|
|
551
|
+
winAh.hookInsts.add(hookInst);
|
|
552
|
+
// 针对头条、抖音 secsdk.umd.js 的兼容性处理
|
|
553
|
+
class AHFunction extends Function {
|
|
554
|
+
call(thisArg, ...args) {
|
|
555
|
+
if (
|
|
556
|
+
thisArg &&
|
|
557
|
+
thisArg.__ajaxHooker &&
|
|
558
|
+
thisArg.__ajaxHooker.proxyXhr === thisArg
|
|
559
|
+
) {
|
|
560
|
+
thisArg = thisArg.__ajaxHooker.originalXhr;
|
|
561
|
+
}
|
|
562
|
+
return Reflect.apply(this, thisArg, args);
|
|
563
|
+
}
|
|
564
|
+
apply(thisArg, args) {
|
|
565
|
+
if (
|
|
566
|
+
thisArg &&
|
|
567
|
+
thisArg.__ajaxHooker &&
|
|
568
|
+
thisArg.__ajaxHooker.proxyXhr === thisArg
|
|
569
|
+
) {
|
|
570
|
+
thisArg = thisArg.__ajaxHooker.originalXhr;
|
|
571
|
+
}
|
|
572
|
+
return Reflect.apply(this, thisArg, args || []);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function hookSecsdk(csrf) {
|
|
576
|
+
Object.setPrototypeOf(
|
|
577
|
+
csrf.nativeXMLHttpRequestSetRequestHeader,
|
|
578
|
+
AHFunction.prototype,
|
|
579
|
+
);
|
|
580
|
+
Object.setPrototypeOf(csrf.nativeXMLHttpRequestOpen, AHFunction.prototype);
|
|
581
|
+
Object.setPrototypeOf(csrf.nativeXMLHttpRequestSend, AHFunction.prototype);
|
|
582
|
+
}
|
|
583
|
+
if (win.secsdk) {
|
|
584
|
+
if (win.secsdk.csrf && win.secsdk.csrf.nativeXMLHttpRequestOpen)
|
|
585
|
+
hookSecsdk(win.secsdk.csrf);
|
|
586
|
+
} else {
|
|
587
|
+
defineProp(win, "secsdk", emptyFn, (secsdk) => {
|
|
588
|
+
delete win.secsdk;
|
|
589
|
+
win.secsdk = secsdk;
|
|
590
|
+
defineProp(secsdk, "csrf", emptyFn, (csrf) => {
|
|
591
|
+
delete secsdk.csrf;
|
|
592
|
+
secsdk.csrf = csrf;
|
|
593
|
+
if (csrf.nativeXMLHttpRequestOpen) hookSecsdk(csrf);
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
return {
|
|
598
|
+
hook: (fn) => hookInst.hookFns.push(fn),
|
|
599
|
+
filter: (arr) => {
|
|
600
|
+
if (Array.isArray(arr)) hookInst.filters = arr;
|
|
601
|
+
},
|
|
602
|
+
protect: () => {
|
|
603
|
+
readonly(win, "XMLHttpRequest", winAh.fakeXHR);
|
|
604
|
+
readonly(win, "fetch", winAh.fakeFetch);
|
|
605
|
+
readonly(resProto, "clone", winAh.fakeFetchClone);
|
|
606
|
+
},
|
|
607
|
+
unhook: () => {
|
|
608
|
+
winAh.hookInsts.delete(hookInst);
|
|
609
|
+
if (!winAh.hookInsts.size) {
|
|
610
|
+
writable(win, "XMLHttpRequest", winAh.realXHR);
|
|
611
|
+
writable(win, "fetch", winAh.realFetch);
|
|
612
|
+
writable(resProto, "clone", winAh.realFetchClone);
|
|
613
|
+
delete win.__ajaxHooker;
|
|
614
|
+
}
|
|
615
|
+
},
|
|
616
|
+
};
|
|
549
617
|
};
|