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