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