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