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