@whitesev/utils 1.5.6 → 1.5.8

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