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