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