@whitesev/utils 1.0.2 → 1.0.3
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/dist/index.amd.js +518 -503
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +518 -503
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +518 -503
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +518 -503
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +518 -503
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +518 -503
- package/dist/index.umd.js.map +1 -1
- package/dist/src/ajaxHooker.d.ts +6 -6
- package/package.json +1 -1
- package/rollup.config.js +1 -0
- package/src/Utils.ts +3 -3
- package/src/ajaxHooker.js +518 -503
package/dist/index.umd.js
CHANGED
|
@@ -379,181 +379,183 @@
|
|
|
379
379
|
// @version 1.4.1
|
|
380
380
|
// @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
|
|
381
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
|
-
function catchError(fn, ...args) {
|
|
421
|
-
try {
|
|
422
|
-
const result = fn(...args);
|
|
423
|
-
if (isThenable(result)) return result.then(null, errorFn);
|
|
424
|
-
return result;
|
|
425
|
-
} catch (err) {
|
|
426
|
-
console.error(err);
|
|
382
|
+
const AjaxHooker = function () {
|
|
383
|
+
return (function () {
|
|
384
|
+
const version = "1.4.1";
|
|
385
|
+
const hookInst = {
|
|
386
|
+
hookFns: [],
|
|
387
|
+
filters: [],
|
|
388
|
+
};
|
|
389
|
+
const win = window.unsafeWindow || document.defaultView || window;
|
|
390
|
+
let winAh = win.__ajaxHooker;
|
|
391
|
+
const resProto = win.Response.prototype;
|
|
392
|
+
const xhrResponses = ["response", "responseText", "responseXML"];
|
|
393
|
+
const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
|
|
394
|
+
const fetchInitProps = [
|
|
395
|
+
"method",
|
|
396
|
+
"headers",
|
|
397
|
+
"body",
|
|
398
|
+
"mode",
|
|
399
|
+
"credentials",
|
|
400
|
+
"cache",
|
|
401
|
+
"redirect",
|
|
402
|
+
"referrer",
|
|
403
|
+
"referrerPolicy",
|
|
404
|
+
"integrity",
|
|
405
|
+
"keepalive",
|
|
406
|
+
"signal",
|
|
407
|
+
"priority",
|
|
408
|
+
];
|
|
409
|
+
const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
|
|
410
|
+
const getType = {}.toString.call.bind({}.toString);
|
|
411
|
+
const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
|
|
412
|
+
const emptyFn = () => {};
|
|
413
|
+
const errorFn = (e) => console.error(e);
|
|
414
|
+
function isThenable(obj) {
|
|
415
|
+
return (
|
|
416
|
+
obj &&
|
|
417
|
+
["object", "function"].includes(typeof obj) &&
|
|
418
|
+
typeof obj.then === "function"
|
|
419
|
+
);
|
|
427
420
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
437
|
-
function readonly(obj, prop, value = obj[prop]) {
|
|
438
|
-
defineProp(obj, prop, () => value, emptyFn);
|
|
439
|
-
}
|
|
440
|
-
function writable(obj, prop, value = obj[prop]) {
|
|
441
|
-
Object.defineProperty(obj, prop, {
|
|
442
|
-
configurable: true,
|
|
443
|
-
enumerable: true,
|
|
444
|
-
writable: true,
|
|
445
|
-
value: value,
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
function parseHeaders(obj) {
|
|
449
|
-
const headers = {};
|
|
450
|
-
switch (getType(obj)) {
|
|
451
|
-
case "[object String]":
|
|
452
|
-
for (const line of obj.trim().split(/[\r\n]+/)) {
|
|
453
|
-
const [header, value] = line.split(/\s*:\s*/);
|
|
454
|
-
if (!header) break;
|
|
455
|
-
const lheader = header.toLowerCase();
|
|
456
|
-
headers[lheader] =
|
|
457
|
-
lheader in headers ? `${headers[lheader]}, ${value}` : value;
|
|
458
|
-
}
|
|
459
|
-
break;
|
|
460
|
-
case "[object Headers]":
|
|
461
|
-
for (const [key, val] of obj) {
|
|
462
|
-
headers[key] = val;
|
|
463
|
-
}
|
|
464
|
-
break;
|
|
465
|
-
case "[object Object]":
|
|
466
|
-
return { ...obj };
|
|
421
|
+
function catchError(fn, ...args) {
|
|
422
|
+
try {
|
|
423
|
+
const result = fn(...args);
|
|
424
|
+
if (isThenable(result)) return result.then(null, errorFn);
|
|
425
|
+
return result;
|
|
426
|
+
} catch (err) {
|
|
427
|
+
console.error(err);
|
|
428
|
+
}
|
|
467
429
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
fn && fn();
|
|
476
|
-
return new SyncThenable();
|
|
430
|
+
function defineProp(obj, prop, getter, setter) {
|
|
431
|
+
Object.defineProperty(obj, prop, {
|
|
432
|
+
configurable: true,
|
|
433
|
+
enumerable: true,
|
|
434
|
+
get: getter,
|
|
435
|
+
set: setter,
|
|
436
|
+
});
|
|
477
437
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
constructor(request) {
|
|
481
|
-
this.request = request;
|
|
482
|
-
this.requestClone = { ...this.request };
|
|
438
|
+
function readonly(obj, prop, value = obj[prop]) {
|
|
439
|
+
defineProp(obj, prop, () => value, emptyFn);
|
|
483
440
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
441
|
+
function writable(obj, prop, value = obj[prop]) {
|
|
442
|
+
Object.defineProperty(obj, prop, {
|
|
443
|
+
configurable: true,
|
|
444
|
+
enumerable: true,
|
|
445
|
+
writable: true,
|
|
446
|
+
value: value,
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
function parseHeaders(obj) {
|
|
450
|
+
const headers = {};
|
|
451
|
+
switch (getType(obj)) {
|
|
452
|
+
case "[object String]":
|
|
453
|
+
for (const line of obj.trim().split(/[\r\n]+/)) {
|
|
454
|
+
const [header, value] = line.split(/\s*:\s*/);
|
|
455
|
+
if (!header) break;
|
|
456
|
+
const lheader = header.toLowerCase();
|
|
457
|
+
headers[lheader] =
|
|
458
|
+
lheader in headers ? `${headers[lheader]}, ${value}` : value;
|
|
498
459
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
460
|
+
break;
|
|
461
|
+
case "[object Headers]":
|
|
462
|
+
for (const [key, val] of obj) {
|
|
463
|
+
headers[key] = val;
|
|
464
|
+
}
|
|
465
|
+
break;
|
|
466
|
+
case "[object Object]":
|
|
467
|
+
return { ...obj };
|
|
468
|
+
}
|
|
469
|
+
return headers;
|
|
502
470
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
if (getType(fn) === "[object Function]")
|
|
510
|
-
catchError(fn, this.request);
|
|
511
|
-
});
|
|
512
|
-
requestKeys.forEach((key) => {
|
|
513
|
-
if (isThenable(this.request[key]))
|
|
514
|
-
this.request[key] = this.requestClone[key];
|
|
515
|
-
});
|
|
516
|
-
});
|
|
471
|
+
function stopImmediatePropagation() {
|
|
472
|
+
this.ajaxHooker_isStopped = true;
|
|
473
|
+
}
|
|
474
|
+
class SyncThenable {
|
|
475
|
+
then(fn) {
|
|
476
|
+
fn && fn();
|
|
517
477
|
return new SyncThenable();
|
|
518
478
|
}
|
|
519
|
-
const promises = [];
|
|
520
|
-
win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
|
|
521
|
-
if (this.shouldFilter(filters)) return;
|
|
522
|
-
promises.push(
|
|
523
|
-
Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
|
|
524
|
-
() =>
|
|
525
|
-
Promise.all(
|
|
526
|
-
requestKeys.map((key) =>
|
|
527
|
-
Promise.resolve(this.request[key]).then(
|
|
528
|
-
(val) => (this.request[key] = val),
|
|
529
|
-
() => (this.request[key] = this.requestClone[key])
|
|
530
|
-
)
|
|
531
|
-
)
|
|
532
|
-
)
|
|
533
|
-
)
|
|
534
|
-
);
|
|
535
|
-
});
|
|
536
|
-
return Promise.all(promises);
|
|
537
479
|
}
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
this.request
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
) {
|
|
549
|
-
|
|
480
|
+
class AHRequest {
|
|
481
|
+
constructor(request) {
|
|
482
|
+
this.request = request;
|
|
483
|
+
this.requestClone = { ...this.request };
|
|
484
|
+
}
|
|
485
|
+
shouldFilter(filters) {
|
|
486
|
+
const { type, url, method, async } = this.request;
|
|
487
|
+
return (
|
|
488
|
+
filters.length &&
|
|
489
|
+
!filters.find((obj) => {
|
|
490
|
+
switch (true) {
|
|
491
|
+
case obj.type && obj.type !== type:
|
|
492
|
+
case getType(obj.url) === "[object String]" &&
|
|
493
|
+
!url.includes(obj.url):
|
|
494
|
+
case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
|
|
495
|
+
case obj.method &&
|
|
496
|
+
obj.method.toUpperCase() !== method.toUpperCase():
|
|
497
|
+
case "async" in obj && obj.async !== async:
|
|
498
|
+
return false;
|
|
550
499
|
}
|
|
500
|
+
return true;
|
|
501
|
+
})
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
waitForRequestKeys() {
|
|
505
|
+
const requestKeys = ["url", "method", "abort", "headers", "data"];
|
|
506
|
+
if (!this.request.async) {
|
|
507
|
+
win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
|
|
508
|
+
if (this.shouldFilter(filters)) return;
|
|
509
|
+
hookFns.forEach((fn) => {
|
|
510
|
+
if (getType(fn) === "[object Function]")
|
|
511
|
+
catchError(fn, this.request);
|
|
512
|
+
});
|
|
513
|
+
requestKeys.forEach((key) => {
|
|
514
|
+
if (isThenable(this.request[key]))
|
|
515
|
+
this.request[key] = this.requestClone[key];
|
|
516
|
+
});
|
|
551
517
|
});
|
|
518
|
+
return new SyncThenable();
|
|
552
519
|
}
|
|
553
|
-
|
|
520
|
+
const promises = [];
|
|
521
|
+
win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
|
|
522
|
+
if (this.shouldFilter(filters)) return;
|
|
523
|
+
promises.push(
|
|
524
|
+
Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
|
|
525
|
+
() =>
|
|
526
|
+
Promise.all(
|
|
527
|
+
requestKeys.map((key) =>
|
|
528
|
+
Promise.resolve(this.request[key]).then(
|
|
529
|
+
(val) => (this.request[key] = val),
|
|
530
|
+
() => (this.request[key] = this.requestClone[key])
|
|
531
|
+
)
|
|
532
|
+
)
|
|
533
|
+
)
|
|
534
|
+
)
|
|
535
|
+
);
|
|
536
|
+
});
|
|
537
|
+
return Promise.all(promises);
|
|
554
538
|
}
|
|
555
|
-
|
|
556
|
-
|
|
539
|
+
waitForResponseKeys(response) {
|
|
540
|
+
const responseKeys =
|
|
541
|
+
this.request.type === "xhr" ? xhrResponses : fetchResponses;
|
|
542
|
+
if (!this.request.async) {
|
|
543
|
+
if (getType(this.request.response) === "[object Function]") {
|
|
544
|
+
catchError(this.request.response, response);
|
|
545
|
+
responseKeys.forEach((key) => {
|
|
546
|
+
if (
|
|
547
|
+
"get" in getDescriptor(response, key) ||
|
|
548
|
+
isThenable(response[key])
|
|
549
|
+
) {
|
|
550
|
+
delete response[key];
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
return new SyncThenable();
|
|
555
|
+
}
|
|
556
|
+
return Promise.resolve(
|
|
557
|
+
catchError(this.request.response, response)
|
|
558
|
+
).then(() =>
|
|
557
559
|
Promise.all(
|
|
558
560
|
responseKeys.map((key) => {
|
|
559
561
|
const descriptor = getDescriptor(response, key);
|
|
@@ -567,368 +569,381 @@
|
|
|
567
569
|
}
|
|
568
570
|
})
|
|
569
571
|
)
|
|
570
|
-
|
|
572
|
+
);
|
|
573
|
+
}
|
|
571
574
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
575
|
+
const proxyHandler = {
|
|
576
|
+
get(target, prop) {
|
|
577
|
+
const descriptor = getDescriptor(target, prop);
|
|
578
|
+
if (
|
|
579
|
+
descriptor &&
|
|
580
|
+
!descriptor.configurable &&
|
|
581
|
+
!descriptor.writable &&
|
|
582
|
+
!descriptor.get
|
|
583
|
+
)
|
|
584
|
+
return target[prop];
|
|
585
|
+
const ah = target.__ajaxHooker;
|
|
586
|
+
if (ah && ah.proxyProps) {
|
|
587
|
+
if (prop in ah.proxyProps) {
|
|
588
|
+
const pDescriptor = ah.proxyProps[prop];
|
|
589
|
+
if ("get" in pDescriptor) return pDescriptor.get();
|
|
590
|
+
if (typeof pDescriptor.value === "function")
|
|
591
|
+
return pDescriptor.value.bind(ah);
|
|
592
|
+
return pDescriptor.value;
|
|
593
|
+
}
|
|
594
|
+
if (typeof target[prop] === "function")
|
|
595
|
+
return target[prop].bind(target);
|
|
596
|
+
}
|
|
582
597
|
return target[prop];
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
598
|
+
},
|
|
599
|
+
set(target, prop, value) {
|
|
600
|
+
const descriptor = getDescriptor(target, prop);
|
|
601
|
+
if (
|
|
602
|
+
descriptor &&
|
|
603
|
+
!descriptor.configurable &&
|
|
604
|
+
!descriptor.writable &&
|
|
605
|
+
!descriptor.set
|
|
606
|
+
)
|
|
607
|
+
return true;
|
|
608
|
+
const ah = target.__ajaxHooker;
|
|
609
|
+
if (ah && ah.proxyProps && prop in ah.proxyProps) {
|
|
586
610
|
const pDescriptor = ah.proxyProps[prop];
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
611
|
+
pDescriptor.set
|
|
612
|
+
? pDescriptor.set(value)
|
|
613
|
+
: (pDescriptor.value = value);
|
|
614
|
+
} else {
|
|
615
|
+
target[prop] = value;
|
|
591
616
|
}
|
|
592
|
-
if (typeof target[prop] === "function")
|
|
593
|
-
return target[prop].bind(target);
|
|
594
|
-
}
|
|
595
|
-
return target[prop];
|
|
596
|
-
},
|
|
597
|
-
set(target, prop, value) {
|
|
598
|
-
const descriptor = getDescriptor(target, prop);
|
|
599
|
-
if (
|
|
600
|
-
descriptor &&
|
|
601
|
-
!descriptor.configurable &&
|
|
602
|
-
!descriptor.writable &&
|
|
603
|
-
!descriptor.set
|
|
604
|
-
)
|
|
605
617
|
return true;
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
618
|
+
},
|
|
619
|
+
};
|
|
620
|
+
class XhrHooker {
|
|
621
|
+
constructor(xhr) {
|
|
622
|
+
const ah = this;
|
|
623
|
+
Object.assign(ah, {
|
|
624
|
+
originalXhr: xhr,
|
|
625
|
+
proxyXhr: new Proxy(xhr, proxyHandler),
|
|
626
|
+
resThenable: new SyncThenable(),
|
|
627
|
+
proxyProps: {},
|
|
628
|
+
proxyEvents: {},
|
|
629
|
+
});
|
|
630
|
+
xhr.addEventListener("readystatechange", (e) => {
|
|
631
|
+
if (
|
|
632
|
+
ah.proxyXhr.readyState === 4 &&
|
|
633
|
+
ah.request &&
|
|
634
|
+
typeof ah.request.response === "function"
|
|
635
|
+
) {
|
|
636
|
+
const response = {
|
|
637
|
+
finalUrl: ah.proxyXhr.responseURL,
|
|
638
|
+
status: ah.proxyXhr.status,
|
|
639
|
+
responseHeaders: parseHeaders(
|
|
640
|
+
ah.proxyXhr.getAllResponseHeaders()
|
|
641
|
+
),
|
|
642
|
+
};
|
|
643
|
+
const tempValues = {};
|
|
644
|
+
for (const key of xhrResponses) {
|
|
645
|
+
try {
|
|
646
|
+
tempValues[key] = ah.originalXhr[key];
|
|
647
|
+
} catch (err) {}
|
|
648
|
+
defineProp(
|
|
649
|
+
response,
|
|
650
|
+
key,
|
|
651
|
+
() => {
|
|
652
|
+
return (response[key] = tempValues[key]);
|
|
653
|
+
},
|
|
654
|
+
(val) => {
|
|
655
|
+
delete response[key];
|
|
656
|
+
response[key] = val;
|
|
657
|
+
}
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
ah.resThenable = new AHRequest(ah.request)
|
|
661
|
+
.waitForResponseKeys(response)
|
|
662
|
+
.then(() => {
|
|
663
|
+
for (const key of xhrResponses) {
|
|
664
|
+
ah.proxyProps[key] = {
|
|
665
|
+
get: () => {
|
|
666
|
+
if (!(key in response)) response[key] = tempValues[key];
|
|
667
|
+
return response[key];
|
|
668
|
+
},
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
});
|
|
653
672
|
}
|
|
654
|
-
ah.
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
673
|
+
ah.dispatchEvent(e);
|
|
674
|
+
});
|
|
675
|
+
xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
|
|
676
|
+
xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
|
|
677
|
+
for (const evt of xhrAsyncEvents) {
|
|
678
|
+
const onEvt = "on" + evt;
|
|
679
|
+
ah.proxyProps[onEvt] = {
|
|
680
|
+
get: () => ah.proxyEvents[onEvt] || null,
|
|
681
|
+
set: (val) => ah.addEvent(onEvt, val),
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
for (const method of [
|
|
685
|
+
"setRequestHeader",
|
|
686
|
+
"addEventListener",
|
|
687
|
+
"removeEventListener",
|
|
688
|
+
"open",
|
|
689
|
+
"send",
|
|
690
|
+
]) {
|
|
691
|
+
ah.proxyProps[method] = { value: ah[method] };
|
|
666
692
|
}
|
|
667
|
-
ah.dispatchEvent(e);
|
|
668
|
-
});
|
|
669
|
-
xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
|
|
670
|
-
xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
|
|
671
|
-
for (const evt of xhrAsyncEvents) {
|
|
672
|
-
const onEvt = "on" + evt;
|
|
673
|
-
ah.proxyProps[onEvt] = {
|
|
674
|
-
get: () => ah.proxyEvents[onEvt] || null,
|
|
675
|
-
set: (val) => ah.addEvent(onEvt, val),
|
|
676
|
-
};
|
|
677
693
|
}
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
"
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
694
|
+
toJSON() {} // Converting circular structure to JSON
|
|
695
|
+
addEvent(type, event) {
|
|
696
|
+
if (type.startsWith("on")) {
|
|
697
|
+
this.proxyEvents[type] = typeof event === "function" ? event : null;
|
|
698
|
+
} else {
|
|
699
|
+
if (typeof event === "object" && event !== null)
|
|
700
|
+
event = event.handleEvent;
|
|
701
|
+
if (typeof event !== "function") return;
|
|
702
|
+
this.proxyEvents[type] = this.proxyEvents[type] || new Set();
|
|
703
|
+
this.proxyEvents[type].add(event);
|
|
704
|
+
}
|
|
686
705
|
}
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
if (typeof event !== "function") return;
|
|
696
|
-
this.proxyEvents[type] = this.proxyEvents[type] || new Set();
|
|
697
|
-
this.proxyEvents[type].add(event);
|
|
706
|
+
removeEvent(type, event) {
|
|
707
|
+
if (type.startsWith("on")) {
|
|
708
|
+
this.proxyEvents[type] = null;
|
|
709
|
+
} else {
|
|
710
|
+
if (typeof event === "object" && event !== null)
|
|
711
|
+
event = event.handleEvent;
|
|
712
|
+
this.proxyEvents[type] && this.proxyEvents[type].delete(event);
|
|
713
|
+
}
|
|
698
714
|
}
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
this.proxyEvents[type]
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
715
|
+
dispatchEvent(e) {
|
|
716
|
+
e.stopImmediatePropagation = stopImmediatePropagation;
|
|
717
|
+
defineProp(e, "target", () => this.proxyXhr);
|
|
718
|
+
this.proxyEvents[e.type] &&
|
|
719
|
+
this.proxyEvents[e.type].forEach((fn) => {
|
|
720
|
+
this.resThenable.then(
|
|
721
|
+
() => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e)
|
|
722
|
+
);
|
|
723
|
+
});
|
|
724
|
+
if (e.ajaxHooker_isStopped) return;
|
|
725
|
+
const onEvent = this.proxyEvents["on" + e.type];
|
|
726
|
+
onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
|
|
707
727
|
}
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
728
|
+
setRequestHeader(header, value) {
|
|
729
|
+
this.originalXhr.setRequestHeader(header, value);
|
|
730
|
+
if (this.originalXhr.readyState !== 1) return;
|
|
731
|
+
const headers = this.request.headers;
|
|
732
|
+
headers[header] =
|
|
733
|
+
header in headers ? `${headers[header]}, ${value}` : value;
|
|
734
|
+
}
|
|
735
|
+
addEventListener(...args) {
|
|
736
|
+
if (xhrAsyncEvents.includes(args[0])) {
|
|
737
|
+
this.addEvent(args[0], args[1]);
|
|
738
|
+
} else {
|
|
739
|
+
this.originalXhr.addEventListener(...args);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
removeEventListener(...args) {
|
|
743
|
+
if (xhrAsyncEvents.includes(args[0])) {
|
|
744
|
+
this.removeEvent(args[0], args[1]);
|
|
745
|
+
} else {
|
|
746
|
+
this.originalXhr.removeEventListener(...args);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
open(method, url, async = true, ...args) {
|
|
750
|
+
this.request = {
|
|
751
|
+
type: "xhr",
|
|
752
|
+
url: url.toString(),
|
|
753
|
+
method: method.toUpperCase(),
|
|
754
|
+
abort: false,
|
|
755
|
+
headers: {},
|
|
756
|
+
data: null,
|
|
757
|
+
response: null,
|
|
758
|
+
async: !!async,
|
|
759
|
+
};
|
|
760
|
+
this.openArgs = args;
|
|
761
|
+
this.resThenable = new SyncThenable();
|
|
762
|
+
[
|
|
763
|
+
"responseURL",
|
|
764
|
+
"readyState",
|
|
765
|
+
"status",
|
|
766
|
+
"statusText",
|
|
767
|
+
...xhrResponses,
|
|
768
|
+
].forEach((key) => {
|
|
769
|
+
delete this.proxyProps[key];
|
|
717
770
|
});
|
|
718
|
-
|
|
719
|
-
const onEvent = this.proxyEvents["on" + e.type];
|
|
720
|
-
onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
|
|
721
|
-
}
|
|
722
|
-
setRequestHeader(header, value) {
|
|
723
|
-
this.originalXhr.setRequestHeader(header, value);
|
|
724
|
-
if (this.originalXhr.readyState !== 1) return;
|
|
725
|
-
const headers = this.request.headers;
|
|
726
|
-
headers[header] =
|
|
727
|
-
header in headers ? `${headers[header]}, ${value}` : value;
|
|
728
|
-
}
|
|
729
|
-
addEventListener(...args) {
|
|
730
|
-
if (xhrAsyncEvents.includes(args[0])) {
|
|
731
|
-
this.addEvent(args[0], args[1]);
|
|
732
|
-
} else {
|
|
733
|
-
this.originalXhr.addEventListener(...args);
|
|
771
|
+
return this.originalXhr.open(method, url, async, ...args);
|
|
734
772
|
}
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
773
|
+
send(data) {
|
|
774
|
+
const ah = this;
|
|
775
|
+
const xhr = ah.originalXhr;
|
|
776
|
+
const request = ah.request;
|
|
777
|
+
if (!request) return xhr.send(data);
|
|
778
|
+
request.data = data;
|
|
779
|
+
new AHRequest(request).waitForRequestKeys().then(() => {
|
|
780
|
+
if (request.abort) {
|
|
781
|
+
if (typeof request.response === "function") {
|
|
782
|
+
Object.assign(ah.proxyProps, {
|
|
783
|
+
responseURL: { value: request.url },
|
|
784
|
+
readyState: { value: 4 },
|
|
785
|
+
status: { value: 200 },
|
|
786
|
+
statusText: { value: "OK" },
|
|
787
|
+
});
|
|
788
|
+
xhrAsyncEvents.forEach((evt) =>
|
|
789
|
+
xhr.dispatchEvent(new Event(evt))
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
} else {
|
|
793
|
+
xhr.open(
|
|
794
|
+
request.method,
|
|
795
|
+
request.url,
|
|
796
|
+
request.async,
|
|
797
|
+
...ah.openArgs
|
|
798
|
+
);
|
|
799
|
+
for (const header in request.headers) {
|
|
800
|
+
xhr.setRequestHeader(header, request.headers[header]);
|
|
801
|
+
}
|
|
802
|
+
xhr.send(request.data);
|
|
803
|
+
}
|
|
804
|
+
});
|
|
741
805
|
}
|
|
742
806
|
}
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
807
|
+
function fakeXHR() {
|
|
808
|
+
const xhr = new winAh.realXHR();
|
|
809
|
+
if ("__ajaxHooker" in xhr)
|
|
810
|
+
console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
|
|
811
|
+
xhr.__ajaxHooker = new XhrHooker(xhr);
|
|
812
|
+
return xhr.__ajaxHooker.proxyXhr;
|
|
813
|
+
}
|
|
814
|
+
fakeXHR.prototype = win.XMLHttpRequest.prototype;
|
|
815
|
+
Object.keys(win.XMLHttpRequest).forEach(
|
|
816
|
+
(key) => (fakeXHR[key] = win.XMLHttpRequest[key])
|
|
817
|
+
);
|
|
818
|
+
function fakeFetch(url, options = {}) {
|
|
819
|
+
if (!url) return winAh.realFetch.call(win, url, options);
|
|
820
|
+
const init = {};
|
|
821
|
+
if (getType(url) === "[object Request]") {
|
|
822
|
+
for (const prop of fetchInitProps) init[prop] = url[prop];
|
|
823
|
+
url = url.url;
|
|
824
|
+
}
|
|
825
|
+
url = url.toString();
|
|
826
|
+
Object.assign(init, options);
|
|
827
|
+
init.method = init.method || "GET";
|
|
828
|
+
init.headers = init.headers || {};
|
|
829
|
+
const request = {
|
|
830
|
+
type: "fetch",
|
|
831
|
+
url: url,
|
|
832
|
+
method: init.method.toUpperCase(),
|
|
748
833
|
abort: false,
|
|
749
|
-
headers:
|
|
750
|
-
data:
|
|
834
|
+
headers: parseHeaders(init.headers),
|
|
835
|
+
data: init.body,
|
|
751
836
|
response: null,
|
|
752
|
-
async:
|
|
837
|
+
async: true,
|
|
753
838
|
};
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
839
|
+
const req = new AHRequest(request);
|
|
840
|
+
return new Promise((resolve, reject) => {
|
|
841
|
+
req
|
|
842
|
+
.waitForRequestKeys()
|
|
843
|
+
.then(() => {
|
|
844
|
+
if (request.abort) {
|
|
845
|
+
if (typeof request.response === "function") {
|
|
846
|
+
const response = {
|
|
847
|
+
finalUrl: request.url,
|
|
848
|
+
status: 200,
|
|
849
|
+
responseHeaders: {},
|
|
850
|
+
};
|
|
851
|
+
req.waitForResponseKeys(response).then(() => {
|
|
852
|
+
const key = fetchResponses.find((k) => k in response);
|
|
853
|
+
let val = response[key];
|
|
854
|
+
if (key === "json" && typeof val === "object") {
|
|
855
|
+
val = catchError(JSON.stringify.bind(JSON), val);
|
|
856
|
+
}
|
|
857
|
+
const res = new Response(val, {
|
|
858
|
+
status: 200,
|
|
859
|
+
statusText: "OK",
|
|
860
|
+
});
|
|
861
|
+
defineProp(res, "type", () => "basic");
|
|
862
|
+
defineProp(res, "url", () => request.url);
|
|
863
|
+
resolve(res);
|
|
864
|
+
});
|
|
865
|
+
} else {
|
|
866
|
+
reject(new DOMException("aborted", "AbortError"));
|
|
867
|
+
}
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
init.method = request.method;
|
|
871
|
+
init.headers = request.headers;
|
|
872
|
+
init.body = request.data;
|
|
873
|
+
winAh.realFetch.call(win, request.url, init).then((res) => {
|
|
874
|
+
if (typeof request.response === "function") {
|
|
875
|
+
const response = {
|
|
876
|
+
finalUrl: res.url,
|
|
877
|
+
status: res.status,
|
|
878
|
+
responseHeaders: parseHeaders(res.headers),
|
|
879
|
+
};
|
|
880
|
+
fetchResponses.forEach(
|
|
881
|
+
(key) =>
|
|
882
|
+
(res[key] = function () {
|
|
883
|
+
if (key in response)
|
|
884
|
+
return Promise.resolve(response[key]);
|
|
885
|
+
return resProto[key].call(this).then((val) => {
|
|
886
|
+
response[key] = val;
|
|
887
|
+
return req
|
|
888
|
+
.waitForResponseKeys(response)
|
|
889
|
+
.then(() => (key in response ? response[key] : val));
|
|
890
|
+
});
|
|
891
|
+
})
|
|
892
|
+
);
|
|
893
|
+
}
|
|
894
|
+
resolve(res);
|
|
895
|
+
}, reject);
|
|
896
|
+
})
|
|
897
|
+
.catch((err) => {
|
|
898
|
+
console.error(err);
|
|
899
|
+
resolve(winAh.realFetch.call(win, url, init));
|
|
900
|
+
});
|
|
764
901
|
});
|
|
765
|
-
return this.originalXhr.open(method, url, async, ...args);
|
|
766
902
|
}
|
|
767
|
-
|
|
768
|
-
const
|
|
769
|
-
const
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
request.data = data;
|
|
773
|
-
new AHRequest(request).waitForRequestKeys().then(() => {
|
|
774
|
-
if (request.abort) {
|
|
775
|
-
if (typeof request.response === "function") {
|
|
776
|
-
Object.assign(ah.proxyProps, {
|
|
777
|
-
responseURL: { value: request.url },
|
|
778
|
-
readyState: { value: 4 },
|
|
779
|
-
status: { value: 200 },
|
|
780
|
-
statusText: { value: "OK" },
|
|
781
|
-
});
|
|
782
|
-
xhrAsyncEvents.forEach((evt) => xhr.dispatchEvent(new Event(evt)));
|
|
783
|
-
}
|
|
784
|
-
} else {
|
|
785
|
-
xhr.open(request.method, request.url, request.async, ...ah.openArgs);
|
|
786
|
-
for (const header in request.headers) {
|
|
787
|
-
xhr.setRequestHeader(header, request.headers[header]);
|
|
788
|
-
}
|
|
789
|
-
xhr.send(request.data);
|
|
790
|
-
}
|
|
791
|
-
});
|
|
903
|
+
function fakeFetchClone() {
|
|
904
|
+
const descriptors = Object.getOwnPropertyDescriptors(this);
|
|
905
|
+
const res = winAh.realFetchClone.call(this);
|
|
906
|
+
Object.defineProperties(res, descriptors);
|
|
907
|
+
return res;
|
|
792
908
|
}
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
909
|
+
winAh = win.__ajaxHooker = winAh || {
|
|
910
|
+
version,
|
|
911
|
+
fakeXHR,
|
|
912
|
+
fakeFetch,
|
|
913
|
+
fakeFetchClone,
|
|
914
|
+
realXHR: win.XMLHttpRequest,
|
|
915
|
+
realFetch: win.fetch,
|
|
916
|
+
realFetchClone: resProto.clone,
|
|
917
|
+
hookInsts: new Set(),
|
|
918
|
+
};
|
|
919
|
+
if (winAh.version !== version)
|
|
797
920
|
console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
headers: parseHeaders(init.headers),
|
|
822
|
-
data: init.body,
|
|
823
|
-
response: null,
|
|
824
|
-
async: true,
|
|
921
|
+
win.XMLHttpRequest = winAh.fakeXHR;
|
|
922
|
+
win.fetch = winAh.fakeFetch;
|
|
923
|
+
resProto.clone = winAh.fakeFetchClone;
|
|
924
|
+
winAh.hookInsts.add(hookInst);
|
|
925
|
+
return {
|
|
926
|
+
hook: (fn) => hookInst.hookFns.push(fn),
|
|
927
|
+
filter: (arr) => {
|
|
928
|
+
if (Array.isArray(arr)) hookInst.filters = arr;
|
|
929
|
+
},
|
|
930
|
+
protect: () => {
|
|
931
|
+
readonly(win, "XMLHttpRequest", winAh.fakeXHR);
|
|
932
|
+
readonly(win, "fetch", winAh.fakeFetch);
|
|
933
|
+
readonly(resProto, "clone", winAh.fakeFetchClone);
|
|
934
|
+
},
|
|
935
|
+
unhook: () => {
|
|
936
|
+
winAh.hookInsts.delete(hookInst);
|
|
937
|
+
if (!winAh.hookInsts.size) {
|
|
938
|
+
writable(win, "XMLHttpRequest", winAh.realXHR);
|
|
939
|
+
writable(win, "fetch", winAh.realFetch);
|
|
940
|
+
writable(resProto, "clone", winAh.realFetchClone);
|
|
941
|
+
delete win.__ajaxHooker;
|
|
942
|
+
}
|
|
943
|
+
},
|
|
825
944
|
};
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
req
|
|
829
|
-
.waitForRequestKeys()
|
|
830
|
-
.then(() => {
|
|
831
|
-
if (request.abort) {
|
|
832
|
-
if (typeof request.response === "function") {
|
|
833
|
-
const response = {
|
|
834
|
-
finalUrl: request.url,
|
|
835
|
-
status: 200,
|
|
836
|
-
responseHeaders: {},
|
|
837
|
-
};
|
|
838
|
-
req.waitForResponseKeys(response).then(() => {
|
|
839
|
-
const key = fetchResponses.find((k) => k in response);
|
|
840
|
-
let val = response[key];
|
|
841
|
-
if (key === "json" && typeof val === "object") {
|
|
842
|
-
val = catchError(JSON.stringify.bind(JSON), val);
|
|
843
|
-
}
|
|
844
|
-
const res = new Response(val, {
|
|
845
|
-
status: 200,
|
|
846
|
-
statusText: "OK",
|
|
847
|
-
});
|
|
848
|
-
defineProp(res, "type", () => "basic");
|
|
849
|
-
defineProp(res, "url", () => request.url);
|
|
850
|
-
resolve(res);
|
|
851
|
-
});
|
|
852
|
-
} else {
|
|
853
|
-
reject(new DOMException("aborted", "AbortError"));
|
|
854
|
-
}
|
|
855
|
-
return;
|
|
856
|
-
}
|
|
857
|
-
init.method = request.method;
|
|
858
|
-
init.headers = request.headers;
|
|
859
|
-
init.body = request.data;
|
|
860
|
-
winAh.realFetch.call(win, request.url, init).then((res) => {
|
|
861
|
-
if (typeof request.response === "function") {
|
|
862
|
-
const response = {
|
|
863
|
-
finalUrl: res.url,
|
|
864
|
-
status: res.status,
|
|
865
|
-
responseHeaders: parseHeaders(res.headers),
|
|
866
|
-
};
|
|
867
|
-
fetchResponses.forEach(
|
|
868
|
-
(key) =>
|
|
869
|
-
(res[key] = function () {
|
|
870
|
-
if (key in response) return Promise.resolve(response[key]);
|
|
871
|
-
return resProto[key].call(this).then((val) => {
|
|
872
|
-
response[key] = val;
|
|
873
|
-
return req
|
|
874
|
-
.waitForResponseKeys(response)
|
|
875
|
-
.then(() => (key in response ? response[key] : val));
|
|
876
|
-
});
|
|
877
|
-
})
|
|
878
|
-
);
|
|
879
|
-
}
|
|
880
|
-
resolve(res);
|
|
881
|
-
}, reject);
|
|
882
|
-
})
|
|
883
|
-
.catch((err) => {
|
|
884
|
-
console.error(err);
|
|
885
|
-
resolve(winAh.realFetch.call(win, url, init));
|
|
886
|
-
});
|
|
887
|
-
});
|
|
888
|
-
}
|
|
889
|
-
function fakeFetchClone() {
|
|
890
|
-
const descriptors = Object.getOwnPropertyDescriptors(this);
|
|
891
|
-
const res = winAh.realFetchClone.call(this);
|
|
892
|
-
Object.defineProperties(res, descriptors);
|
|
893
|
-
return res;
|
|
894
|
-
}
|
|
895
|
-
winAh = win.__ajaxHooker = winAh || {
|
|
896
|
-
version,
|
|
897
|
-
fakeXHR,
|
|
898
|
-
fakeFetch,
|
|
899
|
-
fakeFetchClone,
|
|
900
|
-
realXHR: win.XMLHttpRequest,
|
|
901
|
-
realFetch: win.fetch,
|
|
902
|
-
realFetchClone: resProto.clone,
|
|
903
|
-
hookInsts: new Set(),
|
|
904
|
-
};
|
|
905
|
-
if (winAh.version !== version)
|
|
906
|
-
console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
|
|
907
|
-
win.XMLHttpRequest = winAh.fakeXHR;
|
|
908
|
-
win.fetch = winAh.fakeFetch;
|
|
909
|
-
resProto.clone = winAh.fakeFetchClone;
|
|
910
|
-
winAh.hookInsts.add(hookInst);
|
|
911
|
-
return {
|
|
912
|
-
hook: (fn) => hookInst.hookFns.push(fn),
|
|
913
|
-
filter: (arr) => {
|
|
914
|
-
if (Array.isArray(arr)) hookInst.filters = arr;
|
|
915
|
-
},
|
|
916
|
-
protect: () => {
|
|
917
|
-
readonly(win, "XMLHttpRequest", winAh.fakeXHR);
|
|
918
|
-
readonly(win, "fetch", winAh.fakeFetch);
|
|
919
|
-
readonly(resProto, "clone", winAh.fakeFetchClone);
|
|
920
|
-
},
|
|
921
|
-
unhook: () => {
|
|
922
|
-
winAh.hookInsts.delete(hookInst);
|
|
923
|
-
if (!winAh.hookInsts.size) {
|
|
924
|
-
writable(win, "XMLHttpRequest", winAh.realXHR);
|
|
925
|
-
writable(win, "fetch", winAh.realFetch);
|
|
926
|
-
writable(resProto, "clone", winAh.realFetchClone);
|
|
927
|
-
delete win.__ajaxHooker;
|
|
928
|
-
}
|
|
929
|
-
},
|
|
930
|
-
};
|
|
931
|
-
})();
|
|
945
|
+
})();
|
|
946
|
+
};
|
|
932
947
|
|
|
933
948
|
/**
|
|
934
949
|
*
|
|
@@ -3199,7 +3214,7 @@
|
|
|
3199
3214
|
|
|
3200
3215
|
let Utils$1 = class Utils {
|
|
3201
3216
|
/** 版本号 */
|
|
3202
|
-
version = "2024.5.
|
|
3217
|
+
version = "2024.5.25";
|
|
3203
3218
|
addStyle(cssText) {
|
|
3204
3219
|
if (typeof cssText !== "string") {
|
|
3205
3220
|
throw new Error("Utils.addStyle 参数cssText 必须为String类型");
|
|
@@ -3314,7 +3329,7 @@
|
|
|
3314
3329
|
* + 版本:1.4.1
|
|
3315
3330
|
* + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
|
|
3316
3331
|
*/
|
|
3317
|
-
ajaxHooker =
|
|
3332
|
+
ajaxHooker = AjaxHooker;
|
|
3318
3333
|
canvasClickByPosition(canvasElement, clientX = 0, clientY = 0, view = globalThis) {
|
|
3319
3334
|
if (!(canvasElement instanceof HTMLCanvasElement)) {
|
|
3320
3335
|
throw new Error("Utils.canvasClickByPosition 参数canvasElement必须是canvas元素");
|