@whitesev/utils 2.8.1 → 2.9.0

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.
Files changed (74) hide show
  1. package/README.md +176 -176
  2. package/dist/index.amd.js +271 -777
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.amd.min.js +2 -0
  5. package/dist/index.amd.min.js.map +1 -0
  6. package/dist/index.cjs.js +271 -777
  7. package/dist/index.cjs.js.map +1 -1
  8. package/dist/index.cjs.min.js +2 -0
  9. package/dist/index.cjs.min.js.map +1 -0
  10. package/dist/index.esm.js +271 -777
  11. package/dist/index.esm.js.map +1 -1
  12. package/dist/index.esm.min.js +2 -0
  13. package/dist/index.esm.min.js.map +1 -0
  14. package/dist/index.iife.js +271 -777
  15. package/dist/index.iife.js.map +1 -1
  16. package/dist/index.iife.min.js +2 -0
  17. package/dist/index.iife.min.js.map +1 -0
  18. package/dist/index.system.js +271 -777
  19. package/dist/index.system.js.map +1 -1
  20. package/dist/index.system.min.js +2 -0
  21. package/dist/index.system.min.js.map +1 -0
  22. package/dist/index.umd.js +271 -777
  23. package/dist/index.umd.js.map +1 -1
  24. package/dist/index.umd.min.js +2 -0
  25. package/dist/index.umd.min.js.map +1 -0
  26. package/dist/types/src/Utils.d.ts +103 -450
  27. package/dist/types/src/UtilsGMCookie.d.ts +4 -0
  28. package/dist/types/src/UtilsGMMenu.d.ts +3 -6
  29. package/dist/types/src/types/Httpx.d.ts +1344 -1344
  30. package/dist/types/src/types/Log.d.ts +19 -19
  31. package/dist/types/src/types/Progress.d.ts +20 -20
  32. package/dist/types/src/types/React.d.ts +119 -119
  33. package/dist/types/src/types/TryCatch.d.ts +9 -9
  34. package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
  35. package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
  36. package/dist/types/src/types/Vue2.d.ts +166 -166
  37. package/dist/types/src/types/WindowApi.d.ts +14 -14
  38. package/dist/types/src/types/ajaxHooker.d.ts +151 -151
  39. package/dist/types/src/types/env.d.ts +7 -7
  40. package/dist/types/src/types/global.d.ts +31 -31
  41. package/index.ts +3 -0
  42. package/package.json +16 -10
  43. package/src/ColorConversion.ts +105 -105
  44. package/src/CommonUtil.ts +280 -280
  45. package/src/DOMUtils.ts +251 -251
  46. package/src/Dictionary.ts +153 -153
  47. package/src/GBKEncoder.ts +108 -108
  48. package/src/Hooks.ts +73 -73
  49. package/src/Httpx.ts +1457 -1457
  50. package/src/LockFunction.ts +62 -62
  51. package/src/Log.ts +258 -258
  52. package/src/Progress.ts +108 -108
  53. package/src/TryCatch.ts +86 -86
  54. package/src/Utils.ts +3778 -4772
  55. package/src/UtilsCommon.ts +14 -14
  56. package/src/UtilsGMCookie.ts +272 -254
  57. package/src/UtilsGMMenu.ts +441 -445
  58. package/src/Vue.ts +233 -233
  59. package/src/WindowApi.ts +59 -59
  60. package/src/indexedDB.ts +497 -497
  61. package/src/types/Httpx.d.ts +1344 -1344
  62. package/src/types/Log.d.ts +19 -19
  63. package/src/types/Progress.d.ts +20 -20
  64. package/src/types/React.d.ts +119 -119
  65. package/src/types/TryCatch.d.ts +9 -9
  66. package/src/types/UtilsGMCookie.d.ts +93 -93
  67. package/src/types/UtilsGMMenu.d.ts +77 -77
  68. package/src/types/Vue2.d.ts +166 -166
  69. package/src/types/WindowApi.d.ts +14 -14
  70. package/src/types/ajaxHooker.d.ts +151 -151
  71. package/src/types/env.d.ts +7 -7
  72. package/src/types/global.d.ts +31 -31
  73. package/dist/types/src/types/Event.d.ts +0 -188
  74. package/src/types/Event.d.ts +0 -188
package/dist/index.umd.js CHANGED
@@ -525,18 +525,12 @@
525
525
  }
526
526
  return findValue;
527
527
  }
528
- /**
529
- * 获取多组Cookie
530
- * @param option 配置
531
- * @param callback 获取操作后的回调
532
- * + cookies object[]
533
- * + error string|undefined
534
- **/
535
528
  list(option, callback) {
536
529
  if (option == null) {
537
530
  throw new Error("Utils.GMCookie.list 参数不能为空");
538
531
  }
539
532
  const resultData = [];
533
+ let error;
540
534
  try {
541
535
  let defaultOption = {
542
536
  url: this.windowApi.window.location.href,
@@ -570,14 +564,18 @@
570
564
  });
571
565
  }
572
566
  });
573
- if (typeof callback === "function") {
574
- callback(resultData);
575
- }
576
567
  }
577
- catch (error) {
578
- if (typeof callback === "function") {
579
- callback(resultData, error);
580
- }
568
+ catch (e) {
569
+ error = e;
570
+ }
571
+ if (typeof callback === "function") {
572
+ callback(resultData, error);
573
+ }
574
+ else {
575
+ return {
576
+ cookies: resultData,
577
+ error: error,
578
+ };
581
579
  }
582
580
  }
583
581
  /**
@@ -1786,7 +1784,7 @@
1786
1784
  },
1787
1785
  /**
1788
1786
  * 注册油猴菜单
1789
- * @param menuOptions 如果存在,使用它
1787
+ * @param menuOptions (可选)菜单配置
1790
1788
  */
1791
1789
  register(menuOptions) {
1792
1790
  const that = this;
@@ -1810,7 +1808,8 @@
1810
1808
  },
1811
1809
  /**
1812
1810
  * 获取本地存储菜单键值
1813
- * @param {string} key 键
1811
+ * @param key 键
1812
+ * @param defaultValue 默认值
1814
1813
  */
1815
1814
  getLocalMenuData(key, defaultValue) {
1816
1815
  const localData = this.context.GM_Api.getValue(this.$data.key, {});
@@ -1833,7 +1832,7 @@
1833
1832
  },
1834
1833
  /**
1835
1834
  * 处理初始化配置
1836
- * @param menuOption
1835
+ * @param menuOption 菜单配置
1837
1836
  */
1838
1837
  handleInitDetail(menuOption) {
1839
1838
  menuOption.enable = Boolean(this.getLocalMenuData(menuOption.key, menuOption.enable));
@@ -1851,7 +1850,7 @@
1851
1850
  },
1852
1851
  /**
1853
1852
  * 对菜单数据进行处理
1854
- * @param menuOption
1853
+ * @param menuOption 菜单配置
1855
1854
  */
1856
1855
  handleMenuData(menuOption) {
1857
1856
  const that = this;
@@ -1939,6 +1938,9 @@
1939
1938
  return this.$data.data.find((item) => item.handleData.key === menuKey)?.handleData;
1940
1939
  },
1941
1940
  };
1941
+ /**
1942
+ * @param details 菜单配置
1943
+ */
1942
1944
  constructor(details) {
1943
1945
  this.GM_Api.getValue = details.GM_getValue;
1944
1946
  this.GM_Api.setValue = details.GM_setValue;
@@ -2026,14 +2028,6 @@
2026
2028
  delete(menuId) {
2027
2029
  this.GM_Api.unregisterMenuCommand(menuId);
2028
2030
  }
2029
- /**
2030
- * 根据键值获取enable值
2031
- * @param menuKey 菜单-键key
2032
- * @deprecated
2033
- */
2034
- get(menuKey) {
2035
- return this.getEnable(menuKey);
2036
- }
2037
2031
  /**
2038
2032
  * 根据键值获取enable值
2039
2033
  * @param menuKey 菜单-键key
@@ -2191,11 +2185,11 @@
2191
2185
  return "";
2192
2186
  }
2193
2187
  try {
2194
- new Function("_context", "_funcName", "hookFunc", `_context[_funcName] = function ${_funcName}() {
2195
- let args = Array.prototype.slice.call(arguments, 0);
2196
- let obj = this;
2197
- hookFunc.apply(obj, args);
2198
- return _context['realFunc_${_funcName}'].apply(obj, args);
2188
+ new Function("_context", "_funcName", "hookFunc", `_context[_funcName] = function ${_funcName}() {
2189
+ let args = Array.prototype.slice.call(arguments, 0);
2190
+ let obj = this;
2191
+ hookFunc.apply(obj, args);
2192
+ return _context['realFunc_${_funcName}'].apply(obj, args);
2199
2193
  };`)(_context, _funcName, hookFunc);
2200
2194
  _context[_funcName].prototype.isHooked = true;
2201
2195
  return true;
@@ -5484,38 +5478,15 @@
5484
5478
  }
5485
5479
  const domUtils = new DOMUtils();
5486
5480
 
5481
+ const version = "2.9.0";
5482
+
5487
5483
  class Utils {
5488
5484
  windowApi;
5489
5485
  constructor(option) {
5490
5486
  this.windowApi = new WindowApi(option);
5491
5487
  }
5492
5488
  /** 版本号 */
5493
- version = "2025.9.14";
5494
- addStyle(cssText) {
5495
- if (typeof cssText !== "string") {
5496
- throw new Error("Utils.addStyle 参数cssText 必须为String类型");
5497
- }
5498
- const cssNode = this.windowApi.document.createElement("style");
5499
- cssNode.setAttribute("type", "text/css");
5500
- cssNode.innerHTML = cssText;
5501
- if (this.windowApi.document.head) {
5502
- /* 插入head最后 */
5503
- this.windowApi.document.head.appendChild(cssNode);
5504
- }
5505
- else if (this.windowApi.document.body) {
5506
- /* 插入body后 */
5507
- this.windowApi.document.body.appendChild(cssNode);
5508
- }
5509
- else if (this.windowApi.document.documentElement.childNodes.length === 0) {
5510
- /* 插入#html第一个元素后 */
5511
- this.windowApi.document.documentElement.appendChild(cssNode);
5512
- }
5513
- else {
5514
- /* 插入head前面 */
5515
- this.windowApi.document.documentElement.insertBefore(cssNode, this.windowApi.document.documentElement.childNodes[0]);
5516
- }
5517
- return cssNode;
5518
- }
5489
+ version = version;
5519
5490
  /**
5520
5491
  * JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
5521
5492
  * @param target 目标数据
@@ -5533,7 +5504,7 @@
5533
5504
  */
5534
5505
  assign = commonUtil.assign.bind(commonUtil);
5535
5506
  async asyncReplaceAll(string, pattern, asyncFn) {
5536
- const UtilsContext = this;
5507
+ const that = this;
5537
5508
  if (typeof string !== "string") {
5538
5509
  throw new TypeError("string必须是字符串");
5539
5510
  }
@@ -5542,7 +5513,7 @@
5542
5513
  }
5543
5514
  let reg;
5544
5515
  if (typeof pattern === "string") {
5545
- reg = new RegExp(UtilsContext.parseStringToRegExpString(pattern), "g");
5516
+ reg = new RegExp(that.toRegExpStr(pattern), "g");
5546
5517
  }
5547
5518
  else if (pattern instanceof RegExp) {
5548
5519
  if (!pattern.global) {
@@ -5574,7 +5545,7 @@
5574
5545
  * ajax劫持库,支持xhr和fetch劫持。
5575
5546
  * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5576
5547
  * + 作者:cxxjackie
5577
- * + 版本:1.4.7
5548
+ * + 版本:1.4.8
5578
5549
  * + 旧版本:1.2.4
5579
5550
  * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
5580
5551
  * @param useOldVersion 是否使用旧版本,默认false
@@ -5604,41 +5575,6 @@
5604
5575
  canvasElement.dispatchEvent(new MouseEvent("mousedown", eventInit));
5605
5576
  canvasElement.dispatchEvent(new MouseEvent("mouseup", eventInit));
5606
5577
  }
5607
- checkUserClickInNode(element) {
5608
- const UtilsContext = this;
5609
- if (!UtilsContext.isDOM(element)) {
5610
- throw new Error("Utils.checkUserClickInNode 参数 targetNode 必须为 Element|Node 类型");
5611
- }
5612
- const clickEvent = UtilsContext.windowApi.window.event;
5613
- const touchEvent = UtilsContext.windowApi.window.event;
5614
- const $click = clickEvent?.composedPath()?.[0];
5615
- // 点击的x坐标
5616
- const clickPosX = clickEvent?.clientX != null ? clickEvent.clientX : touchEvent.touches[0].clientX;
5617
- // 点击的y坐标
5618
- const clickPosY = clickEvent?.clientY != null ? clickEvent.clientY : touchEvent.touches[0].clientY;
5619
- const {
5620
- /* 要检测的元素的相对屏幕的横坐标最左边 */
5621
- left: elementPosXLeft,
5622
- /* 要检测的元素的相对屏幕的横坐标最右边 */
5623
- right: elementPosXRight,
5624
- /* 要检测的元素的相对屏幕的纵坐标最上边 */
5625
- top: elementPosYTop,
5626
- /* 要检测的元素的相对屏幕的纵坐标最下边 */
5627
- bottom: elementPosYBottom, } = element.getBoundingClientRect();
5628
- if (clickPosX >= elementPosXLeft &&
5629
- clickPosX <= elementPosXRight &&
5630
- clickPosY >= elementPosYTop &&
5631
- clickPosY <= elementPosYBottom) {
5632
- return true;
5633
- }
5634
- else if (($click && element.contains($click)) || $click == element) {
5635
- /* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
5636
- return true;
5637
- }
5638
- else {
5639
- return false;
5640
- }
5641
- }
5642
5578
  /**
5643
5579
  * 复制formData数据
5644
5580
  * @param formData 需要clone的数据
@@ -5686,33 +5622,14 @@
5686
5622
  deepClone = commonUtil.deepClone.bind(commonUtil);
5687
5623
  debounce(fn, delay = 0) {
5688
5624
  let timer = null;
5689
- const UtilsContext = this;
5625
+ const that = this;
5690
5626
  return function (...args) {
5691
- UtilsContext.workerClearTimeout(timer);
5692
- timer = UtilsContext.workerSetTimeout(function () {
5693
- fn.apply(UtilsContext, args);
5627
+ that.workerClearTimeout(timer);
5628
+ timer = that.workerSetTimeout(function () {
5629
+ fn.apply(that, args);
5694
5630
  }, delay);
5695
5631
  };
5696
5632
  }
5697
- deleteParentNode(element, targetSelector) {
5698
- const UtilsContext = this;
5699
- if (element == null) {
5700
- return;
5701
- }
5702
- if (!UtilsContext.isDOM(element)) {
5703
- throw new Error("Utils.deleteParentNode 参数 target 必须为 Node|HTMLElement 类型");
5704
- }
5705
- if (typeof targetSelector !== "string") {
5706
- throw new Error("Utils.deleteParentNode 参数 targetSelector 必须为 string 类型");
5707
- }
5708
- let result = false;
5709
- const needRemoveDOM = domUtils.closest(element, targetSelector);
5710
- if (needRemoveDOM) {
5711
- needRemoveDOM.remove();
5712
- result = true;
5713
- }
5714
- return result;
5715
- }
5716
5633
  /**
5717
5634
  * 字典
5718
5635
  * @example
@@ -5726,25 +5643,8 @@
5726
5643
  * dictionary.concat(dictionary2);
5727
5644
  **/
5728
5645
  Dictionary = UtilsDictionary;
5729
- dispatchEvent(element, eventName, details) {
5730
- // let UtilsContext = this;
5731
- let eventNameList = [];
5732
- if (typeof eventName === "string") {
5733
- eventNameList = [eventName];
5734
- }
5735
- if (Array.isArray(eventName)) {
5736
- eventNameList = [...eventName];
5737
- }
5738
- eventNameList.forEach((_eventName_) => {
5739
- const event = new Event(_eventName_);
5740
- if (details) {
5741
- Object.assign(event, details);
5742
- }
5743
- element.dispatchEvent(event);
5744
- });
5745
- }
5746
5646
  downloadBase64(base64Data, fileName, isIFrame = false) {
5747
- const UtilsContext = this;
5647
+ const that = this;
5748
5648
  if (typeof base64Data !== "string") {
5749
5649
  throw new Error("Utils.downloadBase64 参数 base64Data 必须为 string 类型");
5750
5650
  }
@@ -5753,13 +5653,13 @@
5753
5653
  }
5754
5654
  if (isIFrame) {
5755
5655
  /* 使用iframe */
5756
- const iframeElement = this.windowApi.document.createElement("iframe");
5757
- iframeElement.style.display = "none";
5758
- iframeElement.src = base64Data;
5759
- this.windowApi.document.body.appendChild(iframeElement);
5760
- UtilsContext.workerSetTimeout(() => {
5761
- iframeElement.contentWindow.document.execCommand("SaveAs", true, fileName);
5762
- this.windowApi.document.body.removeChild(iframeElement);
5656
+ const $iframe = this.windowApi.document.createElement("iframe");
5657
+ $iframe.style.display = "none";
5658
+ $iframe.src = base64Data;
5659
+ (this.windowApi.document.body || this.windowApi.document.documentElement).appendChild($iframe);
5660
+ that.workerSetTimeout(() => {
5661
+ $iframe.contentWindow.document.execCommand("SaveAs", true, fileName);
5662
+ (this.windowApi.document.body || this.windowApi.document.documentElement).removeChild($iframe);
5763
5663
  }, 100);
5764
5664
  }
5765
5665
  else {
@@ -5809,50 +5709,6 @@
5809
5709
  }
5810
5710
  return strFound ? true : false;
5811
5711
  }
5812
- *findElementsWithText(element, text, filter) {
5813
- const that = this;
5814
- if (element.outerHTML.includes(text)) {
5815
- if (element.children.length === 0) {
5816
- const filterResult = typeof filter === "function" ? filter(element) : false;
5817
- if (!filterResult) {
5818
- yield element;
5819
- }
5820
- }
5821
- else {
5822
- const textElement = Array.from(element.childNodes).filter((ele) => ele.nodeType === Node.TEXT_NODE);
5823
- for (const $child of textElement) {
5824
- if ($child.textContent.includes(text)) {
5825
- const filterResult = typeof filter === "function" ? filter(element) : false;
5826
- if (!filterResult) {
5827
- yield $child;
5828
- }
5829
- }
5830
- }
5831
- }
5832
- }
5833
- for (let index = 0; index < element.children.length; index++) {
5834
- const $child = element.children[index];
5835
- yield* that.findElementsWithText($child, text, filter);
5836
- }
5837
- }
5838
- /**
5839
- * 判断该元素是否可见,如果不可见,向上找它的父元素直至找到可见的元素
5840
- * @param element
5841
- * @example
5842
- * let visibleElement = Utils.findVisibleElement(document.querySelector("a.xx"));
5843
- * > <HTMLElement>
5844
- */
5845
- findVisibleElement(element) {
5846
- let currentElement = element;
5847
- while (currentElement) {
5848
- const elementRect = currentElement.getBoundingClientRect();
5849
- if (elementRect.length) {
5850
- return currentElement;
5851
- }
5852
- currentElement = currentElement.parentElement;
5853
- }
5854
- return null;
5855
- }
5856
5712
  formatByteToSize(byteSize, addType = true) {
5857
5713
  byteSize = parseInt(byteSize.toString());
5858
5714
  if (isNaN(byteSize)) {
@@ -5901,9 +5757,9 @@
5901
5757
  }
5902
5758
  getNonNullValue(...args) {
5903
5759
  let resultValue = args[args.length - 1];
5904
- const UtilsContext = this;
5760
+ const that = this;
5905
5761
  for (const argValue of args) {
5906
- if (UtilsContext.isNotNull(argValue)) {
5762
+ if (that.isNotNull(argValue)) {
5907
5763
  resultValue = argValue;
5908
5764
  break;
5909
5765
  }
@@ -5977,20 +5833,8 @@
5977
5833
  * > 测试
5978
5834
  */
5979
5835
  GBKEncoder = GBKEncoder;
5980
- /**
5981
- * 获取 transitionend 的在各个浏览器的兼容名
5982
- */
5983
- getTransitionEndNameList() {
5984
- return ["webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend"];
5985
- }
5986
- /**
5987
- * 获取 animationend 的在各个浏览器的兼容名
5988
- */
5989
- getAnimationEndNameList() {
5990
- return ["webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend"];
5991
- }
5992
- getArrayLastValue(targetObj) {
5993
- return targetObj[targetObj.length - 1];
5836
+ getArrayLastValue(target) {
5837
+ return target[target.length - 1];
5994
5838
  }
5995
5839
  getArrayRealValue(...args) {
5996
5840
  let result = null;
@@ -6075,30 +5919,6 @@
6075
5919
  }
6076
5920
  return diffValue;
6077
5921
  }
6078
- getElementSelector(element) {
6079
- const UtilsContext = this;
6080
- if (!element)
6081
- return void 0;
6082
- if (!element.parentElement)
6083
- return void 0;
6084
- /* 如果元素有id属性,则直接返回id选择器 */
6085
- if (element.id)
6086
- return `#${element.id}`;
6087
- /* 递归地获取父元素的选择器 */
6088
- let selector = UtilsContext.getElementSelector(element.parentElement);
6089
- if (!selector) {
6090
- return element.tagName.toLowerCase();
6091
- }
6092
- /* 如果有多个相同类型的兄弟元素,则需要添加索引 */
6093
- if (element.parentElement.querySelectorAll(element.tagName).length > 1) {
6094
- const index = Array.prototype.indexOf.call(element.parentElement.children, element) + 1;
6095
- selector += ` > ${element.tagName.toLowerCase()}:nth-child(${index})`;
6096
- }
6097
- else {
6098
- selector += ` > ${element.tagName.toLowerCase()}`;
6099
- }
6100
- return selector;
6101
- }
6102
5922
  /**
6103
5923
  * 获取最大值
6104
5924
  * @example
@@ -6139,7 +5959,7 @@
6139
5959
  }
6140
5960
  getMaxZIndexNodeInfo(deviation = 1, target = this.windowApi.document, ignoreCallBack) {
6141
5961
  deviation = Number.isNaN(deviation) ? 1 : deviation;
6142
- const UtilsContext = this;
5962
+ const that = this;
6143
5963
  // 最大值 2147483647
6144
5964
  // const maxZIndex = Math.pow(2, 31) - 1;
6145
5965
  // 比较值 2000000000
@@ -6168,7 +5988,7 @@
6168
5988
  }
6169
5989
  }
6170
5990
  /** 元素的样式 */
6171
- const nodeStyle = UtilsContext.windowApi.window.getComputedStyle($ele);
5991
+ const nodeStyle = that.windowApi.window.getComputedStyle($ele);
6172
5992
  /* 不对position为static和display为none的元素进行获取它们的z-index */
6173
5993
  if (isVisibleNode(nodeStyle)) {
6174
5994
  const nodeZIndex = parseInt(nodeStyle.zIndex);
@@ -6242,7 +6062,7 @@
6242
6062
  * > 'Mozilla/5.0 (Linux; Android 10; MI 13 Build/OPR1.170623.027; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.3490.40 Mobile Safari/537.36'
6243
6063
  **/
6244
6064
  getRandomAndroidUA() {
6245
- const UtilsContext = this;
6065
+ const that = this;
6246
6066
  const mobileNameList = [
6247
6067
  "LDN-LX3",
6248
6068
  "RNE-L03",
@@ -6260,14 +6080,14 @@
6260
6080
  "MI 13 Build/OPR1.170623.027; wv",
6261
6081
  ];
6262
6082
  /* 安卓版本 */
6263
- const androidVersion = UtilsContext.getRandomValue(12, 14);
6083
+ const androidVersion = that.getRandomValue(12, 14);
6264
6084
  /* 手机型号 */
6265
- const randomMobile = UtilsContext.getRandomValue(mobileNameList);
6085
+ const randomMobile = that.getRandomValue(mobileNameList);
6266
6086
  /* chrome大版本号 */
6267
- const chromeVersion1 = UtilsContext.getRandomValue(120, 132);
6268
- const chromeVersion2 = UtilsContext.getRandomValue(0, 0);
6269
- const chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
6270
- const chromeVersion4 = UtilsContext.getRandomValue(1, 218);
6087
+ const chromeVersion1 = that.getRandomValue(130, 140);
6088
+ const chromeVersion2 = that.getRandomValue(0, 0);
6089
+ const chromeVersion3 = that.getRandomValue(2272, 6099);
6090
+ const chromeVersion4 = that.getRandomValue(1, 218);
6271
6091
  return `Mozilla/5.0 (Linux; Android ${androidVersion}; ${randomMobile}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion1}.${chromeVersion2}.${chromeVersion3}.${chromeVersion4} Mobile Safari/537.36`;
6272
6092
  }
6273
6093
  /**
@@ -6289,12 +6109,12 @@
6289
6109
  * > 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.5068.19 Safari/537.36'
6290
6110
  **/
6291
6111
  getRandomPCUA() {
6292
- const UtilsContext = this;
6112
+ const that = this;
6293
6113
  /* chrome大版本号 */
6294
- const chromeVersion1 = UtilsContext.getRandomValue(120, 132);
6295
- const chromeVersion2 = UtilsContext.getRandomValue(0, 0);
6296
- const chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
6297
- const chromeVersion4 = UtilsContext.getRandomValue(1, 218);
6114
+ const chromeVersion1 = that.getRandomValue(130, 140);
6115
+ const chromeVersion2 = that.getRandomValue(0, 0);
6116
+ const chromeVersion3 = that.getRandomValue(2272, 6099);
6117
+ const chromeVersion4 = that.getRandomValue(1, 218);
6298
6118
  return `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion1}.${chromeVersion2}.${chromeVersion3}.${chromeVersion4} Safari/537.36`;
6299
6119
  }
6300
6120
  getRandomValue(...args) {
@@ -6328,9 +6148,9 @@
6328
6148
  * @param element 需要获取的目标元素
6329
6149
  * @returns
6330
6150
  * @example
6331
- * Utils.getReactObj(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
6151
+ * Utils.getReactInstance(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
6332
6152
  */
6333
- getReactObj(element) {
6153
+ getReactInstance(element) {
6334
6154
  const result = {};
6335
6155
  if (element == null) {
6336
6156
  return result;
@@ -6340,7 +6160,9 @@
6340
6160
  if (domPropsName.startsWith("__react")) {
6341
6161
  const propsName = domPropsName.replace(/__(.+)\$.+/i, "$1");
6342
6162
  const propsValue = Reflect.get(element, domPropsName);
6343
- if (propsName in result) ;
6163
+ if (propsName in result) {
6164
+ console.error(`重复属性 ${domPropsName}`);
6165
+ }
6344
6166
  else {
6345
6167
  Reflect.set(result, propsName, propsValue);
6346
6168
  }
@@ -6395,8 +6217,8 @@
6395
6217
  return bytes.length;
6396
6218
  }
6397
6219
  getTextStorageSize(text, addType = true) {
6398
- const UtilsContext = this;
6399
- return UtilsContext.formatByteToSize(UtilsContext.getTextLength(text), addType);
6220
+ const that = this;
6221
+ return that.formatByteToSize(that.getTextLength(text), addType);
6400
6222
  }
6401
6223
  getThunderUrl(url) {
6402
6224
  if (url == null) {
@@ -6970,12 +6792,12 @@
6970
6792
  }
6971
6793
  isWebView_Via() {
6972
6794
  let result = true;
6973
- const UtilsContext = this;
6795
+ const that = this;
6974
6796
  if (typeof this.windowApi.top.window.via === "object") {
6975
6797
  for (const key in Object.values(this.windowApi.top.window.via)) {
6976
6798
  if (Reflect.has(this.windowApi.top.window.via, key)) {
6977
6799
  const objValueFunc = this.windowApi.top.window.via[key];
6978
- if (typeof objValueFunc === "function" && UtilsContext.isNativeFunc(objValueFunc)) {
6800
+ if (typeof objValueFunc === "function" && that.isNativeFunc(objValueFunc)) {
6979
6801
  result = true;
6980
6802
  }
6981
6803
  else {
@@ -6992,12 +6814,12 @@
6992
6814
  }
6993
6815
  isWebView_X() {
6994
6816
  let result = true;
6995
- const UtilsContext = this;
6817
+ const that = this;
6996
6818
  if (typeof this.windowApi.top.window.mbrowser === "object") {
6997
6819
  for (const key in Object.values(this.windowApi.top.window.mbrowser)) {
6998
6820
  if (Reflect.has(this.windowApi.top.window.mbrowser, key)) {
6999
6821
  const objValueFunc = this.windowApi.top.window.mbrowser[key];
7000
- if (typeof objValueFunc === "function" && UtilsContext.isNativeFunc(objValueFunc)) {
6822
+ if (typeof objValueFunc === "function" && that.isNativeFunc(objValueFunc)) {
7001
6823
  result = true;
7002
6824
  }
7003
6825
  else {
@@ -7017,8 +6839,10 @@
7017
6839
  throw new Error("Utils.parseObjectToArray 参数 target 必须为 object 类型");
7018
6840
  }
7019
6841
  let result = [];
7020
- Object.keys(target).forEach(function (keyName) {
7021
- result = result.concat(target[keyName]);
6842
+ const keys = Object.keys(target);
6843
+ keys.forEach(function (keyName) {
6844
+ const value = Reflect.get(target, keyName);
6845
+ result = result.concat(value);
7022
6846
  });
7023
6847
  return result;
7024
6848
  }
@@ -7091,7 +6915,7 @@
7091
6915
  return content;
7092
6916
  }
7093
6917
  mutationObserver(target, observer_config) {
7094
- const UtilsContext = this;
6918
+ const that = this;
7095
6919
  const default_obverser_config = {
7096
6920
  /* 监听到元素有反馈,需执行的函数 */
7097
6921
  callback: () => { },
@@ -7133,7 +6957,7 @@
7133
6957
  },
7134
6958
  immediate: false,
7135
6959
  };
7136
- observer_config = UtilsContext.assign(default_obverser_config, observer_config);
6960
+ observer_config = that.assign(default_obverser_config, observer_config);
7137
6961
  const windowMutationObserver = this.windowApi.window.MutationObserver ||
7138
6962
  this.windowApi.window.webkitMutationObserver ||
7139
6963
  this.windowApi.window.MozMutationObserver;
@@ -7149,7 +6973,7 @@
7149
6973
  mutationObserver.observe(item, observer_config.config);
7150
6974
  });
7151
6975
  }
7152
- else if (UtilsContext.isJQuery(target)) {
6976
+ else if (that.isJQuery(target)) {
7153
6977
  /* 传入的参数是jQuery对象 */
7154
6978
  target.each((_, item) => {
7155
6979
  mutationObserver.observe(item, observer_config.config);
@@ -7219,7 +7043,7 @@
7219
7043
  return utils;
7220
7044
  }
7221
7045
  noConflictFunc(needReleaseObject, needReleaseName, functionNameList = [], release = true) {
7222
- const UtilsContext = this;
7046
+ const that = this;
7223
7047
  if (typeof needReleaseObject !== "object") {
7224
7048
  throw new Error("Utils.noConflictFunc 参数 needReleaseObject 必须为 object 类型");
7225
7049
  }
@@ -7234,11 +7058,11 @@
7234
7058
  * 释放所有
7235
7059
  */
7236
7060
  function releaseAll() {
7237
- if (typeof UtilsContext.windowApi.window[needReleaseKey] !== "undefined") {
7061
+ if (typeof that.windowApi.window[needReleaseKey] !== "undefined") {
7238
7062
  /* 已存在 */
7239
7063
  return;
7240
7064
  }
7241
- UtilsContext.windowApi.window[needReleaseKey] = UtilsContext.deepClone(needReleaseObject);
7065
+ that.windowApi.window[needReleaseKey] = that.deepClone(needReleaseObject);
7242
7066
  Object.values(needReleaseObject).forEach((value) => {
7243
7067
  if (typeof value === "function") {
7244
7068
  needReleaseObject[value.name] = () => { };
@@ -7252,11 +7076,11 @@
7252
7076
  Array.from(functionNameList).forEach((item) => {
7253
7077
  Object.values(needReleaseObject).forEach((value) => {
7254
7078
  if (typeof value === "function") {
7255
- if (typeof UtilsContext.windowApi.window[needReleaseKey] === "undefined") {
7256
- UtilsContext.windowApi.window[needReleaseKey] = {};
7079
+ if (typeof that.windowApi.window[needReleaseKey] === "undefined") {
7080
+ that.windowApi.window[needReleaseKey] = {};
7257
7081
  }
7258
7082
  if (item === value.name) {
7259
- UtilsContext.windowApi.window[needReleaseKey][value.name] = needReleaseObject[value.name];
7083
+ that.windowApi.window[needReleaseKey][value.name] = needReleaseObject[value.name];
7260
7084
  needReleaseObject[value.name] = () => { };
7261
7085
  }
7262
7086
  }
@@ -7267,26 +7091,26 @@
7267
7091
  * 恢复所有
7268
7092
  */
7269
7093
  function recoveryAll() {
7270
- if (typeof UtilsContext.windowApi.window[needReleaseKey] === "undefined") {
7094
+ if (typeof that.windowApi.window[needReleaseKey] === "undefined") {
7271
7095
  /* 未存在 */
7272
7096
  return;
7273
7097
  }
7274
- Object.assign(needReleaseObject, UtilsContext.windowApi.window[needReleaseKey]);
7275
- Reflect.deleteProperty(UtilsContext.windowApi.window, "needReleaseKey");
7098
+ Object.assign(needReleaseObject, that.windowApi.window[needReleaseKey]);
7099
+ Reflect.deleteProperty(that.windowApi.window, "needReleaseKey");
7276
7100
  }
7277
7101
  /**
7278
7102
  * 恢复单个
7279
7103
  */
7280
7104
  function recoveryOne() {
7281
- if (typeof UtilsContext.windowApi.window[needReleaseKey] === "undefined") {
7105
+ if (typeof that.windowApi.window[needReleaseKey] === "undefined") {
7282
7106
  /* 未存在 */
7283
7107
  return;
7284
7108
  }
7285
7109
  Array.from(functionNameList).forEach((item) => {
7286
- if (UtilsContext.windowApi.window[needReleaseKey][item]) {
7287
- needReleaseObject[item] = UtilsContext.windowApi.window[needReleaseKey][item];
7288
- Reflect.deleteProperty(UtilsContext.windowApi.window[needReleaseKey], item);
7289
- if (Object.keys(UtilsContext.windowApi.window[needReleaseKey]).length === 0) {
7110
+ if (that.windowApi.window[needReleaseKey][item]) {
7111
+ needReleaseObject[item] = that.windowApi.window[needReleaseKey][item];
7112
+ Reflect.deleteProperty(that.windowApi.window[needReleaseKey], item);
7113
+ if (Object.keys(that.windowApi.window[needReleaseKey]).length === 0) {
7290
7114
  Reflect.deleteProperty(window, needReleaseKey);
7291
7115
  }
7292
7116
  }
@@ -7390,39 +7214,44 @@
7390
7214
  const parser = new DOMParser();
7391
7215
  return parser.parseFromString(text, mimeType);
7392
7216
  }
7393
- parseStringToRegExpString(text) {
7394
- if (typeof text !== "string") {
7395
- throw new TypeError("string必须是字符串");
7396
- }
7397
- const regString = text.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
7398
- return regString;
7399
- }
7400
- preventEvent(element, eventNameList = [], capture) {
7401
- function stopEvent(event) {
7402
- /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
7403
- event?.preventDefault();
7404
- /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
7405
- event?.stopPropagation();
7406
- /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
7407
- event?.stopImmediatePropagation();
7408
- return false;
7217
+ /**
7218
+ * 字符串转正则,用于把字符串中不规范的字符进行转义
7219
+ * @param text 需要进行转换的字符串
7220
+ * @param flags (可选)正则标志,默认`gi`
7221
+ * @example
7222
+ * Utils.toRegExp("^替换$");
7223
+ * > /^替换$/gi
7224
+ */
7225
+ toRegExp(text, flags = "gi") {
7226
+ let regExp;
7227
+ flags = flags.toLowerCase();
7228
+ if (typeof text === "string") {
7229
+ const pattern = this.toRegExpStr(text);
7230
+ regExp = new RegExp(pattern, flags);
7409
7231
  }
7410
- if (arguments.length === 1) {
7411
- /* 直接阻止事件 */
7412
- // eslint-disable-next-line prefer-rest-params
7413
- return stopEvent(arguments[0]);
7232
+ else if (text instanceof RegExp) {
7233
+ regExp = text;
7414
7234
  }
7415
7235
  else {
7416
- /* 添加对应的事件来阻止触发 */
7417
- if (typeof eventNameList === "string") {
7418
- eventNameList = [eventNameList];
7419
- }
7420
- eventNameList.forEach((eventName) => {
7421
- element.addEventListener(eventName, stopEvent, {
7422
- capture: Boolean(capture),
7423
- });
7424
- });
7236
+ throw new Error("Utils.toRegExp 参数text必须是string|Regexp类型");
7237
+ }
7238
+ return regExp;
7239
+ }
7240
+ /**
7241
+ * 将字符串进行正则转义
7242
+ * 例如:^替换$
7243
+ * 转换:\^替换\$
7244
+ * @param text 需要转义的字符串
7245
+ * @example
7246
+ * Utils.toRegExpStr("^替换$");
7247
+ * > \^替换\$
7248
+ */
7249
+ toRegExpStr(text) {
7250
+ if (typeof text !== "string") {
7251
+ throw new TypeError("toRegExpStr 参数text必须是string类型");
7425
7252
  }
7253
+ const regExpStr = text.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
7254
+ return regExpStr;
7426
7255
  }
7427
7256
  /**
7428
7257
  * 在canvas元素节点上绘制进度圆圈
@@ -7431,7 +7260,7 @@
7431
7260
  progress.draw();
7432
7261
  * **/
7433
7262
  Progress = Progress;
7434
- registerTrustClickEvent(isTrustValue = true, filter) {
7263
+ hookEvent_isTrusted(isTrustValue = true, filter) {
7435
7264
  function trustEvent(event) {
7436
7265
  return new Proxy(event, {
7437
7266
  get: function (target, property) {
@@ -7494,25 +7323,7 @@
7494
7323
  }
7495
7324
  return isNegative ? -reversedNum : reversedNum;
7496
7325
  }
7497
- selectElementText(element, childTextNode, startIndex, endIndex) {
7498
- const range = this.windowApi.document.createRange();
7499
- range.selectNodeContents(element);
7500
- if (childTextNode) {
7501
- if (childTextNode.nodeType !== Node.TEXT_NODE) {
7502
- throw new TypeError("childTextNode必须是#text元素");
7503
- }
7504
- if (startIndex != null && endIndex != null) {
7505
- range.setStart(childTextNode, startIndex);
7506
- range.setEnd(childTextNode, endIndex);
7507
- }
7508
- }
7509
- const selection = this.windowApi.globalThis.getSelection();
7510
- if (selection) {
7511
- selection.removeAllRanges();
7512
- selection.addRange(range);
7513
- }
7514
- }
7515
- setClip(data, info = {
7326
+ copy(data, info = {
7516
7327
  type: "text",
7517
7328
  mimetype: "text/plain",
7518
7329
  }) {
@@ -7534,7 +7345,7 @@
7534
7345
  else {
7535
7346
  textType = "text/plain";
7536
7347
  }
7537
- const UtilsContext = this;
7348
+ const that = this;
7538
7349
  class UtilsClipboard {
7539
7350
  #resolve;
7540
7351
  #copyData;
@@ -7585,15 +7396,15 @@
7585
7396
  */
7586
7397
  copyTextByTextArea() {
7587
7398
  try {
7588
- const copyElement = UtilsContext.windowApi.document.createElement("textarea");
7399
+ const copyElement = that.windowApi.document.createElement("textarea");
7589
7400
  copyElement.value = this.#copyData;
7590
7401
  copyElement.setAttribute("type", "text");
7591
7402
  copyElement.setAttribute("style", "opacity:0;position:absolute;");
7592
7403
  copyElement.setAttribute("readonly", "readonly");
7593
- UtilsContext.windowApi.document.body.appendChild(copyElement);
7404
+ that.windowApi.document.body.appendChild(copyElement);
7594
7405
  copyElement.select();
7595
- UtilsContext.windowApi.document.execCommand("copy");
7596
- UtilsContext.windowApi.document.body.removeChild(copyElement);
7406
+ that.windowApi.document.execCommand("copy");
7407
+ that.windowApi.document.body.removeChild(copyElement);
7597
7408
  return true;
7598
7409
  }
7599
7410
  catch (error) {
@@ -7663,18 +7474,88 @@
7663
7474
  }
7664
7475
  return new Promise((resolve) => {
7665
7476
  const utilsClipboard = new UtilsClipboard(resolve, data, textType);
7666
- if (UtilsContext.windowApi.document.hasFocus()) {
7477
+ if (that.windowApi.document.hasFocus()) {
7667
7478
  utilsClipboard.init();
7668
7479
  }
7669
7480
  else {
7670
- UtilsContext.windowApi.window.addEventListener("focus", () => {
7481
+ that.windowApi.window.addEventListener("focus", () => {
7671
7482
  utilsClipboard.init();
7672
7483
  }, { once: true });
7673
7484
  }
7674
7485
  });
7675
7486
  }
7487
+ /**
7488
+ * 获取剪贴板信息
7489
+ * @example
7490
+ * await Utils.getClipboardInfo();
7491
+ * > { error: null, content: "剪贴板内容" }
7492
+ */
7493
+ async getClipboardInfo() {
7494
+ return new Promise((resolve) => {
7495
+ /** 读取剪贴板 */
7496
+ function readClipboardText() {
7497
+ navigator.clipboard
7498
+ .readText()
7499
+ .then((clipboardText) => {
7500
+ resolve({
7501
+ error: null,
7502
+ content: clipboardText,
7503
+ });
7504
+ })
7505
+ .catch((error) => {
7506
+ resolve({
7507
+ error: error,
7508
+ content: "",
7509
+ });
7510
+ });
7511
+ }
7512
+ /** 申请读取剪贴板的权限 */
7513
+ function requestPermissionsWithClipboard() {
7514
+ navigator.permissions
7515
+ .query({
7516
+ name: "clipboard-read",
7517
+ })
7518
+ .then(() => {
7519
+ readClipboardText();
7520
+ })
7521
+ .catch(() => {
7522
+ /* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
7523
+ readClipboardText();
7524
+ });
7525
+ }
7526
+ /**
7527
+ * 检查当前环境是否支持读取剪贴板Api
7528
+ */
7529
+ function checkClipboardApi() {
7530
+ if (typeof navigator?.clipboard?.readText !== "function") {
7531
+ return false;
7532
+ }
7533
+ if (typeof navigator?.permissions?.query !== "function") {
7534
+ return false;
7535
+ }
7536
+ return true;
7537
+ }
7538
+ if (!checkClipboardApi()) {
7539
+ resolve({
7540
+ error: new Error("当前环境不支持读取剪贴板Api"),
7541
+ content: "",
7542
+ });
7543
+ return;
7544
+ }
7545
+ if (document.hasFocus()) {
7546
+ requestPermissionsWithClipboard();
7547
+ }
7548
+ else {
7549
+ window.addEventListener("focus", () => {
7550
+ requestPermissionsWithClipboard();
7551
+ }, {
7552
+ once: true,
7553
+ });
7554
+ }
7555
+ });
7556
+ }
7676
7557
  setTimeout(callback, delayTime = 0) {
7677
- const UtilsContext = this;
7558
+ const that = this;
7678
7559
  if (typeof callback !== "function" && typeof callback !== "string") {
7679
7560
  throw new TypeError("Utils.setTimeout 参数 callback 必须为 function|string 类型");
7680
7561
  }
@@ -7682,27 +7563,27 @@
7682
7563
  throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
7683
7564
  }
7684
7565
  return new Promise((resolve) => {
7685
- UtilsContext.workerSetTimeout(() => {
7686
- resolve(UtilsContext.tryCatch().run(callback));
7566
+ that.workerSetTimeout(() => {
7567
+ resolve(that.tryCatch().run(callback));
7687
7568
  }, delayTime);
7688
7569
  });
7689
7570
  }
7690
7571
  sleep(delayTime = 0) {
7691
- const UtilsContext = this;
7572
+ const that = this;
7692
7573
  if (typeof delayTime !== "number") {
7693
7574
  throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
7694
7575
  }
7695
7576
  return new Promise((resolve) => {
7696
- UtilsContext.workerSetTimeout(() => {
7577
+ that.workerSetTimeout(() => {
7697
7578
  resolve(void 0);
7698
7579
  }, delayTime);
7699
7580
  });
7700
7581
  }
7701
7582
  dragSlider(selector, offsetX = this.windowApi.window.innerWidth) {
7702
- const UtilsContext = this;
7583
+ const that = this;
7703
7584
  function initMouseEvent(eventName, offSetX, offSetY) {
7704
7585
  const win = typeof unsafeWindow === "undefined" ? globalThis : unsafeWindow;
7705
- const mouseEvent = UtilsContext.windowApi.document.createEvent("MouseEvents");
7586
+ const mouseEvent = that.windowApi.document.createEvent("MouseEvents");
7706
7587
  mouseEvent.initMouseEvent(eventName, true, true, win, 0, offSetX, offSetY, offSetX, offSetY, false, false, false, false, 0, null);
7707
7588
  return mouseEvent;
7708
7589
  }
@@ -7758,7 +7639,7 @@
7758
7639
  }
7759
7640
  }
7760
7641
  sortListByProperty(data, getPropertyValueFunc, sortByDesc = true) {
7761
- const UtilsContext = this;
7642
+ const that = this;
7762
7643
  if (typeof getPropertyValueFunc !== "function" && typeof getPropertyValueFunc !== "string") {
7763
7644
  throw new Error("Utils.sortListByProperty 参数 getPropertyValueFunc 必须为 function|string 类型");
7764
7645
  }
@@ -7770,9 +7651,8 @@
7770
7651
  };
7771
7652
  /**
7772
7653
  * 排序方法
7773
- * @param {any} after_obj
7774
- * @param {any} before_obj
7775
- * @returns
7654
+ * @param after_obj
7655
+ * @param before_obj
7776
7656
  */
7777
7657
  const sortFunc = function (after_obj, before_obj) {
7778
7658
  const beforeValue = getObjValue(before_obj); /* 前 */
@@ -7802,7 +7682,7 @@
7802
7682
  };
7803
7683
  /**
7804
7684
  * 排序元素方法
7805
- * @param nodeList 元素列表
7685
+ * @param nodeList 元素列表
7806
7686
  * @param getNodeListFunc 获取元素列表的函数
7807
7687
  */
7808
7688
  const sortNodeFunc = function (nodeList, getNodeListFunc) {
@@ -7840,7 +7720,7 @@
7840
7720
  if (Array.isArray(data)) {
7841
7721
  data.sort(sortFunc);
7842
7722
  }
7843
- else if (data instanceof NodeList || UtilsContext.isJQuery(data)) {
7723
+ else if (data instanceof NodeList || that.isJQuery(data)) {
7844
7724
  sortNodeFunc(data, getDataFunc);
7845
7725
  result = getDataFunc();
7846
7726
  }
@@ -7849,20 +7729,6 @@
7849
7729
  }
7850
7730
  return result;
7851
7731
  }
7852
- stringToRegular(targetString, flags = "ig") {
7853
- let reg;
7854
- flags = flags.toLowerCase();
7855
- if (typeof targetString === "string") {
7856
- reg = new RegExp(targetString.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"), flags);
7857
- }
7858
- else if (targetString instanceof RegExp) {
7859
- reg = targetString;
7860
- }
7861
- else {
7862
- throw new Error("Utils.stringToRegular 参数targetString必须是string|Regexp类型");
7863
- }
7864
- return reg;
7865
- }
7866
7732
  stringTitleToUpperCase(targetString, otherStrToLowerCase = false) {
7867
7733
  let newTargetString = targetString.slice(0, 1).toUpperCase();
7868
7734
  if (otherStrToLowerCase) {
@@ -7874,7 +7740,7 @@
7874
7740
  return newTargetString;
7875
7741
  }
7876
7742
  startsWith(target, searchString, position = 0) {
7877
- const UtilsContext = this;
7743
+ const that = this;
7878
7744
  if (position > target.length) {
7879
7745
  /* 超出目标字符串的长度 */
7880
7746
  return false;
@@ -7889,7 +7755,7 @@
7889
7755
  else if (Array.isArray(searchString)) {
7890
7756
  let flag = false;
7891
7757
  for (const searcStr of searchString) {
7892
- if (!UtilsContext.startsWith(target, searcStr, position)) {
7758
+ if (!that.startsWith(target, searcStr, position)) {
7893
7759
  flag = true;
7894
7760
  break;
7895
7761
  }
@@ -7898,13 +7764,18 @@
7898
7764
  }
7899
7765
  return Boolean(target.match(searchStringRegexp));
7900
7766
  }
7901
- stringTitleToLowerCase(targetString, otherStrToUpperCase = false) {
7902
- let newTargetString = targetString.slice(0, 1).toLowerCase();
7903
- if (otherStrToUpperCase) {
7904
- newTargetString = newTargetString + targetString.slice(1).toUpperCase();
7767
+ /**
7768
+ * 字符串首字母转小写
7769
+ * @param text 目标字符串
7770
+ * @param otherStrToLowerCase (可选)剩余部分字符串转大写,默认false
7771
+ */
7772
+ firstLetterToLowercase(text, otherToUpperCase = false) {
7773
+ let newTargetString = text.slice(0, 1).toLowerCase();
7774
+ if (otherToUpperCase) {
7775
+ newTargetString = newTargetString + text.slice(1).toUpperCase();
7905
7776
  }
7906
7777
  else {
7907
- newTargetString = newTargetString + targetString.slice(1);
7778
+ newTargetString = newTargetString + text.slice(1);
7908
7779
  }
7909
7780
  return newTargetString;
7910
7781
  }
@@ -7918,15 +7789,15 @@
7918
7789
  */
7919
7790
  toJSON = commonUtil.toJSON.bind(commonUtil);
7920
7791
  toSearchParamsStr(obj, addPrefix) {
7921
- const UtilsContext = this;
7792
+ const that = this;
7922
7793
  let searhParamsStr = "";
7923
7794
  if (Array.isArray(obj)) {
7924
7795
  obj.forEach((item) => {
7925
7796
  if (searhParamsStr === "") {
7926
- searhParamsStr += UtilsContext.toSearchParamsStr(item);
7797
+ searhParamsStr += that.toSearchParamsStr(item);
7927
7798
  }
7928
7799
  else {
7929
- searhParamsStr += `&${UtilsContext.toSearchParamsStr(item)}`;
7800
+ searhParamsStr += `&${that.toSearchParamsStr(item)}`;
7930
7801
  }
7931
7802
  });
7932
7803
  }
@@ -7981,341 +7852,33 @@
7981
7852
  }));
7982
7853
  }
7983
7854
  }
7855
+ /**
7856
+ * 等待函数数组全部执行完毕,注意,每个函数的顺序不是同步
7857
+ * @param data 需要遍历的数组
7858
+ * @param handleFunc 对该数组进行操作的函数,该函数的参数为数组格式的参数,[数组下标,数组项]
7859
+ * @example
7860
+ * await Utils.waitArrayLoopToEnd([callback,callback,callback],xxxcallback);
7861
+ **/
7984
7862
  waitArrayLoopToEnd(data, handleFunc) {
7985
- const UtilsContext = this;
7863
+ const that = this;
7986
7864
  if (typeof handleFunc !== "function" && typeof handleFunc !== "string") {
7987
7865
  throw new Error("Utils.waitArrayLoopToEnd 参数 handleDataFunction 必须为 function|string 类型");
7988
7866
  }
7989
7867
  return Promise.all(Array.from(data).map(async (item, index) => {
7990
- await UtilsContext.tryCatch(index, item).run(handleFunc);
7868
+ await that.tryCatch(index, item).run(handleFunc);
7991
7869
  }));
7992
7870
  }
7993
- wait(checkFn, timeout, parent) {
7994
- const UtilsContext = this;
7995
- const __timeout__ = typeof timeout === "number" ? timeout : 0;
7871
+ waitProperty(target, propertyName) {
7996
7872
  return new Promise((resolve) => {
7997
- const observer = UtilsContext.mutationObserver(parent || UtilsContext.windowApi.document, {
7998
- config: {
7999
- subtree: true,
8000
- childList: true,
8001
- attributes: true,
8002
- },
8003
- immediate: true,
8004
- callback(_, __observer__) {
8005
- const result = checkFn();
8006
- if (result.success) {
8007
- // 取消观察器
8008
- if (typeof __observer__?.disconnect === "function") {
8009
- __observer__.disconnect();
8010
- }
8011
- resolve(result.data);
8012
- }
8013
- },
8014
- });
8015
- if (__timeout__ > 0) {
8016
- UtilsContext.workerSetTimeout(() => {
8017
- // 取消观察器
8018
- if (typeof observer?.disconnect === "function") {
8019
- observer.disconnect();
8020
- }
8021
- resolve(null);
8022
- }, __timeout__);
8023
- }
8024
- });
8025
- }
8026
- waitNode(...args) {
8027
- // 过滤掉undefined
8028
- args = args.filter((arg) => arg !== void 0);
8029
- const UtilsContext = this;
8030
- // 选择器
8031
- const selector = args[0];
8032
- // 父元素(监听的元素)
8033
- let parent = UtilsContext.windowApi.document;
8034
- // 超时时间
8035
- let timeout = 0;
8036
- if (typeof args[0] !== "string" && !Array.isArray(args[0]) && typeof args[0] !== "function") {
8037
- throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]|Function");
8038
- }
8039
- if (args.length === 1) ;
8040
- else if (args.length === 2) {
8041
- const secondParam = args[1];
8042
- if (typeof secondParam === "number") {
8043
- // "div",10000
8044
- timeout = secondParam;
8045
- }
8046
- else if (typeof secondParam === "object" && secondParam instanceof Node) {
8047
- // "div",document
8048
- parent = secondParam;
8049
- }
8050
- else {
8051
- throw new TypeError("Utils.waitNode 第二个参数必须是number|Node");
8052
- }
8053
- }
8054
- else if (args.length === 3) {
8055
- // "div",document,10000
8056
- // 第二个参数,parent
8057
- const secondParam = args[1];
8058
- // 第三个参数,timeout
8059
- const thirdParam = args[2];
8060
- if (typeof secondParam === "object" && secondParam instanceof Node) {
8061
- parent = secondParam;
8062
- if (typeof thirdParam === "number") {
8063
- timeout = thirdParam;
8064
- }
8065
- else {
8066
- throw new TypeError("Utils.waitNode 第三个参数必须是number");
8067
- }
8068
- }
8069
- else {
8070
- throw new TypeError("Utils.waitNode 第二个参数必须是Node");
8071
- }
8072
- }
8073
- else {
8074
- throw new TypeError("Utils.waitNode 参数个数错误");
8075
- }
8076
- function getNode() {
8077
- if (Array.isArray(selector)) {
8078
- const result = [];
8079
- for (let index = 0; index < selector.length; index++) {
8080
- const node = domUtils.selector(selector[index]);
8081
- if (node) {
8082
- result.push(node);
8083
- }
8084
- }
8085
- if (result.length === selector.length) {
8086
- return result;
8087
- }
8088
- }
8089
- else if (typeof selector === "function") {
8090
- return selector();
8091
- }
8092
- else {
8093
- return domUtils.selector(selector, parent);
8094
- }
8095
- }
8096
- return UtilsContext.wait(() => {
8097
- const node = getNode();
8098
- if (node) {
8099
- return {
8100
- success: true,
8101
- data: node,
8102
- };
8103
- }
8104
- else {
8105
- return {
8106
- success: false,
8107
- data: node,
8108
- };
8109
- }
8110
- }, timeout, parent);
8111
- }
8112
- waitAnyNode(...args) {
8113
- // 过滤掉undefined
8114
- args = args.filter((arg) => arg !== void 0);
8115
- const UtilsContext = this;
8116
- // 选择器
8117
- const selectorList = args[0];
8118
- // 父元素(监听的元素)
8119
- let parent = UtilsContext.windowApi.document;
8120
- // 超时时间
8121
- let timeout = 0;
8122
- if (typeof args[0] !== "object" && !Array.isArray(args[0])) {
8123
- throw new TypeError("Utils.waitAnyNode 第一个参数必须是string[]");
8124
- }
8125
- if (args.length === 1) ;
8126
- else if (args.length === 2) {
8127
- const secondParam = args[1];
8128
- if (typeof secondParam === "number") {
8129
- // "div",10000
8130
- timeout = secondParam;
8131
- }
8132
- else if (typeof secondParam === "object" && secondParam instanceof Node) {
8133
- // "div",document
8134
- parent = secondParam;
8135
- }
8136
- else {
8137
- throw new TypeError("Utils.waitAnyNode 第二个参数必须是number|Node");
8138
- }
8139
- }
8140
- else if (args.length === 3) {
8141
- // "div",document,10000
8142
- // 第二个参数,parent
8143
- const secondParam = args[1];
8144
- // 第三个参数,timeout
8145
- const thirdParam = args[2];
8146
- if (typeof secondParam === "object" && secondParam instanceof Node) {
8147
- parent = secondParam;
8148
- if (typeof thirdParam === "number") {
8149
- timeout = thirdParam;
8150
- }
8151
- else {
8152
- throw new TypeError("Utils.waitAnyNode 第三个参数必须是number");
8153
- }
8154
- }
8155
- else {
8156
- throw new TypeError("Utils.waitAnyNode 第二个参数必须是Node");
8157
- }
8158
- }
8159
- else {
8160
- throw new TypeError("Utils.waitAnyNode 参数个数错误");
8161
- }
8162
- const promiseList = selectorList.map((selector) => {
8163
- return UtilsContext.waitNode(selector, parent, timeout);
8164
- });
8165
- return Promise.any(promiseList);
8166
- }
8167
- waitNodeList(...args) {
8168
- // 过滤掉undefined
8169
- args = args.filter((arg) => arg !== void 0);
8170
- const UtilsContext = this;
8171
- // 选择器数组
8172
- const selector = args[0];
8173
- // 父元素(监听的元素)
8174
- let parent = UtilsContext.windowApi.document;
8175
- // 超时时间
8176
- let timeout = 0;
8177
- if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
8178
- throw new TypeError("Utils.waitNodeList 第一个参数必须是string|string[]");
8179
- }
8180
- if (args.length === 1) ;
8181
- else if (args.length === 2) {
8182
- const secondParam = args[1];
8183
- if (typeof secondParam === "number") {
8184
- // "div",10000
8185
- timeout = secondParam;
8186
- }
8187
- else if (typeof secondParam === "object" && secondParam instanceof Node) {
8188
- // "div",document
8189
- parent = secondParam;
7873
+ let obj = target;
7874
+ if (typeof target === "function") {
7875
+ obj = target();
8190
7876
  }
8191
- else {
8192
- throw new TypeError("Utils.waitNodeList 第二个参数必须是number|Node");
8193
- }
8194
- }
8195
- else if (args.length === 3) {
8196
- // "div",document,10000
8197
- // 第二个参数,parent
8198
- const secondParam = args[1];
8199
- // 第三个参数,timeout
8200
- const thirdParam = args[2];
8201
- if (typeof secondParam === "object" && secondParam instanceof Node) {
8202
- parent = secondParam;
8203
- if (typeof thirdParam === "number") {
8204
- timeout = thirdParam;
8205
- }
8206
- else {
8207
- throw new TypeError("Utils.waitNodeList 第三个参数必须是number");
8208
- }
7877
+ if (Reflect.has(obj, propertyName)) {
7878
+ resolve(obj[propertyName]);
8209
7879
  }
8210
7880
  else {
8211
- throw new TypeError("Utils.waitNodeList 第二个参数必须是Node");
8212
- }
8213
- }
8214
- else {
8215
- throw new TypeError("Utils.waitNodeList 参数个数错误");
8216
- }
8217
- function getNodeList() {
8218
- if (Array.isArray(selector)) {
8219
- const result = [];
8220
- for (let index = 0; index < selector.length; index++) {
8221
- const nodeList = domUtils.selectorAll(selector[index], parent);
8222
- if (nodeList.length) {
8223
- result.push(nodeList);
8224
- }
8225
- }
8226
- if (result.length === selector.length) {
8227
- return result;
8228
- }
8229
- }
8230
- else {
8231
- const nodeList = domUtils.selectorAll(selector, parent);
8232
- if (nodeList.length) {
8233
- return nodeList;
8234
- }
8235
- }
8236
- }
8237
- return UtilsContext.wait(() => {
8238
- const node = getNodeList();
8239
- if (node) {
8240
- return {
8241
- success: true,
8242
- data: node,
8243
- };
8244
- }
8245
- else {
8246
- return {
8247
- success: false,
8248
- data: node,
8249
- };
8250
- }
8251
- }, timeout, parent);
8252
- }
8253
- waitAnyNodeList(...args) {
8254
- // 过滤掉undefined
8255
- args = args.filter((arg) => arg !== void 0);
8256
- const UtilsContext = this;
8257
- // 选择器数组
8258
- const selectorList = args[0];
8259
- // 父元素(监听的元素)
8260
- let parent = UtilsContext.windowApi.document;
8261
- // 超时时间
8262
- let timeout = 0;
8263
- if (!Array.isArray(args[0])) {
8264
- throw new TypeError("Utils.waitAnyNodeList 第一个参数必须是string[]");
8265
- }
8266
- if (args.length === 1) ;
8267
- else if (args.length === 2) {
8268
- const secondParam = args[1];
8269
- if (typeof secondParam === "number") {
8270
- // "div",10000
8271
- timeout = secondParam;
8272
- }
8273
- else if (typeof secondParam === "object" && secondParam instanceof Node) {
8274
- // "div",document
8275
- parent = secondParam;
8276
- }
8277
- else {
8278
- throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是number|Node");
8279
- }
8280
- }
8281
- else if (args.length === 3) {
8282
- // "div",document,10000
8283
- // 第二个参数,parent
8284
- const secondParam = args[1];
8285
- // 第三个参数,timeout
8286
- const thirdParam = args[2];
8287
- if (typeof secondParam === "object" && secondParam instanceof Node) {
8288
- parent = secondParam;
8289
- if (typeof thirdParam === "number") {
8290
- timeout = thirdParam;
8291
- }
8292
- else {
8293
- throw new TypeError("Utils.waitAnyNodeList 第三个参数必须是number");
8294
- }
8295
- }
8296
- else {
8297
- throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是Node");
8298
- }
8299
- }
8300
- else {
8301
- throw new TypeError("Utils.waitAnyNodeList 参数个数错误");
8302
- }
8303
- const promiseList = selectorList.map((selector) => {
8304
- return UtilsContext.waitNodeList(selector, parent, timeout);
8305
- });
8306
- return Promise.any(promiseList);
8307
- }
8308
- waitProperty(checkObj, checkPropertyName) {
8309
- return new Promise((resolve) => {
8310
- let obj = checkObj;
8311
- if (typeof checkObj === "function") {
8312
- obj = checkObj();
8313
- }
8314
- if (Reflect.has(obj, checkPropertyName)) {
8315
- resolve(obj[checkPropertyName]);
8316
- }
8317
- else {
8318
- Object.defineProperty(obj, checkPropertyName, {
7881
+ Object.defineProperty(obj, propertyName, {
8319
7882
  set: function (value) {
8320
7883
  try {
8321
7884
  resolve(value);
@@ -8328,49 +7891,48 @@
8328
7891
  }
8329
7892
  });
8330
7893
  }
8331
- waitPropertyByInterval(checkObj, checkPropertyName, intervalTimer = 250, maxTime = -1) {
8332
- const UtilsContext = this;
8333
- if (checkObj == null) {
7894
+ waitPropertyByInterval(checkFn, propertyName, intervalTimer = 250, maxTime = -1) {
7895
+ const that = this;
7896
+ if (checkFn == null) {
8334
7897
  throw new TypeError("checkObj 不能为空对象 ");
8335
7898
  }
8336
7899
  let isResolve = false;
8337
7900
  return new Promise((resolve, reject) => {
8338
- const interval = UtilsContext.workerSetInterval(() => {
8339
- let obj = checkObj;
8340
- if (typeof checkObj === "function") {
8341
- obj = checkObj();
7901
+ const interval = that.workerSetInterval(() => {
7902
+ let inst = checkFn;
7903
+ if (typeof checkFn === "function") {
7904
+ inst = checkFn();
8342
7905
  }
8343
- if (typeof obj !== "object") {
7906
+ if (typeof inst !== "object") {
8344
7907
  return;
8345
7908
  }
8346
- if (obj == null) {
7909
+ if (inst == null) {
8347
7910
  return;
8348
7911
  }
8349
- if ((typeof checkPropertyName === "function" && checkPropertyName(obj)) ||
8350
- Reflect.has(obj, checkPropertyName)) {
7912
+ if ((typeof propertyName === "function" && propertyName(inst)) || Reflect.has(inst, propertyName)) {
8351
7913
  isResolve = true;
8352
- UtilsContext.workerClearInterval(interval);
8353
- resolve(obj[checkPropertyName]);
7914
+ that.workerClearInterval(interval);
7915
+ resolve(inst[propertyName]);
8354
7916
  }
8355
7917
  }, intervalTimer);
8356
7918
  if (maxTime !== -1) {
8357
- UtilsContext.workerSetTimeout(() => {
7919
+ that.workerSetTimeout(() => {
8358
7920
  if (!isResolve) {
8359
- UtilsContext.workerClearInterval(interval);
7921
+ that.workerClearInterval(interval);
8360
7922
  reject();
8361
7923
  }
8362
7924
  }, maxTime);
8363
7925
  }
8364
7926
  });
8365
7927
  }
8366
- async waitVueByInterval(element, propertyName, timer = 250, maxTime = -1, vueName = "__vue__") {
8367
- if (element == null) {
7928
+ async waitVueByInterval($el, propertyName, timer = 250, maxTime = -1, vueName = "__vue__") {
7929
+ if ($el == null) {
8368
7930
  throw new Error("Utils.waitVueByInterval 参数element 不能为空");
8369
7931
  }
8370
7932
  let flag = false;
8371
- const UtilsContext = this;
7933
+ const that = this;
8372
7934
  try {
8373
- await UtilsContext.waitPropertyByInterval(element, function (targetElement) {
7935
+ await that.waitPropertyByInterval($el, function (targetElement) {
8374
7936
  if (targetElement == null) {
8375
7937
  return false;
8376
7938
  }
@@ -8380,25 +7942,24 @@
8380
7942
  if (propertyName == null) {
8381
7943
  return true;
8382
7944
  }
8383
- const vueObject = targetElement[vueName];
7945
+ const $vueEl = targetElement[vueName];
8384
7946
  if (typeof propertyName === "string") {
8385
- if (propertyName in vueObject) {
7947
+ if (propertyName in $vueEl) {
8386
7948
  flag = true;
8387
7949
  return true;
8388
7950
  }
8389
7951
  }
8390
7952
  else {
8391
7953
  /* Function */
8392
- if (propertyName(vueObject)) {
7954
+ if (propertyName($vueEl)) {
8393
7955
  flag = true;
8394
7956
  return true;
8395
7957
  }
8396
7958
  }
8397
7959
  return false;
8398
7960
  }, timer, maxTime);
8399
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8400
7961
  }
8401
- catch (error) {
7962
+ catch {
8402
7963
  return flag;
8403
7964
  }
8404
7965
  return flag;
@@ -8635,73 +8196,6 @@
8635
8196
  this.windowApi.clearInterval(timeId);
8636
8197
  }
8637
8198
  }
8638
- /**
8639
- * 获取剪贴板信息
8640
- */
8641
- async getClipboardInfo() {
8642
- return new Promise((resolve) => {
8643
- /** 读取剪贴板 */
8644
- function readClipboardText() {
8645
- navigator.clipboard
8646
- .readText()
8647
- .then((clipboardText) => {
8648
- resolve({
8649
- error: null,
8650
- content: clipboardText,
8651
- });
8652
- })
8653
- .catch((error) => {
8654
- resolve({
8655
- error: error,
8656
- content: "",
8657
- });
8658
- });
8659
- }
8660
- /** 申请读取剪贴板的权限 */
8661
- function requestPermissionsWithClipboard() {
8662
- navigator.permissions
8663
- .query({
8664
- name: "clipboard-read",
8665
- })
8666
- .then(() => {
8667
- readClipboardText();
8668
- })
8669
- .catch(() => {
8670
- /* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
8671
- readClipboardText();
8672
- });
8673
- }
8674
- /**
8675
- * 检查当前环境是否支持读取剪贴板Api
8676
- */
8677
- function checkClipboardApi() {
8678
- if (typeof navigator?.clipboard?.readText !== "function") {
8679
- return false;
8680
- }
8681
- if (typeof navigator?.permissions?.query !== "function") {
8682
- return false;
8683
- }
8684
- return true;
8685
- }
8686
- if (!checkClipboardApi()) {
8687
- resolve({
8688
- error: new Error("当前环境不支持读取剪贴板Api"),
8689
- content: "",
8690
- });
8691
- return;
8692
- }
8693
- if (document.hasFocus()) {
8694
- requestPermissionsWithClipboard();
8695
- }
8696
- else {
8697
- window.addEventListener("focus", () => {
8698
- requestPermissionsWithClipboard();
8699
- }, {
8700
- once: true,
8701
- });
8702
- }
8703
- });
8704
- }
8705
8199
  }
8706
8200
  const utils = new Utils();
8707
8201