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