@whitesev/utils 1.4.1 → 1.4.3

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.
@@ -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.1
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 (function () {
469
- const version = "1.4.1";
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 = ["response", "responseText", "responseXML"];
478
- const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
479
- const fetchInitProps = [
480
- "method",
481
- "headers",
482
- "body",
483
- "mode",
484
- "credentials",
485
- "cache",
486
- "redirect",
487
- "referrer",
488
- "referrerPolicy",
489
- "integrity",
490
- "keepalive",
491
- "signal",
492
- "priority",
493
- ];
494
- const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
495
- const getType = {}.toString.call.bind({}.toString);
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 = (e) => console.error(e);
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 "[object String]":
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 "[object Headers]":
529
+ case '[object Headers]':
547
530
  for (const [key, val] of obj) {
548
531
  headers[key] = val;
549
532
  }
550
533
  break;
551
- case "[object Object]":
552
- return { ...obj };
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 = { ...this.request };
551
+ this.requestClone = {...this.request};
569
552
  }
570
553
  shouldFilter(filters) {
571
- const { type, url, method, async } = this.request;
572
- return (
573
- filters.length &&
574
- !filters.find((obj) => {
575
- switch (true) {
576
- case obj.type && obj.type !== type:
577
- case getType(obj.url) === "[object String]" &&
578
- !url.includes(obj.url):
579
- case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
580
- case obj.method &&
581
- obj.method.toUpperCase() !== method.toUpperCase():
582
- case "async" in obj && obj.async !== async:
583
- return false;
584
- }
585
- return true;
586
- })
587
- );
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 = ["url", "method", "abort", "headers", "data"];
568
+ const requestKeys = ['url', 'method', 'abort', 'headers', 'data'];
591
569
  if (!this.request.async) {
592
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
570
+ win.__ajaxHooker.hookInsts.forEach(({hookFns, filters}) => {
593
571
  if (this.shouldFilter(filters)) return;
594
- hookFns.forEach((fn) => {
595
- if (getType(fn) === "[object Function]")
596
- catchError(fn, this.request);
572
+ hookFns.forEach(fn => {
573
+ if (getType(fn) === '[object Function]') catchError(fn, this.request);
597
574
  });
598
- requestKeys.forEach((key) => {
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(({ hookFns, filters }) => {
582
+ win.__ajaxHooker.hookInsts.forEach(({hookFns, filters}) => {
607
583
  if (this.shouldFilter(filters)) return;
608
- promises.push(
609
- Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
610
- () =>
611
- Promise.all(
612
- requestKeys.map((key) =>
613
- Promise.resolve(this.request[key]).then(
614
- (val) => (this.request[key] = val),
615
- () => (this.request[key] = this.requestClone[key])
616
- )
617
- )
618
- )
619
- )
620
- );
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) === "[object Function]") {
596
+ if (getType(this.request.response) === '[object Function]') {
629
597
  catchError(this.request.response, response);
630
- responseKeys.forEach((key) => {
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
- catchError(this.request.response, response)
643
- ).then(() =>
644
- Promise.all(
645
- responseKeys.map((key) => {
646
- const descriptor = getDescriptor(response, key);
647
- if (descriptor && "value" in descriptor) {
648
- return Promise.resolve(descriptor.value).then(
649
- (val) => (response[key] = val),
650
- () => delete response[key]
651
- );
652
- } else {
653
- delete response[key];
654
- }
655
- })
656
- )
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 ("get" in pDescriptor) return pDescriptor.get();
675
- if (typeof pDescriptor.value === "function")
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] === "function")
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("readystatechange", (e) => {
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
- key,
736
- () => {
737
- return (response[key] = tempValues[key]);
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("load", (e) => ah.dispatchEvent(e));
761
- xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
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 = "on" + evt;
693
+ const onEvt = 'on' + evt;
764
694
  ah.proxyProps[onEvt] = {
765
695
  get: () => ah.proxyEvents[onEvt] || null,
766
- set: (val) => ah.addEvent(onEvt, val),
696
+ set: val => ah.addEvent(onEvt, val)
767
697
  };
768
698
  }
769
- for (const method of [
770
- "setRequestHeader",
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("on")) {
782
- this.proxyEvents[type] = typeof event === "function" ? event : null;
705
+ if (type.startsWith('on')) {
706
+ this.proxyEvents[type] = typeof event === 'function' ? event : null;
783
707
  } else {
784
- if (typeof event === "object" && event !== null)
785
- event = event.handleEvent;
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("on")) {
715
+ if (type.startsWith('on')) {
793
716
  this.proxyEvents[type] = null;
794
717
  } else {
795
- if (typeof event === "object" && event !== null)
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, "target", () => this.proxyXhr);
803
- this.proxyEvents[e.type] &&
804
- this.proxyEvents[e.type].forEach((fn) => {
805
- this.resThenable.then(
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["on" + e.type];
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: "xhr",
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 === "function") {
778
+ if (typeof request.response === 'function') {
867
779
  Object.assign(ah.proxyProps, {
868
- responseURL: { value: request.url },
869
- readyState: { value: 4 },
870
- status: { value: 200 },
871
- statusText: { value: "OK" },
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 ("__ajaxHooker" in xhr)
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
- const init = {};
906
- if (getType(url) === "[object Request]") {
907
- for (const prop of fetchInitProps) init[prop] = url[prop];
908
- url = url.url;
909
- }
910
- url = url.toString();
911
- Object.assign(init, options);
912
- init.method = init.method || "GET";
913
- init.headers = init.headers || {};
914
- const request = {
915
- type: "fetch",
916
- url: url,
917
- method: init.method.toUpperCase(),
918
- abort: false,
919
- headers: parseHeaders(init.headers),
920
- data: init.body,
921
- response: null,
922
- async: true,
923
- };
924
- const req = new AHRequest(request);
925
- return new Promise((resolve, reject) => {
926
- req
927
- .waitForRequestKeys()
928
- .then(() => {
929
- if (request.abort) {
930
- if (typeof request.response === "function") {
931
- const response = {
932
- finalUrl: request.url,
933
- status: 200,
934
- responseHeaders: {},
935
- };
936
- req.waitForResponseKeys(response).then(() => {
937
- const key = fetchResponses.find((k) => k in response);
938
- let val = response[key];
939
- if (key === "json" && typeof val === "object") {
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
- init.method = request.method;
956
- init.headers = request.headers;
957
- init.body = request.data;
958
- winAh.realFetch.call(win, request.url, init).then((res) => {
959
- if (typeof request.response === "function") {
960
- const response = {
961
- finalUrl: res.url,
962
- status: res.status,
963
- responseHeaders: parseHeaders(res.headers),
964
- };
965
- fetchResponses.forEach(
966
- (key) =>
967
- (res[key] = function () {
968
- if (key in response)
969
- return Promise.resolve(response[key]);
970
- return resProto[key].call(this).then((val) => {
971
- response[key] = val;
972
- return req
973
- .waitForResponseKeys(response)
974
- .then(() => (key in response ? response[key] : val));
975
- });
976
- })
977
- );
978
- }
979
- resolve(res);
980
- }, reject);
981
- })
982
- .catch((err) => {
983
- console.error(err);
984
- resolve(winAh.realFetch.call(win, url, init));
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: (fn) => hookInst.hookFns.push(fn),
1012
- filter: (arr) => {
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, "XMLHttpRequest", winAh.fakeXHR);
1017
- readonly(win, "fetch", winAh.fakeFetch);
1018
- readonly(resProto, "clone", winAh.fakeFetchClone);
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, "XMLHttpRequest", winAh.realXHR);
1024
- writable(win, "fetch", winAh.realFetch);
1025
- writable(resProto, "clone", winAh.realFetchClone);
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.5";
3063
+ version = "2024.6.11";
3179
3064
  addStyle(cssText) {
3180
3065
  if (typeof cssText !== "string") {
3181
3066
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -4774,6 +4659,7 @@ var Utils = (function () {
4774
4659
  */
4775
4660
  characterDataOldValue: void 0,
4776
4661
  },
4662
+ immediate: false,
4777
4663
  };
4778
4664
  observer_config = UtilsContext.assign(default_obverser_config, observer_config);
4779
4665
  let windowMutationObserver = window.MutationObserver ||
@@ -4802,6 +4688,10 @@ var Utils = (function () {
4802
4688
  /* 未知 */
4803
4689
  console.error("Utils.mutationObserver 未知参数", arguments);
4804
4690
  }
4691
+ if (observer_config.immediate) {
4692
+ /* 主动触发一次 */
4693
+ observer_config.callback([], mutationObserver);
4694
+ }
4805
4695
  return mutationObserver;
4806
4696
  }
4807
4697
  /**