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