@whitesev/utils 1.5.5 → 1.5.7
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 +259 -153
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +259 -153
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +259 -153
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +259 -153
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +259 -153
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +259 -153
- package/dist/index.umd.js.map +1 -1
- package/dist/src/AjaxHookerType.d.ts +147 -0
- package/dist/src/Utils.d.ts +1 -0
- package/package.json +1 -1
- package/src/{ajaxHooker/index.d.ts → AjaxHookerType.ts} +6 -4
- package/src/Utils.ts +1 -1
- package/src/ajaxHooker/ajaxHooker.js +261 -156
package/dist/index.iife.js
CHANGED
|
@@ -458,7 +458,6 @@ var Utils = (function () {
|
|
|
458
458
|
}
|
|
459
459
|
}
|
|
460
460
|
|
|
461
|
-
/// <reference path="./index.d.ts" />
|
|
462
461
|
// @name ajaxHooker
|
|
463
462
|
// @author cxxjackie
|
|
464
463
|
// @version 1.4.3
|
|
@@ -467,26 +466,43 @@ var Utils = (function () {
|
|
|
467
466
|
// @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
|
|
468
467
|
|
|
469
468
|
const AjaxHooker = function () {
|
|
470
|
-
return function() {
|
|
471
|
-
const version =
|
|
469
|
+
return (function () {
|
|
470
|
+
const version = "1.4.3";
|
|
472
471
|
const hookInst = {
|
|
473
472
|
hookFns: [],
|
|
474
|
-
filters: []
|
|
473
|
+
filters: [],
|
|
475
474
|
};
|
|
476
475
|
const win = window.unsafeWindow || document.defaultView || window;
|
|
477
476
|
let winAh = win.__ajaxHooker;
|
|
478
477
|
const resProto = win.Response.prototype;
|
|
479
|
-
const xhrResponses = [
|
|
480
|
-
const fetchResponses = [
|
|
481
|
-
const fetchInitProps = [
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
478
|
+
const xhrResponses = ["response", "responseText", "responseXML"];
|
|
479
|
+
const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
|
|
480
|
+
const fetchInitProps = [
|
|
481
|
+
"method",
|
|
482
|
+
"headers",
|
|
483
|
+
"body",
|
|
484
|
+
"mode",
|
|
485
|
+
"credentials",
|
|
486
|
+
"cache",
|
|
487
|
+
"redirect",
|
|
488
|
+
"referrer",
|
|
489
|
+
"referrerPolicy",
|
|
490
|
+
"integrity",
|
|
491
|
+
"keepalive",
|
|
492
|
+
"signal",
|
|
493
|
+
"priority",
|
|
494
|
+
];
|
|
495
|
+
const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
|
|
496
|
+
const getType = {}.toString.call.bind({}.toString);
|
|
485
497
|
const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
|
|
486
498
|
const emptyFn = () => {};
|
|
487
|
-
const errorFn = e => console.error(e);
|
|
499
|
+
const errorFn = (e) => console.error(e);
|
|
488
500
|
function isThenable(obj) {
|
|
489
|
-
return
|
|
501
|
+
return (
|
|
502
|
+
obj &&
|
|
503
|
+
["object", "function"].includes(typeof obj) &&
|
|
504
|
+
typeof obj.then === "function"
|
|
505
|
+
);
|
|
490
506
|
}
|
|
491
507
|
function catchError(fn, ...args) {
|
|
492
508
|
try {
|
|
@@ -502,7 +518,7 @@ var Utils = (function () {
|
|
|
502
518
|
configurable: true,
|
|
503
519
|
enumerable: true,
|
|
504
520
|
get: getter,
|
|
505
|
-
set: setter
|
|
521
|
+
set: setter,
|
|
506
522
|
});
|
|
507
523
|
}
|
|
508
524
|
function readonly(obj, prop, value = obj[prop]) {
|
|
@@ -513,27 +529,28 @@ var Utils = (function () {
|
|
|
513
529
|
configurable: true,
|
|
514
530
|
enumerable: true,
|
|
515
531
|
writable: true,
|
|
516
|
-
value: value
|
|
532
|
+
value: value,
|
|
517
533
|
});
|
|
518
534
|
}
|
|
519
535
|
function parseHeaders(obj) {
|
|
520
536
|
const headers = {};
|
|
521
537
|
switch (getType(obj)) {
|
|
522
|
-
case
|
|
538
|
+
case "[object String]":
|
|
523
539
|
for (const line of obj.trim().split(/[\r\n]+/)) {
|
|
524
540
|
const [header, value] = line.split(/\s*:\s*/);
|
|
525
541
|
if (!header) break;
|
|
526
542
|
const lheader = header.toLowerCase();
|
|
527
|
-
headers[lheader] =
|
|
543
|
+
headers[lheader] =
|
|
544
|
+
lheader in headers ? `${headers[lheader]}, ${value}` : value;
|
|
528
545
|
}
|
|
529
546
|
break;
|
|
530
|
-
case
|
|
547
|
+
case "[object Headers]":
|
|
531
548
|
for (const [key, val] of obj) {
|
|
532
549
|
headers[key] = val;
|
|
533
550
|
}
|
|
534
551
|
break;
|
|
535
|
-
case
|
|
536
|
-
return {...obj};
|
|
552
|
+
case "[object Object]":
|
|
553
|
+
return { ...obj };
|
|
537
554
|
}
|
|
538
555
|
return headers;
|
|
539
556
|
}
|
|
@@ -549,104 +566,142 @@ var Utils = (function () {
|
|
|
549
566
|
class AHRequest {
|
|
550
567
|
constructor(request) {
|
|
551
568
|
this.request = request;
|
|
552
|
-
this.requestClone = {...this.request};
|
|
569
|
+
this.requestClone = { ...this.request };
|
|
553
570
|
}
|
|
554
571
|
shouldFilter(filters) {
|
|
555
|
-
const {type, url, method, async} = this.request;
|
|
556
|
-
return
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
572
|
+
const { type, url, method, async } = this.request;
|
|
573
|
+
return (
|
|
574
|
+
filters.length &&
|
|
575
|
+
!filters.find((obj) => {
|
|
576
|
+
switch (true) {
|
|
577
|
+
case obj.type && obj.type !== type:
|
|
578
|
+
case getType(obj.url) === "[object String]" &&
|
|
579
|
+
!url.includes(obj.url):
|
|
580
|
+
case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
|
|
581
|
+
case obj.method &&
|
|
582
|
+
obj.method.toUpperCase() !== method.toUpperCase():
|
|
583
|
+
case "async" in obj && obj.async !== async:
|
|
584
|
+
return false;
|
|
585
|
+
}
|
|
586
|
+
return true;
|
|
587
|
+
})
|
|
588
|
+
);
|
|
567
589
|
}
|
|
568
590
|
waitForRequestKeys() {
|
|
569
|
-
const requestKeys = [
|
|
591
|
+
const requestKeys = ["url", "method", "abort", "headers", "data"];
|
|
570
592
|
if (!this.request.async) {
|
|
571
|
-
win.__ajaxHooker.hookInsts.forEach(({hookFns, filters}) => {
|
|
593
|
+
win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
|
|
572
594
|
if (this.shouldFilter(filters)) return;
|
|
573
|
-
hookFns.forEach(fn => {
|
|
574
|
-
if (getType(fn) ===
|
|
595
|
+
hookFns.forEach((fn) => {
|
|
596
|
+
if (getType(fn) === "[object Function]")
|
|
597
|
+
catchError(fn, this.request);
|
|
575
598
|
});
|
|
576
|
-
requestKeys.forEach(key => {
|
|
577
|
-
if (isThenable(this.request[key]))
|
|
599
|
+
requestKeys.forEach((key) => {
|
|
600
|
+
if (isThenable(this.request[key]))
|
|
601
|
+
this.request[key] = this.requestClone[key];
|
|
578
602
|
});
|
|
579
603
|
});
|
|
580
604
|
return new SyncThenable();
|
|
581
605
|
}
|
|
582
606
|
const promises = [];
|
|
583
|
-
win.__ajaxHooker.hookInsts.forEach(({hookFns, filters}) => {
|
|
607
|
+
win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
|
|
584
608
|
if (this.shouldFilter(filters)) return;
|
|
585
|
-
promises.push(
|
|
586
|
-
Promise.all(
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
609
|
+
promises.push(
|
|
610
|
+
Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
|
|
611
|
+
() =>
|
|
612
|
+
Promise.all(
|
|
613
|
+
requestKeys.map((key) =>
|
|
614
|
+
Promise.resolve(this.request[key]).then(
|
|
615
|
+
(val) => (this.request[key] = val),
|
|
616
|
+
() => (this.request[key] = this.requestClone[key])
|
|
617
|
+
)
|
|
618
|
+
)
|
|
619
|
+
)
|
|
620
|
+
)
|
|
621
|
+
);
|
|
591
622
|
});
|
|
592
623
|
return Promise.all(promises);
|
|
593
624
|
}
|
|
594
625
|
waitForResponseKeys(response) {
|
|
595
|
-
const responseKeys =
|
|
626
|
+
const responseKeys =
|
|
627
|
+
this.request.type === "xhr" ? xhrResponses : fetchResponses;
|
|
596
628
|
if (!this.request.async) {
|
|
597
|
-
if (getType(this.request.response) ===
|
|
629
|
+
if (getType(this.request.response) === "[object Function]") {
|
|
598
630
|
catchError(this.request.response, response);
|
|
599
|
-
responseKeys.forEach(key => {
|
|
600
|
-
if (
|
|
631
|
+
responseKeys.forEach((key) => {
|
|
632
|
+
if (
|
|
633
|
+
"get" in getDescriptor(response, key) ||
|
|
634
|
+
isThenable(response[key])
|
|
635
|
+
) {
|
|
601
636
|
delete response[key];
|
|
602
637
|
}
|
|
603
638
|
});
|
|
604
639
|
}
|
|
605
640
|
return new SyncThenable();
|
|
606
641
|
}
|
|
607
|
-
return Promise.resolve(
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
642
|
+
return Promise.resolve(
|
|
643
|
+
catchError(this.request.response, response)
|
|
644
|
+
).then(() =>
|
|
645
|
+
Promise.all(
|
|
646
|
+
responseKeys.map((key) => {
|
|
647
|
+
const descriptor = getDescriptor(response, key);
|
|
648
|
+
if (descriptor && "value" in descriptor) {
|
|
649
|
+
return Promise.resolve(descriptor.value).then(
|
|
650
|
+
(val) => (response[key] = val),
|
|
651
|
+
() => delete response[key]
|
|
652
|
+
);
|
|
653
|
+
} else {
|
|
654
|
+
delete response[key];
|
|
655
|
+
}
|
|
656
|
+
})
|
|
657
|
+
)
|
|
619
658
|
);
|
|
620
659
|
}
|
|
621
660
|
}
|
|
622
661
|
const proxyHandler = {
|
|
623
662
|
get(target, prop) {
|
|
624
663
|
const descriptor = getDescriptor(target, prop);
|
|
625
|
-
if (
|
|
664
|
+
if (
|
|
665
|
+
descriptor &&
|
|
666
|
+
!descriptor.configurable &&
|
|
667
|
+
!descriptor.writable &&
|
|
668
|
+
!descriptor.get
|
|
669
|
+
)
|
|
670
|
+
return target[prop];
|
|
626
671
|
const ah = target.__ajaxHooker;
|
|
627
672
|
if (ah && ah.proxyProps) {
|
|
628
673
|
if (prop in ah.proxyProps) {
|
|
629
674
|
const pDescriptor = ah.proxyProps[prop];
|
|
630
|
-
if (
|
|
631
|
-
if (typeof pDescriptor.value ===
|
|
675
|
+
if ("get" in pDescriptor) return pDescriptor.get();
|
|
676
|
+
if (typeof pDescriptor.value === "function")
|
|
677
|
+
return pDescriptor.value.bind(ah);
|
|
632
678
|
return pDescriptor.value;
|
|
633
679
|
}
|
|
634
|
-
if (typeof target[prop] ===
|
|
680
|
+
if (typeof target[prop] === "function")
|
|
681
|
+
return target[prop].bind(target);
|
|
635
682
|
}
|
|
636
683
|
return target[prop];
|
|
637
684
|
},
|
|
638
685
|
set(target, prop, value) {
|
|
639
686
|
const descriptor = getDescriptor(target, prop);
|
|
640
|
-
if (
|
|
687
|
+
if (
|
|
688
|
+
descriptor &&
|
|
689
|
+
!descriptor.configurable &&
|
|
690
|
+
!descriptor.writable &&
|
|
691
|
+
!descriptor.set
|
|
692
|
+
)
|
|
693
|
+
return true;
|
|
641
694
|
const ah = target.__ajaxHooker;
|
|
642
695
|
if (ah && ah.proxyProps && prop in ah.proxyProps) {
|
|
643
696
|
const pDescriptor = ah.proxyProps[prop];
|
|
644
|
-
pDescriptor.set
|
|
697
|
+
pDescriptor.set
|
|
698
|
+
? pDescriptor.set(value)
|
|
699
|
+
: (pDescriptor.value = value);
|
|
645
700
|
} else {
|
|
646
701
|
target[prop] = value;
|
|
647
702
|
}
|
|
648
703
|
return true;
|
|
649
|
-
}
|
|
704
|
+
},
|
|
650
705
|
};
|
|
651
706
|
class XhrHooker {
|
|
652
707
|
constructor(xhr) {
|
|
@@ -656,86 +711,113 @@ var Utils = (function () {
|
|
|
656
711
|
proxyXhr: new Proxy(xhr, proxyHandler),
|
|
657
712
|
resThenable: new SyncThenable(),
|
|
658
713
|
proxyProps: {},
|
|
659
|
-
proxyEvents: {}
|
|
714
|
+
proxyEvents: {},
|
|
660
715
|
});
|
|
661
|
-
xhr.addEventListener(
|
|
662
|
-
if (
|
|
716
|
+
xhr.addEventListener("readystatechange", (e) => {
|
|
717
|
+
if (
|
|
718
|
+
ah.proxyXhr.readyState === 4 &&
|
|
719
|
+
ah.request &&
|
|
720
|
+
typeof ah.request.response === "function"
|
|
721
|
+
) {
|
|
663
722
|
const response = {
|
|
664
723
|
finalUrl: ah.proxyXhr.responseURL,
|
|
665
724
|
status: ah.proxyXhr.status,
|
|
666
|
-
responseHeaders: parseHeaders(
|
|
725
|
+
responseHeaders: parseHeaders(
|
|
726
|
+
ah.proxyXhr.getAllResponseHeaders()
|
|
727
|
+
),
|
|
667
728
|
};
|
|
668
729
|
const tempValues = {};
|
|
669
730
|
for (const key of xhrResponses) {
|
|
670
731
|
try {
|
|
671
732
|
tempValues[key] = ah.originalXhr[key];
|
|
672
733
|
} catch (err) {}
|
|
673
|
-
defineProp(
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
734
|
+
defineProp(
|
|
735
|
+
response,
|
|
736
|
+
key,
|
|
737
|
+
() => {
|
|
738
|
+
return (response[key] = tempValues[key]);
|
|
739
|
+
},
|
|
740
|
+
(val) => {
|
|
741
|
+
delete response[key];
|
|
742
|
+
response[key] = val;
|
|
743
|
+
}
|
|
744
|
+
);
|
|
679
745
|
}
|
|
680
|
-
ah.resThenable = new AHRequest(ah.request)
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
746
|
+
ah.resThenable = new AHRequest(ah.request)
|
|
747
|
+
.waitForResponseKeys(response)
|
|
748
|
+
.then(() => {
|
|
749
|
+
for (const key of xhrResponses) {
|
|
750
|
+
ah.proxyProps[key] = {
|
|
751
|
+
get: () => {
|
|
752
|
+
if (!(key in response)) response[key] = tempValues[key];
|
|
753
|
+
return response[key];
|
|
754
|
+
},
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
});
|
|
688
758
|
}
|
|
689
759
|
ah.dispatchEvent(e);
|
|
690
760
|
});
|
|
691
|
-
xhr.addEventListener(
|
|
692
|
-
xhr.addEventListener(
|
|
761
|
+
xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
|
|
762
|
+
xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
|
|
693
763
|
for (const evt of xhrAsyncEvents) {
|
|
694
|
-
const onEvt =
|
|
764
|
+
const onEvt = "on" + evt;
|
|
695
765
|
ah.proxyProps[onEvt] = {
|
|
696
766
|
get: () => ah.proxyEvents[onEvt] || null,
|
|
697
|
-
set: val => ah.addEvent(onEvt, val)
|
|
767
|
+
set: (val) => ah.addEvent(onEvt, val),
|
|
698
768
|
};
|
|
699
769
|
}
|
|
700
|
-
for (const method of [
|
|
701
|
-
|
|
770
|
+
for (const method of [
|
|
771
|
+
"setRequestHeader",
|
|
772
|
+
"addEventListener",
|
|
773
|
+
"removeEventListener",
|
|
774
|
+
"open",
|
|
775
|
+
"send",
|
|
776
|
+
]) {
|
|
777
|
+
ah.proxyProps[method] = { value: ah[method] };
|
|
702
778
|
}
|
|
703
779
|
}
|
|
704
780
|
toJSON() {} // Converting circular structure to JSON
|
|
705
781
|
addEvent(type, event) {
|
|
706
|
-
if (type.startsWith(
|
|
707
|
-
this.proxyEvents[type] = typeof event ===
|
|
782
|
+
if (type.startsWith("on")) {
|
|
783
|
+
this.proxyEvents[type] = typeof event === "function" ? event : null;
|
|
708
784
|
} else {
|
|
709
|
-
if (typeof event ===
|
|
710
|
-
|
|
785
|
+
if (typeof event === "object" && event !== null)
|
|
786
|
+
event = event.handleEvent;
|
|
787
|
+
if (typeof event !== "function") return;
|
|
711
788
|
this.proxyEvents[type] = this.proxyEvents[type] || new Set();
|
|
712
789
|
this.proxyEvents[type].add(event);
|
|
713
790
|
}
|
|
714
791
|
}
|
|
715
792
|
removeEvent(type, event) {
|
|
716
|
-
if (type.startsWith(
|
|
793
|
+
if (type.startsWith("on")) {
|
|
717
794
|
this.proxyEvents[type] = null;
|
|
718
795
|
} else {
|
|
719
|
-
if (typeof event ===
|
|
796
|
+
if (typeof event === "object" && event !== null)
|
|
797
|
+
event = event.handleEvent;
|
|
720
798
|
this.proxyEvents[type] && this.proxyEvents[type].delete(event);
|
|
721
799
|
}
|
|
722
800
|
}
|
|
723
801
|
dispatchEvent(e) {
|
|
724
802
|
e.stopImmediatePropagation = stopImmediatePropagation;
|
|
725
|
-
defineProp(e,
|
|
726
|
-
defineProp(e,
|
|
727
|
-
this.proxyEvents[e.type] &&
|
|
728
|
-
this.
|
|
729
|
-
|
|
803
|
+
defineProp(e, "target", () => this.proxyXhr);
|
|
804
|
+
defineProp(e, "currentTarget", () => this.proxyXhr);
|
|
805
|
+
this.proxyEvents[e.type] &&
|
|
806
|
+
this.proxyEvents[e.type].forEach((fn) => {
|
|
807
|
+
this.resThenable.then(
|
|
808
|
+
() => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e)
|
|
809
|
+
);
|
|
810
|
+
});
|
|
730
811
|
if (e.ajaxHooker_isStopped) return;
|
|
731
|
-
const onEvent = this.proxyEvents[
|
|
812
|
+
const onEvent = this.proxyEvents["on" + e.type];
|
|
732
813
|
onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
|
|
733
814
|
}
|
|
734
815
|
setRequestHeader(header, value) {
|
|
735
816
|
this.originalXhr.setRequestHeader(header, value);
|
|
736
817
|
if (!this.request) return;
|
|
737
818
|
const headers = this.request.headers;
|
|
738
|
-
headers[header] =
|
|
819
|
+
headers[header] =
|
|
820
|
+
header in headers ? `${headers[header]}, ${value}` : value;
|
|
739
821
|
}
|
|
740
822
|
addEventListener(...args) {
|
|
741
823
|
if (xhrAsyncEvents.includes(args[0])) {
|
|
@@ -753,18 +835,24 @@ var Utils = (function () {
|
|
|
753
835
|
}
|
|
754
836
|
open(method, url, async = true, ...args) {
|
|
755
837
|
this.request = {
|
|
756
|
-
type:
|
|
838
|
+
type: "xhr",
|
|
757
839
|
url: url.toString(),
|
|
758
840
|
method: method.toUpperCase(),
|
|
759
841
|
abort: false,
|
|
760
842
|
headers: {},
|
|
761
843
|
data: null,
|
|
762
844
|
response: null,
|
|
763
|
-
async: !!async
|
|
845
|
+
async: !!async,
|
|
764
846
|
};
|
|
765
847
|
this.openArgs = args;
|
|
766
848
|
this.resThenable = new SyncThenable();
|
|
767
|
-
[
|
|
849
|
+
[
|
|
850
|
+
"responseURL",
|
|
851
|
+
"readyState",
|
|
852
|
+
"status",
|
|
853
|
+
"statusText",
|
|
854
|
+
...xhrResponses,
|
|
855
|
+
].forEach((key) => {
|
|
768
856
|
delete this.proxyProps[key];
|
|
769
857
|
});
|
|
770
858
|
return this.originalXhr.open(method, url, async, ...args);
|
|
@@ -777,17 +865,24 @@ var Utils = (function () {
|
|
|
777
865
|
request.data = data;
|
|
778
866
|
new AHRequest(request).waitForRequestKeys().then(() => {
|
|
779
867
|
if (request.abort) {
|
|
780
|
-
if (typeof request.response ===
|
|
868
|
+
if (typeof request.response === "function") {
|
|
781
869
|
Object.assign(ah.proxyProps, {
|
|
782
|
-
responseURL: {value: request.url},
|
|
783
|
-
readyState: {value: 4},
|
|
784
|
-
status: {value: 200},
|
|
785
|
-
statusText: {value:
|
|
870
|
+
responseURL: { value: request.url },
|
|
871
|
+
readyState: { value: 4 },
|
|
872
|
+
status: { value: 200 },
|
|
873
|
+
statusText: { value: "OK" },
|
|
786
874
|
});
|
|
787
|
-
xhrAsyncEvents.forEach(evt =>
|
|
875
|
+
xhrAsyncEvents.forEach((evt) =>
|
|
876
|
+
xhr.dispatchEvent(new Event(evt))
|
|
877
|
+
);
|
|
788
878
|
}
|
|
789
879
|
} else {
|
|
790
|
-
xhr.open(
|
|
880
|
+
xhr.open(
|
|
881
|
+
request.method,
|
|
882
|
+
request.url,
|
|
883
|
+
request.async,
|
|
884
|
+
...ah.openArgs
|
|
885
|
+
);
|
|
791
886
|
for (const header in request.headers) {
|
|
792
887
|
xhr.setRequestHeader(header, request.headers[header]);
|
|
793
888
|
}
|
|
@@ -798,79 +893,87 @@ var Utils = (function () {
|
|
|
798
893
|
}
|
|
799
894
|
function fakeXHR() {
|
|
800
895
|
const xhr = new winAh.realXHR();
|
|
801
|
-
if (
|
|
896
|
+
if ("__ajaxHooker" in xhr)
|
|
897
|
+
console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
|
|
802
898
|
xhr.__ajaxHooker = new XhrHooker(xhr);
|
|
803
899
|
return xhr.__ajaxHooker.proxyXhr;
|
|
804
900
|
}
|
|
805
901
|
fakeXHR.prototype = win.XMLHttpRequest.prototype;
|
|
806
|
-
Object.keys(win.XMLHttpRequest).forEach(
|
|
902
|
+
Object.keys(win.XMLHttpRequest).forEach(
|
|
903
|
+
(key) => (fakeXHR[key] = win.XMLHttpRequest[key])
|
|
904
|
+
);
|
|
807
905
|
function fakeFetch(url, options = {}) {
|
|
808
906
|
if (!url) return winAh.realFetch.call(win, url, options);
|
|
809
907
|
return new Promise(async (resolve, reject) => {
|
|
810
908
|
const init = {};
|
|
811
|
-
if (getType(url) ===
|
|
909
|
+
if (getType(url) === "[object Request]") {
|
|
812
910
|
for (const prop of fetchInitProps) init[prop] = url[prop];
|
|
813
911
|
if (url.body) init.body = await url.arrayBuffer();
|
|
814
912
|
url = url.url;
|
|
815
913
|
}
|
|
816
914
|
url = url.toString();
|
|
817
915
|
Object.assign(init, options);
|
|
818
|
-
init.method = init.method ||
|
|
916
|
+
init.method = init.method || "GET";
|
|
819
917
|
init.headers = init.headers || {};
|
|
820
918
|
const request = {
|
|
821
|
-
type:
|
|
919
|
+
type: "fetch",
|
|
822
920
|
url: url,
|
|
823
921
|
method: init.method.toUpperCase(),
|
|
824
922
|
abort: false,
|
|
825
923
|
headers: parseHeaders(init.headers),
|
|
826
924
|
data: init.body,
|
|
827
925
|
response: null,
|
|
828
|
-
async: true
|
|
926
|
+
async: true,
|
|
829
927
|
};
|
|
830
928
|
const req = new AHRequest(request);
|
|
831
929
|
await req.waitForRequestKeys();
|
|
832
930
|
if (request.abort) {
|
|
833
|
-
if (typeof request.response ===
|
|
931
|
+
if (typeof request.response === "function") {
|
|
834
932
|
const response = {
|
|
835
933
|
finalUrl: request.url,
|
|
836
934
|
status: 200,
|
|
837
|
-
responseHeaders: {}
|
|
935
|
+
responseHeaders: {},
|
|
838
936
|
};
|
|
839
937
|
await req.waitForResponseKeys(response);
|
|
840
|
-
const key = fetchResponses.find(k => k in response);
|
|
938
|
+
const key = fetchResponses.find((k) => k in response);
|
|
841
939
|
let val = response[key];
|
|
842
|
-
if (key ===
|
|
940
|
+
if (key === "json" && typeof val === "object") {
|
|
843
941
|
val = catchError(JSON.stringify.bind(JSON), val);
|
|
844
942
|
}
|
|
845
943
|
const res = new Response(val, {
|
|
846
944
|
status: 200,
|
|
847
|
-
statusText:
|
|
945
|
+
statusText: "OK",
|
|
848
946
|
});
|
|
849
|
-
defineProp(res,
|
|
850
|
-
defineProp(res,
|
|
947
|
+
defineProp(res, "type", () => "basic");
|
|
948
|
+
defineProp(res, "url", () => request.url);
|
|
851
949
|
resolve(res);
|
|
852
950
|
} else {
|
|
853
|
-
reject(new DOMException(
|
|
951
|
+
reject(new DOMException("aborted", "AbortError"));
|
|
854
952
|
}
|
|
855
953
|
return;
|
|
856
954
|
}
|
|
857
955
|
init.method = request.method;
|
|
858
956
|
init.headers = request.headers;
|
|
859
957
|
init.body = request.data;
|
|
860
|
-
winAh.realFetch.call(win, request.url, init).then(res => {
|
|
861
|
-
if (typeof request.response ===
|
|
958
|
+
winAh.realFetch.call(win, request.url, init).then((res) => {
|
|
959
|
+
if (typeof request.response === "function") {
|
|
862
960
|
const response = {
|
|
863
961
|
finalUrl: res.url,
|
|
864
962
|
status: res.status,
|
|
865
|
-
responseHeaders: parseHeaders(res.headers)
|
|
963
|
+
responseHeaders: parseHeaders(res.headers),
|
|
866
964
|
};
|
|
867
|
-
fetchResponses.forEach(
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
965
|
+
fetchResponses.forEach(
|
|
966
|
+
(key) =>
|
|
967
|
+
(res[key] = function () {
|
|
968
|
+
if (key in response) return Promise.resolve(response[key]);
|
|
969
|
+
return resProto[key].call(this).then((val) => {
|
|
970
|
+
response[key] = val;
|
|
971
|
+
return req
|
|
972
|
+
.waitForResponseKeys(response)
|
|
973
|
+
.then(() => (key in response ? response[key] : val));
|
|
974
|
+
});
|
|
975
|
+
})
|
|
976
|
+
);
|
|
874
977
|
}
|
|
875
978
|
resolve(res);
|
|
876
979
|
}, reject);
|
|
@@ -883,38 +986,42 @@ var Utils = (function () {
|
|
|
883
986
|
return res;
|
|
884
987
|
}
|
|
885
988
|
winAh = win.__ajaxHooker = winAh || {
|
|
886
|
-
version,
|
|
989
|
+
version,
|
|
990
|
+
fakeXHR,
|
|
991
|
+
fakeFetch,
|
|
992
|
+
fakeFetchClone,
|
|
887
993
|
realXHR: win.XMLHttpRequest,
|
|
888
994
|
realFetch: win.fetch,
|
|
889
995
|
realFetchClone: resProto.clone,
|
|
890
|
-
hookInsts: new Set()
|
|
996
|
+
hookInsts: new Set(),
|
|
891
997
|
};
|
|
892
|
-
if (winAh.version !== version)
|
|
998
|
+
if (winAh.version !== version)
|
|
999
|
+
console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
|
|
893
1000
|
win.XMLHttpRequest = winAh.fakeXHR;
|
|
894
1001
|
win.fetch = winAh.fakeFetch;
|
|
895
1002
|
resProto.clone = winAh.fakeFetchClone;
|
|
896
1003
|
winAh.hookInsts.add(hookInst);
|
|
897
1004
|
return {
|
|
898
|
-
hook: fn => hookInst.hookFns.push(fn),
|
|
899
|
-
filter: arr => {
|
|
1005
|
+
hook: (fn) => hookInst.hookFns.push(fn),
|
|
1006
|
+
filter: (arr) => {
|
|
900
1007
|
if (Array.isArray(arr)) hookInst.filters = arr;
|
|
901
1008
|
},
|
|
902
1009
|
protect: () => {
|
|
903
|
-
readonly(win,
|
|
904
|
-
readonly(win,
|
|
905
|
-
readonly(resProto,
|
|
1010
|
+
readonly(win, "XMLHttpRequest", winAh.fakeXHR);
|
|
1011
|
+
readonly(win, "fetch", winAh.fakeFetch);
|
|
1012
|
+
readonly(resProto, "clone", winAh.fakeFetchClone);
|
|
906
1013
|
},
|
|
907
1014
|
unhook: () => {
|
|
908
1015
|
winAh.hookInsts.delete(hookInst);
|
|
909
1016
|
if (!winAh.hookInsts.size) {
|
|
910
|
-
writable(win,
|
|
911
|
-
writable(win,
|
|
912
|
-
writable(resProto,
|
|
1017
|
+
writable(win, "XMLHttpRequest", winAh.realXHR);
|
|
1018
|
+
writable(win, "fetch", winAh.realFetch);
|
|
1019
|
+
writable(resProto, "clone", winAh.realFetchClone);
|
|
913
1020
|
delete win.__ajaxHooker;
|
|
914
1021
|
}
|
|
915
|
-
}
|
|
1022
|
+
},
|
|
916
1023
|
};
|
|
917
|
-
}();
|
|
1024
|
+
})();
|
|
918
1025
|
};
|
|
919
1026
|
|
|
920
1027
|
class GMMenu {
|
|
@@ -3284,7 +3391,6 @@ var Utils = (function () {
|
|
|
3284
3391
|
}
|
|
3285
3392
|
}
|
|
3286
3393
|
|
|
3287
|
-
/// <reference path="./ajaxHooker/index.d.ts" />
|
|
3288
3394
|
class Utils {
|
|
3289
3395
|
constructor(option) {
|
|
3290
3396
|
UtilsCore.init(option);
|