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