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