@whitesev/utils 1.0.2 → 1.0.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.
package/dist/index.cjs.js CHANGED
@@ -375,181 +375,183 @@ class UtilsGMCookie {
375
375
  // @version 1.4.1
376
376
  // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
377
377
 
378
- var ajaxHooker = (function () {
379
- const version = "1.4.1";
380
- const hookInst = {
381
- hookFns: [],
382
- filters: [],
383
- };
384
- const win = window.unsafeWindow || document.defaultView || window;
385
- let winAh = win.__ajaxHooker;
386
- const resProto = win.Response.prototype;
387
- const xhrResponses = ["response", "responseText", "responseXML"];
388
- const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
389
- const fetchInitProps = [
390
- "method",
391
- "headers",
392
- "body",
393
- "mode",
394
- "credentials",
395
- "cache",
396
- "redirect",
397
- "referrer",
398
- "referrerPolicy",
399
- "integrity",
400
- "keepalive",
401
- "signal",
402
- "priority",
403
- ];
404
- const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
405
- const getType = {}.toString.call.bind({}.toString);
406
- const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
407
- const emptyFn = () => {};
408
- const errorFn = (e) => console.error(e);
409
- function isThenable(obj) {
410
- return (
411
- obj &&
412
- ["object", "function"].includes(typeof obj) &&
413
- typeof obj.then === "function"
414
- );
415
- }
416
- function catchError(fn, ...args) {
417
- try {
418
- const result = fn(...args);
419
- if (isThenable(result)) return result.then(null, errorFn);
420
- return result;
421
- } catch (err) {
422
- console.error(err);
378
+ const AjaxHooker = function () {
379
+ return (function () {
380
+ const version = "1.4.1";
381
+ const hookInst = {
382
+ hookFns: [],
383
+ filters: [],
384
+ };
385
+ const win = window.unsafeWindow || document.defaultView || window;
386
+ let winAh = win.__ajaxHooker;
387
+ const resProto = win.Response.prototype;
388
+ const xhrResponses = ["response", "responseText", "responseXML"];
389
+ const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
390
+ const fetchInitProps = [
391
+ "method",
392
+ "headers",
393
+ "body",
394
+ "mode",
395
+ "credentials",
396
+ "cache",
397
+ "redirect",
398
+ "referrer",
399
+ "referrerPolicy",
400
+ "integrity",
401
+ "keepalive",
402
+ "signal",
403
+ "priority",
404
+ ];
405
+ const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
406
+ const getType = {}.toString.call.bind({}.toString);
407
+ const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
408
+ const emptyFn = () => {};
409
+ const errorFn = (e) => console.error(e);
410
+ function isThenable(obj) {
411
+ return (
412
+ obj &&
413
+ ["object", "function"].includes(typeof obj) &&
414
+ typeof obj.then === "function"
415
+ );
423
416
  }
424
- }
425
- function defineProp(obj, prop, getter, setter) {
426
- Object.defineProperty(obj, prop, {
427
- configurable: true,
428
- enumerable: true,
429
- get: getter,
430
- set: setter,
431
- });
432
- }
433
- function readonly(obj, prop, value = obj[prop]) {
434
- defineProp(obj, prop, () => value, emptyFn);
435
- }
436
- function writable(obj, prop, value = obj[prop]) {
437
- Object.defineProperty(obj, prop, {
438
- configurable: true,
439
- enumerable: true,
440
- writable: true,
441
- value: value,
442
- });
443
- }
444
- function parseHeaders(obj) {
445
- const headers = {};
446
- switch (getType(obj)) {
447
- case "[object String]":
448
- for (const line of obj.trim().split(/[\r\n]+/)) {
449
- const [header, value] = line.split(/\s*:\s*/);
450
- if (!header) break;
451
- const lheader = header.toLowerCase();
452
- headers[lheader] =
453
- lheader in headers ? `${headers[lheader]}, ${value}` : value;
454
- }
455
- break;
456
- case "[object Headers]":
457
- for (const [key, val] of obj) {
458
- headers[key] = val;
459
- }
460
- break;
461
- case "[object Object]":
462
- return { ...obj };
417
+ function catchError(fn, ...args) {
418
+ try {
419
+ const result = fn(...args);
420
+ if (isThenable(result)) return result.then(null, errorFn);
421
+ return result;
422
+ } catch (err) {
423
+ console.error(err);
424
+ }
463
425
  }
464
- return headers;
465
- }
466
- function stopImmediatePropagation() {
467
- this.ajaxHooker_isStopped = true;
468
- }
469
- class SyncThenable {
470
- then(fn) {
471
- fn && fn();
472
- return new SyncThenable();
426
+ function defineProp(obj, prop, getter, setter) {
427
+ Object.defineProperty(obj, prop, {
428
+ configurable: true,
429
+ enumerable: true,
430
+ get: getter,
431
+ set: setter,
432
+ });
473
433
  }
474
- }
475
- class AHRequest {
476
- constructor(request) {
477
- this.request = request;
478
- this.requestClone = { ...this.request };
434
+ function readonly(obj, prop, value = obj[prop]) {
435
+ defineProp(obj, prop, () => value, emptyFn);
479
436
  }
480
- shouldFilter(filters) {
481
- const { type, url, method, async } = this.request;
482
- return (
483
- filters.length &&
484
- !filters.find((obj) => {
485
- switch (true) {
486
- case obj.type && obj.type !== type:
487
- case getType(obj.url) === "[object String]" &&
488
- !url.includes(obj.url):
489
- case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
490
- case obj.method &&
491
- obj.method.toUpperCase() !== method.toUpperCase():
492
- case "async" in obj && obj.async !== async:
493
- return false;
437
+ function writable(obj, prop, value = obj[prop]) {
438
+ Object.defineProperty(obj, prop, {
439
+ configurable: true,
440
+ enumerable: true,
441
+ writable: true,
442
+ value: value,
443
+ });
444
+ }
445
+ function parseHeaders(obj) {
446
+ const headers = {};
447
+ switch (getType(obj)) {
448
+ case "[object String]":
449
+ for (const line of obj.trim().split(/[\r\n]+/)) {
450
+ const [header, value] = line.split(/\s*:\s*/);
451
+ if (!header) break;
452
+ const lheader = header.toLowerCase();
453
+ headers[lheader] =
454
+ lheader in headers ? `${headers[lheader]}, ${value}` : value;
494
455
  }
495
- return true;
496
- })
497
- );
456
+ break;
457
+ case "[object Headers]":
458
+ for (const [key, val] of obj) {
459
+ headers[key] = val;
460
+ }
461
+ break;
462
+ case "[object Object]":
463
+ return { ...obj };
464
+ }
465
+ return headers;
498
466
  }
499
- waitForRequestKeys() {
500
- const requestKeys = ["url", "method", "abort", "headers", "data"];
501
- if (!this.request.async) {
502
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
503
- if (this.shouldFilter(filters)) return;
504
- hookFns.forEach((fn) => {
505
- if (getType(fn) === "[object Function]")
506
- catchError(fn, this.request);
507
- });
508
- requestKeys.forEach((key) => {
509
- if (isThenable(this.request[key]))
510
- this.request[key] = this.requestClone[key];
511
- });
512
- });
467
+ function stopImmediatePropagation() {
468
+ this.ajaxHooker_isStopped = true;
469
+ }
470
+ class SyncThenable {
471
+ then(fn) {
472
+ fn && fn();
513
473
  return new SyncThenable();
514
474
  }
515
- const promises = [];
516
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
517
- if (this.shouldFilter(filters)) return;
518
- promises.push(
519
- Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
520
- () =>
521
- Promise.all(
522
- requestKeys.map((key) =>
523
- Promise.resolve(this.request[key]).then(
524
- (val) => (this.request[key] = val),
525
- () => (this.request[key] = this.requestClone[key])
526
- )
527
- )
528
- )
529
- )
530
- );
531
- });
532
- return Promise.all(promises);
533
475
  }
534
- waitForResponseKeys(response) {
535
- const responseKeys =
536
- this.request.type === "xhr" ? xhrResponses : fetchResponses;
537
- if (!this.request.async) {
538
- if (getType(this.request.response) === "[object Function]") {
539
- catchError(this.request.response, response);
540
- responseKeys.forEach((key) => {
541
- if (
542
- "get" in getDescriptor(response, key) ||
543
- isThenable(response[key])
544
- ) {
545
- delete response[key];
476
+ class AHRequest {
477
+ constructor(request) {
478
+ this.request = request;
479
+ this.requestClone = { ...this.request };
480
+ }
481
+ shouldFilter(filters) {
482
+ const { type, url, method, async } = this.request;
483
+ return (
484
+ filters.length &&
485
+ !filters.find((obj) => {
486
+ switch (true) {
487
+ case obj.type && obj.type !== type:
488
+ case getType(obj.url) === "[object String]" &&
489
+ !url.includes(obj.url):
490
+ case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
491
+ case obj.method &&
492
+ obj.method.toUpperCase() !== method.toUpperCase():
493
+ case "async" in obj && obj.async !== async:
494
+ return false;
546
495
  }
496
+ return true;
497
+ })
498
+ );
499
+ }
500
+ waitForRequestKeys() {
501
+ const requestKeys = ["url", "method", "abort", "headers", "data"];
502
+ if (!this.request.async) {
503
+ win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
504
+ if (this.shouldFilter(filters)) return;
505
+ hookFns.forEach((fn) => {
506
+ if (getType(fn) === "[object Function]")
507
+ catchError(fn, this.request);
508
+ });
509
+ requestKeys.forEach((key) => {
510
+ if (isThenable(this.request[key]))
511
+ this.request[key] = this.requestClone[key];
512
+ });
547
513
  });
514
+ return new SyncThenable();
548
515
  }
549
- return new SyncThenable();
516
+ const promises = [];
517
+ win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
518
+ if (this.shouldFilter(filters)) return;
519
+ promises.push(
520
+ Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
521
+ () =>
522
+ Promise.all(
523
+ requestKeys.map((key) =>
524
+ Promise.resolve(this.request[key]).then(
525
+ (val) => (this.request[key] = val),
526
+ () => (this.request[key] = this.requestClone[key])
527
+ )
528
+ )
529
+ )
530
+ )
531
+ );
532
+ });
533
+ return Promise.all(promises);
550
534
  }
551
- return Promise.resolve(catchError(this.request.response, response)).then(
552
- () =>
535
+ waitForResponseKeys(response) {
536
+ const responseKeys =
537
+ this.request.type === "xhr" ? xhrResponses : fetchResponses;
538
+ if (!this.request.async) {
539
+ if (getType(this.request.response) === "[object Function]") {
540
+ catchError(this.request.response, response);
541
+ responseKeys.forEach((key) => {
542
+ if (
543
+ "get" in getDescriptor(response, key) ||
544
+ isThenable(response[key])
545
+ ) {
546
+ delete response[key];
547
+ }
548
+ });
549
+ }
550
+ return new SyncThenable();
551
+ }
552
+ return Promise.resolve(
553
+ catchError(this.request.response, response)
554
+ ).then(() =>
553
555
  Promise.all(
554
556
  responseKeys.map((key) => {
555
557
  const descriptor = getDescriptor(response, key);
@@ -563,368 +565,381 @@ var ajaxHooker = (function () {
563
565
  }
564
566
  })
565
567
  )
566
- );
568
+ );
569
+ }
567
570
  }
568
- }
569
- const proxyHandler = {
570
- get(target, prop) {
571
- const descriptor = getDescriptor(target, prop);
572
- if (
573
- descriptor &&
574
- !descriptor.configurable &&
575
- !descriptor.writable &&
576
- !descriptor.get
577
- )
571
+ const proxyHandler = {
572
+ get(target, prop) {
573
+ const descriptor = getDescriptor(target, prop);
574
+ if (
575
+ descriptor &&
576
+ !descriptor.configurable &&
577
+ !descriptor.writable &&
578
+ !descriptor.get
579
+ )
580
+ return target[prop];
581
+ const ah = target.__ajaxHooker;
582
+ if (ah && ah.proxyProps) {
583
+ if (prop in ah.proxyProps) {
584
+ const pDescriptor = ah.proxyProps[prop];
585
+ if ("get" in pDescriptor) return pDescriptor.get();
586
+ if (typeof pDescriptor.value === "function")
587
+ return pDescriptor.value.bind(ah);
588
+ return pDescriptor.value;
589
+ }
590
+ if (typeof target[prop] === "function")
591
+ return target[prop].bind(target);
592
+ }
578
593
  return target[prop];
579
- const ah = target.__ajaxHooker;
580
- if (ah && ah.proxyProps) {
581
- if (prop in ah.proxyProps) {
594
+ },
595
+ set(target, prop, value) {
596
+ const descriptor = getDescriptor(target, prop);
597
+ if (
598
+ descriptor &&
599
+ !descriptor.configurable &&
600
+ !descriptor.writable &&
601
+ !descriptor.set
602
+ )
603
+ return true;
604
+ const ah = target.__ajaxHooker;
605
+ if (ah && ah.proxyProps && prop in ah.proxyProps) {
582
606
  const pDescriptor = ah.proxyProps[prop];
583
- if ("get" in pDescriptor) return pDescriptor.get();
584
- if (typeof pDescriptor.value === "function")
585
- return pDescriptor.value.bind(ah);
586
- return pDescriptor.value;
607
+ pDescriptor.set
608
+ ? pDescriptor.set(value)
609
+ : (pDescriptor.value = value);
610
+ } else {
611
+ target[prop] = value;
587
612
  }
588
- if (typeof target[prop] === "function")
589
- return target[prop].bind(target);
590
- }
591
- return target[prop];
592
- },
593
- set(target, prop, value) {
594
- const descriptor = getDescriptor(target, prop);
595
- if (
596
- descriptor &&
597
- !descriptor.configurable &&
598
- !descriptor.writable &&
599
- !descriptor.set
600
- )
601
613
  return true;
602
- const ah = target.__ajaxHooker;
603
- if (ah && ah.proxyProps && prop in ah.proxyProps) {
604
- const pDescriptor = ah.proxyProps[prop];
605
- pDescriptor.set ? pDescriptor.set(value) : (pDescriptor.value = value);
606
- } else {
607
- target[prop] = value;
608
- }
609
- return true;
610
- },
611
- };
612
- class XhrHooker {
613
- constructor(xhr) {
614
- const ah = this;
615
- Object.assign(ah, {
616
- originalXhr: xhr,
617
- proxyXhr: new Proxy(xhr, proxyHandler),
618
- resThenable: new SyncThenable(),
619
- proxyProps: {},
620
- proxyEvents: {},
621
- });
622
- xhr.addEventListener("readystatechange", (e) => {
623
- if (
624
- ah.proxyXhr.readyState === 4 &&
625
- ah.request &&
626
- typeof ah.request.response === "function"
627
- ) {
628
- const response = {
629
- finalUrl: ah.proxyXhr.responseURL,
630
- status: ah.proxyXhr.status,
631
- responseHeaders: parseHeaders(ah.proxyXhr.getAllResponseHeaders()),
632
- };
633
- const tempValues = {};
634
- for (const key of xhrResponses) {
635
- try {
636
- tempValues[key] = ah.originalXhr[key];
637
- } catch (err) {}
638
- defineProp(
639
- response,
640
- key,
641
- () => {
642
- return (response[key] = tempValues[key]);
643
- },
644
- (val) => {
645
- delete response[key];
646
- response[key] = val;
647
- }
648
- );
614
+ },
615
+ };
616
+ class XhrHooker {
617
+ constructor(xhr) {
618
+ const ah = this;
619
+ Object.assign(ah, {
620
+ originalXhr: xhr,
621
+ proxyXhr: new Proxy(xhr, proxyHandler),
622
+ resThenable: new SyncThenable(),
623
+ proxyProps: {},
624
+ proxyEvents: {},
625
+ });
626
+ xhr.addEventListener("readystatechange", (e) => {
627
+ if (
628
+ ah.proxyXhr.readyState === 4 &&
629
+ ah.request &&
630
+ typeof ah.request.response === "function"
631
+ ) {
632
+ const response = {
633
+ finalUrl: ah.proxyXhr.responseURL,
634
+ status: ah.proxyXhr.status,
635
+ responseHeaders: parseHeaders(
636
+ ah.proxyXhr.getAllResponseHeaders()
637
+ ),
638
+ };
639
+ const tempValues = {};
640
+ for (const key of xhrResponses) {
641
+ try {
642
+ tempValues[key] = ah.originalXhr[key];
643
+ } catch (err) {}
644
+ defineProp(
645
+ response,
646
+ key,
647
+ () => {
648
+ return (response[key] = tempValues[key]);
649
+ },
650
+ (val) => {
651
+ delete response[key];
652
+ response[key] = val;
653
+ }
654
+ );
655
+ }
656
+ ah.resThenable = new AHRequest(ah.request)
657
+ .waitForResponseKeys(response)
658
+ .then(() => {
659
+ for (const key of xhrResponses) {
660
+ ah.proxyProps[key] = {
661
+ get: () => {
662
+ if (!(key in response)) response[key] = tempValues[key];
663
+ return response[key];
664
+ },
665
+ };
666
+ }
667
+ });
649
668
  }
650
- ah.resThenable = new AHRequest(ah.request)
651
- .waitForResponseKeys(response)
652
- .then(() => {
653
- for (const key of xhrResponses) {
654
- ah.proxyProps[key] = {
655
- get: () => {
656
- if (!(key in response)) response[key] = tempValues[key];
657
- return response[key];
658
- },
659
- };
660
- }
661
- });
669
+ ah.dispatchEvent(e);
670
+ });
671
+ xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
672
+ xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
673
+ for (const evt of xhrAsyncEvents) {
674
+ const onEvt = "on" + evt;
675
+ ah.proxyProps[onEvt] = {
676
+ get: () => ah.proxyEvents[onEvt] || null,
677
+ set: (val) => ah.addEvent(onEvt, val),
678
+ };
679
+ }
680
+ for (const method of [
681
+ "setRequestHeader",
682
+ "addEventListener",
683
+ "removeEventListener",
684
+ "open",
685
+ "send",
686
+ ]) {
687
+ ah.proxyProps[method] = { value: ah[method] };
662
688
  }
663
- ah.dispatchEvent(e);
664
- });
665
- xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
666
- xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
667
- for (const evt of xhrAsyncEvents) {
668
- const onEvt = "on" + evt;
669
- ah.proxyProps[onEvt] = {
670
- get: () => ah.proxyEvents[onEvt] || null,
671
- set: (val) => ah.addEvent(onEvt, val),
672
- };
673
689
  }
674
- for (const method of [
675
- "setRequestHeader",
676
- "addEventListener",
677
- "removeEventListener",
678
- "open",
679
- "send",
680
- ]) {
681
- ah.proxyProps[method] = { value: ah[method] };
690
+ toJSON() {} // Converting circular structure to JSON
691
+ addEvent(type, event) {
692
+ if (type.startsWith("on")) {
693
+ this.proxyEvents[type] = typeof event === "function" ? event : null;
694
+ } else {
695
+ if (typeof event === "object" && event !== null)
696
+ event = event.handleEvent;
697
+ if (typeof event !== "function") return;
698
+ this.proxyEvents[type] = this.proxyEvents[type] || new Set();
699
+ this.proxyEvents[type].add(event);
700
+ }
682
701
  }
683
- }
684
- toJSON() {} // Converting circular structure to JSON
685
- addEvent(type, event) {
686
- if (type.startsWith("on")) {
687
- this.proxyEvents[type] = typeof event === "function" ? event : null;
688
- } else {
689
- if (typeof event === "object" && event !== null)
690
- event = event.handleEvent;
691
- if (typeof event !== "function") return;
692
- this.proxyEvents[type] = this.proxyEvents[type] || new Set();
693
- this.proxyEvents[type].add(event);
702
+ removeEvent(type, event) {
703
+ if (type.startsWith("on")) {
704
+ this.proxyEvents[type] = null;
705
+ } else {
706
+ if (typeof event === "object" && event !== null)
707
+ event = event.handleEvent;
708
+ this.proxyEvents[type] && this.proxyEvents[type].delete(event);
709
+ }
694
710
  }
695
- }
696
- removeEvent(type, event) {
697
- if (type.startsWith("on")) {
698
- this.proxyEvents[type] = null;
699
- } else {
700
- if (typeof event === "object" && event !== null)
701
- event = event.handleEvent;
702
- this.proxyEvents[type] && this.proxyEvents[type].delete(event);
711
+ dispatchEvent(e) {
712
+ e.stopImmediatePropagation = stopImmediatePropagation;
713
+ defineProp(e, "target", () => this.proxyXhr);
714
+ this.proxyEvents[e.type] &&
715
+ this.proxyEvents[e.type].forEach((fn) => {
716
+ this.resThenable.then(
717
+ () => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e)
718
+ );
719
+ });
720
+ if (e.ajaxHooker_isStopped) return;
721
+ const onEvent = this.proxyEvents["on" + e.type];
722
+ onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
703
723
  }
704
- }
705
- dispatchEvent(e) {
706
- e.stopImmediatePropagation = stopImmediatePropagation;
707
- defineProp(e, "target", () => this.proxyXhr);
708
- this.proxyEvents[e.type] &&
709
- this.proxyEvents[e.type].forEach((fn) => {
710
- this.resThenable.then(
711
- () => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e)
712
- );
724
+ setRequestHeader(header, value) {
725
+ this.originalXhr.setRequestHeader(header, value);
726
+ if (this.originalXhr.readyState !== 1) return;
727
+ const headers = this.request.headers;
728
+ headers[header] =
729
+ header in headers ? `${headers[header]}, ${value}` : value;
730
+ }
731
+ addEventListener(...args) {
732
+ if (xhrAsyncEvents.includes(args[0])) {
733
+ this.addEvent(args[0], args[1]);
734
+ } else {
735
+ this.originalXhr.addEventListener(...args);
736
+ }
737
+ }
738
+ removeEventListener(...args) {
739
+ if (xhrAsyncEvents.includes(args[0])) {
740
+ this.removeEvent(args[0], args[1]);
741
+ } else {
742
+ this.originalXhr.removeEventListener(...args);
743
+ }
744
+ }
745
+ open(method, url, async = true, ...args) {
746
+ this.request = {
747
+ type: "xhr",
748
+ url: url.toString(),
749
+ method: method.toUpperCase(),
750
+ abort: false,
751
+ headers: {},
752
+ data: null,
753
+ response: null,
754
+ async: !!async,
755
+ };
756
+ this.openArgs = args;
757
+ this.resThenable = new SyncThenable();
758
+ [
759
+ "responseURL",
760
+ "readyState",
761
+ "status",
762
+ "statusText",
763
+ ...xhrResponses,
764
+ ].forEach((key) => {
765
+ delete this.proxyProps[key];
713
766
  });
714
- if (e.ajaxHooker_isStopped) return;
715
- const onEvent = this.proxyEvents["on" + e.type];
716
- onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
717
- }
718
- setRequestHeader(header, value) {
719
- this.originalXhr.setRequestHeader(header, value);
720
- if (this.originalXhr.readyState !== 1) return;
721
- const headers = this.request.headers;
722
- headers[header] =
723
- header in headers ? `${headers[header]}, ${value}` : value;
724
- }
725
- addEventListener(...args) {
726
- if (xhrAsyncEvents.includes(args[0])) {
727
- this.addEvent(args[0], args[1]);
728
- } else {
729
- this.originalXhr.addEventListener(...args);
767
+ return this.originalXhr.open(method, url, async, ...args);
730
768
  }
731
- }
732
- removeEventListener(...args) {
733
- if (xhrAsyncEvents.includes(args[0])) {
734
- this.removeEvent(args[0], args[1]);
735
- } else {
736
- this.originalXhr.removeEventListener(...args);
769
+ send(data) {
770
+ const ah = this;
771
+ const xhr = ah.originalXhr;
772
+ const request = ah.request;
773
+ if (!request) return xhr.send(data);
774
+ request.data = data;
775
+ new AHRequest(request).waitForRequestKeys().then(() => {
776
+ if (request.abort) {
777
+ if (typeof request.response === "function") {
778
+ Object.assign(ah.proxyProps, {
779
+ responseURL: { value: request.url },
780
+ readyState: { value: 4 },
781
+ status: { value: 200 },
782
+ statusText: { value: "OK" },
783
+ });
784
+ xhrAsyncEvents.forEach((evt) =>
785
+ xhr.dispatchEvent(new Event(evt))
786
+ );
787
+ }
788
+ } else {
789
+ xhr.open(
790
+ request.method,
791
+ request.url,
792
+ request.async,
793
+ ...ah.openArgs
794
+ );
795
+ for (const header in request.headers) {
796
+ xhr.setRequestHeader(header, request.headers[header]);
797
+ }
798
+ xhr.send(request.data);
799
+ }
800
+ });
737
801
  }
738
802
  }
739
- open(method, url, async = true, ...args) {
740
- this.request = {
741
- type: "xhr",
742
- url: url.toString(),
743
- method: method.toUpperCase(),
803
+ function fakeXHR() {
804
+ const xhr = new winAh.realXHR();
805
+ if ("__ajaxHooker" in xhr)
806
+ console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
807
+ xhr.__ajaxHooker = new XhrHooker(xhr);
808
+ return xhr.__ajaxHooker.proxyXhr;
809
+ }
810
+ fakeXHR.prototype = win.XMLHttpRequest.prototype;
811
+ Object.keys(win.XMLHttpRequest).forEach(
812
+ (key) => (fakeXHR[key] = win.XMLHttpRequest[key])
813
+ );
814
+ function fakeFetch(url, options = {}) {
815
+ if (!url) return winAh.realFetch.call(win, url, options);
816
+ const init = {};
817
+ if (getType(url) === "[object Request]") {
818
+ for (const prop of fetchInitProps) init[prop] = url[prop];
819
+ url = url.url;
820
+ }
821
+ url = url.toString();
822
+ Object.assign(init, options);
823
+ init.method = init.method || "GET";
824
+ init.headers = init.headers || {};
825
+ const request = {
826
+ type: "fetch",
827
+ url: url,
828
+ method: init.method.toUpperCase(),
744
829
  abort: false,
745
- headers: {},
746
- data: null,
830
+ headers: parseHeaders(init.headers),
831
+ data: init.body,
747
832
  response: null,
748
- async: !!async,
833
+ async: true,
749
834
  };
750
- this.openArgs = args;
751
- this.resThenable = new SyncThenable();
752
- [
753
- "responseURL",
754
- "readyState",
755
- "status",
756
- "statusText",
757
- ...xhrResponses,
758
- ].forEach((key) => {
759
- delete this.proxyProps[key];
835
+ const req = new AHRequest(request);
836
+ return new Promise((resolve, reject) => {
837
+ req
838
+ .waitForRequestKeys()
839
+ .then(() => {
840
+ if (request.abort) {
841
+ if (typeof request.response === "function") {
842
+ const response = {
843
+ finalUrl: request.url,
844
+ status: 200,
845
+ responseHeaders: {},
846
+ };
847
+ req.waitForResponseKeys(response).then(() => {
848
+ const key = fetchResponses.find((k) => k in response);
849
+ let val = response[key];
850
+ if (key === "json" && typeof val === "object") {
851
+ val = catchError(JSON.stringify.bind(JSON), val);
852
+ }
853
+ const res = new Response(val, {
854
+ status: 200,
855
+ statusText: "OK",
856
+ });
857
+ defineProp(res, "type", () => "basic");
858
+ defineProp(res, "url", () => request.url);
859
+ resolve(res);
860
+ });
861
+ } else {
862
+ reject(new DOMException("aborted", "AbortError"));
863
+ }
864
+ return;
865
+ }
866
+ init.method = request.method;
867
+ init.headers = request.headers;
868
+ init.body = request.data;
869
+ winAh.realFetch.call(win, request.url, init).then((res) => {
870
+ if (typeof request.response === "function") {
871
+ const response = {
872
+ finalUrl: res.url,
873
+ status: res.status,
874
+ responseHeaders: parseHeaders(res.headers),
875
+ };
876
+ fetchResponses.forEach(
877
+ (key) =>
878
+ (res[key] = function () {
879
+ if (key in response)
880
+ return Promise.resolve(response[key]);
881
+ return resProto[key].call(this).then((val) => {
882
+ response[key] = val;
883
+ return req
884
+ .waitForResponseKeys(response)
885
+ .then(() => (key in response ? response[key] : val));
886
+ });
887
+ })
888
+ );
889
+ }
890
+ resolve(res);
891
+ }, reject);
892
+ })
893
+ .catch((err) => {
894
+ console.error(err);
895
+ resolve(winAh.realFetch.call(win, url, init));
896
+ });
760
897
  });
761
- return this.originalXhr.open(method, url, async, ...args);
762
898
  }
763
- send(data) {
764
- const ah = this;
765
- const xhr = ah.originalXhr;
766
- const request = ah.request;
767
- if (!request) return xhr.send(data);
768
- request.data = data;
769
- new AHRequest(request).waitForRequestKeys().then(() => {
770
- if (request.abort) {
771
- if (typeof request.response === "function") {
772
- Object.assign(ah.proxyProps, {
773
- responseURL: { value: request.url },
774
- readyState: { value: 4 },
775
- status: { value: 200 },
776
- statusText: { value: "OK" },
777
- });
778
- xhrAsyncEvents.forEach((evt) => xhr.dispatchEvent(new Event(evt)));
779
- }
780
- } else {
781
- xhr.open(request.method, request.url, request.async, ...ah.openArgs);
782
- for (const header in request.headers) {
783
- xhr.setRequestHeader(header, request.headers[header]);
784
- }
785
- xhr.send(request.data);
786
- }
787
- });
899
+ function fakeFetchClone() {
900
+ const descriptors = Object.getOwnPropertyDescriptors(this);
901
+ const res = winAh.realFetchClone.call(this);
902
+ Object.defineProperties(res, descriptors);
903
+ return res;
788
904
  }
789
- }
790
- function fakeXHR() {
791
- const xhr = new winAh.realXHR();
792
- if ("__ajaxHooker" in xhr)
905
+ winAh = win.__ajaxHooker = winAh || {
906
+ version,
907
+ fakeXHR,
908
+ fakeFetch,
909
+ fakeFetchClone,
910
+ realXHR: win.XMLHttpRequest,
911
+ realFetch: win.fetch,
912
+ realFetchClone: resProto.clone,
913
+ hookInsts: new Set(),
914
+ };
915
+ if (winAh.version !== version)
793
916
  console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
794
- xhr.__ajaxHooker = new XhrHooker(xhr);
795
- return xhr.__ajaxHooker.proxyXhr;
796
- }
797
- fakeXHR.prototype = win.XMLHttpRequest.prototype;
798
- Object.keys(win.XMLHttpRequest).forEach(
799
- (key) => (fakeXHR[key] = win.XMLHttpRequest[key])
800
- );
801
- function fakeFetch(url, options = {}) {
802
- if (!url) return winAh.realFetch.call(win, url, options);
803
- const init = {};
804
- if (getType(url) === "[object Request]") {
805
- for (const prop of fetchInitProps) init[prop] = url[prop];
806
- url = url.url;
807
- }
808
- url = url.toString();
809
- Object.assign(init, options);
810
- init.method = init.method || "GET";
811
- init.headers = init.headers || {};
812
- const request = {
813
- type: "fetch",
814
- url: url,
815
- method: init.method.toUpperCase(),
816
- abort: false,
817
- headers: parseHeaders(init.headers),
818
- data: init.body,
819
- response: null,
820
- async: true,
917
+ win.XMLHttpRequest = winAh.fakeXHR;
918
+ win.fetch = winAh.fakeFetch;
919
+ resProto.clone = winAh.fakeFetchClone;
920
+ winAh.hookInsts.add(hookInst);
921
+ return {
922
+ hook: (fn) => hookInst.hookFns.push(fn),
923
+ filter: (arr) => {
924
+ if (Array.isArray(arr)) hookInst.filters = arr;
925
+ },
926
+ protect: () => {
927
+ readonly(win, "XMLHttpRequest", winAh.fakeXHR);
928
+ readonly(win, "fetch", winAh.fakeFetch);
929
+ readonly(resProto, "clone", winAh.fakeFetchClone);
930
+ },
931
+ unhook: () => {
932
+ winAh.hookInsts.delete(hookInst);
933
+ if (!winAh.hookInsts.size) {
934
+ writable(win, "XMLHttpRequest", winAh.realXHR);
935
+ writable(win, "fetch", winAh.realFetch);
936
+ writable(resProto, "clone", winAh.realFetchClone);
937
+ delete win.__ajaxHooker;
938
+ }
939
+ },
821
940
  };
822
- const req = new AHRequest(request);
823
- return new Promise((resolve, reject) => {
824
- req
825
- .waitForRequestKeys()
826
- .then(() => {
827
- if (request.abort) {
828
- if (typeof request.response === "function") {
829
- const response = {
830
- finalUrl: request.url,
831
- status: 200,
832
- responseHeaders: {},
833
- };
834
- req.waitForResponseKeys(response).then(() => {
835
- const key = fetchResponses.find((k) => k in response);
836
- let val = response[key];
837
- if (key === "json" && typeof val === "object") {
838
- val = catchError(JSON.stringify.bind(JSON), val);
839
- }
840
- const res = new Response(val, {
841
- status: 200,
842
- statusText: "OK",
843
- });
844
- defineProp(res, "type", () => "basic");
845
- defineProp(res, "url", () => request.url);
846
- resolve(res);
847
- });
848
- } else {
849
- reject(new DOMException("aborted", "AbortError"));
850
- }
851
- return;
852
- }
853
- init.method = request.method;
854
- init.headers = request.headers;
855
- init.body = request.data;
856
- winAh.realFetch.call(win, request.url, init).then((res) => {
857
- if (typeof request.response === "function") {
858
- const response = {
859
- finalUrl: res.url,
860
- status: res.status,
861
- responseHeaders: parseHeaders(res.headers),
862
- };
863
- fetchResponses.forEach(
864
- (key) =>
865
- (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
870
- .waitForResponseKeys(response)
871
- .then(() => (key in response ? response[key] : val));
872
- });
873
- })
874
- );
875
- }
876
- resolve(res);
877
- }, reject);
878
- })
879
- .catch((err) => {
880
- console.error(err);
881
- resolve(winAh.realFetch.call(win, url, init));
882
- });
883
- });
884
- }
885
- function fakeFetchClone() {
886
- const descriptors = Object.getOwnPropertyDescriptors(this);
887
- const res = winAh.realFetchClone.call(this);
888
- Object.defineProperties(res, descriptors);
889
- return res;
890
- }
891
- winAh = win.__ajaxHooker = winAh || {
892
- version,
893
- fakeXHR,
894
- fakeFetch,
895
- fakeFetchClone,
896
- realXHR: win.XMLHttpRequest,
897
- realFetch: win.fetch,
898
- realFetchClone: resProto.clone,
899
- hookInsts: new Set(),
900
- };
901
- if (winAh.version !== version)
902
- console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
903
- win.XMLHttpRequest = winAh.fakeXHR;
904
- win.fetch = winAh.fakeFetch;
905
- resProto.clone = winAh.fakeFetchClone;
906
- winAh.hookInsts.add(hookInst);
907
- return {
908
- hook: (fn) => hookInst.hookFns.push(fn),
909
- filter: (arr) => {
910
- if (Array.isArray(arr)) hookInst.filters = arr;
911
- },
912
- protect: () => {
913
- readonly(win, "XMLHttpRequest", winAh.fakeXHR);
914
- readonly(win, "fetch", winAh.fakeFetch);
915
- readonly(resProto, "clone", winAh.fakeFetchClone);
916
- },
917
- unhook: () => {
918
- winAh.hookInsts.delete(hookInst);
919
- if (!winAh.hookInsts.size) {
920
- writable(win, "XMLHttpRequest", winAh.realXHR);
921
- writable(win, "fetch", winAh.realFetch);
922
- writable(resProto, "clone", winAh.realFetchClone);
923
- delete win.__ajaxHooker;
924
- }
925
- },
926
- };
927
- })();
941
+ })();
942
+ };
928
943
 
929
944
  /**
930
945
  *
@@ -3195,7 +3210,7 @@ class UtilsDictionary {
3195
3210
 
3196
3211
  let Utils$1 = class Utils {
3197
3212
  /** 版本号 */
3198
- version = "2024.5.24";
3213
+ version = "2024.5.25";
3199
3214
  addStyle(cssText) {
3200
3215
  if (typeof cssText !== "string") {
3201
3216
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -3310,7 +3325,7 @@ let Utils$1 = class Utils {
3310
3325
  * + 版本:1.4.1
3311
3326
  * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
3312
3327
  */
3313
- ajaxHooker = ajaxHooker;
3328
+ ajaxHooker = AjaxHooker;
3314
3329
  canvasClickByPosition(canvasElement, clientX = 0, clientY = 0, view = globalThis) {
3315
3330
  if (!(canvasElement instanceof HTMLCanvasElement)) {
3316
3331
  throw new Error("Utils.canvasClickByPosition 参数canvasElement必须是canvas元素");