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