@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.esm.js CHANGED
@@ -519,18 +519,12 @@ class UtilsGMCookie {
519
519
  }
520
520
  return findValue;
521
521
  }
522
- /**
523
- * 获取多组Cookie
524
- * @param option 配置
525
- * @param callback 获取操作后的回调
526
- * + cookies object[]
527
- * + error string|undefined
528
- **/
529
522
  list(option, callback) {
530
523
  if (option == null) {
531
524
  throw new Error("Utils.GMCookie.list 参数不能为空");
532
525
  }
533
526
  const resultData = [];
527
+ let error;
534
528
  try {
535
529
  let defaultOption = {
536
530
  url: this.windowApi.window.location.href,
@@ -564,14 +558,18 @@ class UtilsGMCookie {
564
558
  });
565
559
  }
566
560
  });
567
- if (typeof callback === "function") {
568
- callback(resultData);
569
- }
570
561
  }
571
- catch (error) {
572
- if (typeof callback === "function") {
573
- callback(resultData, error);
574
- }
562
+ catch (e) {
563
+ error = e;
564
+ }
565
+ if (typeof callback === "function") {
566
+ callback(resultData, error);
567
+ }
568
+ else {
569
+ return {
570
+ cookies: resultData,
571
+ error: error,
572
+ };
575
573
  }
576
574
  }
577
575
  /**
@@ -1780,7 +1778,7 @@ class GMMenu {
1780
1778
  },
1781
1779
  /**
1782
1780
  * 注册油猴菜单
1783
- * @param menuOptions 如果存在,使用它
1781
+ * @param menuOptions (可选)菜单配置
1784
1782
  */
1785
1783
  register(menuOptions) {
1786
1784
  const that = this;
@@ -1804,7 +1802,8 @@ class GMMenu {
1804
1802
  },
1805
1803
  /**
1806
1804
  * 获取本地存储菜单键值
1807
- * @param {string} key 键
1805
+ * @param key 键
1806
+ * @param defaultValue 默认值
1808
1807
  */
1809
1808
  getLocalMenuData(key, defaultValue) {
1810
1809
  const localData = this.context.GM_Api.getValue(this.$data.key, {});
@@ -1827,7 +1826,7 @@ class GMMenu {
1827
1826
  },
1828
1827
  /**
1829
1828
  * 处理初始化配置
1830
- * @param menuOption
1829
+ * @param menuOption 菜单配置
1831
1830
  */
1832
1831
  handleInitDetail(menuOption) {
1833
1832
  menuOption.enable = Boolean(this.getLocalMenuData(menuOption.key, menuOption.enable));
@@ -1845,7 +1844,7 @@ class GMMenu {
1845
1844
  },
1846
1845
  /**
1847
1846
  * 对菜单数据进行处理
1848
- * @param menuOption
1847
+ * @param menuOption 菜单配置
1849
1848
  */
1850
1849
  handleMenuData(menuOption) {
1851
1850
  const that = this;
@@ -1933,6 +1932,9 @@ class GMMenu {
1933
1932
  return this.$data.data.find((item) => item.handleData.key === menuKey)?.handleData;
1934
1933
  },
1935
1934
  };
1935
+ /**
1936
+ * @param details 菜单配置
1937
+ */
1936
1938
  constructor(details) {
1937
1939
  this.GM_Api.getValue = details.GM_getValue;
1938
1940
  this.GM_Api.setValue = details.GM_setValue;
@@ -2020,14 +2022,6 @@ class GMMenu {
2020
2022
  delete(menuId) {
2021
2023
  this.GM_Api.unregisterMenuCommand(menuId);
2022
2024
  }
2023
- /**
2024
- * 根据键值获取enable值
2025
- * @param menuKey 菜单-键key
2026
- * @deprecated
2027
- */
2028
- get(menuKey) {
2029
- return this.getEnable(menuKey);
2030
- }
2031
2025
  /**
2032
2026
  * 根据键值获取enable值
2033
2027
  * @param menuKey 菜单-键key
@@ -2185,11 +2179,11 @@ class Hooks {
2185
2179
  return "";
2186
2180
  }
2187
2181
  try {
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);
2182
+ new Function("_context", "_funcName", "hookFunc", `_context[_funcName] = function ${_funcName}() {
2183
+ let args = Array.prototype.slice.call(arguments, 0);
2184
+ let obj = this;
2185
+ hookFunc.apply(obj, args);
2186
+ return _context['realFunc_${_funcName}'].apply(obj, args);
2193
2187
  };`)(_context, _funcName, hookFunc);
2194
2188
  _context[_funcName].prototype.isHooked = true;
2195
2189
  return true;
@@ -5478,38 +5472,15 @@ class DOMUtils {
5478
5472
  }
5479
5473
  const domUtils = new DOMUtils();
5480
5474
 
5475
+ const version = "2.9.0";
5476
+
5481
5477
  class Utils {
5482
5478
  windowApi;
5483
5479
  constructor(option) {
5484
5480
  this.windowApi = new WindowApi(option);
5485
5481
  }
5486
5482
  /** 版本号 */
5487
- version = "2025.9.14";
5488
- addStyle(cssText) {
5489
- if (typeof cssText !== "string") {
5490
- throw new Error("Utils.addStyle 参数cssText 必须为String类型");
5491
- }
5492
- const cssNode = this.windowApi.document.createElement("style");
5493
- cssNode.setAttribute("type", "text/css");
5494
- cssNode.innerHTML = cssText;
5495
- if (this.windowApi.document.head) {
5496
- /* 插入head最后 */
5497
- this.windowApi.document.head.appendChild(cssNode);
5498
- }
5499
- else if (this.windowApi.document.body) {
5500
- /* 插入body后 */
5501
- this.windowApi.document.body.appendChild(cssNode);
5502
- }
5503
- else if (this.windowApi.document.documentElement.childNodes.length === 0) {
5504
- /* 插入#html第一个元素后 */
5505
- this.windowApi.document.documentElement.appendChild(cssNode);
5506
- }
5507
- else {
5508
- /* 插入head前面 */
5509
- this.windowApi.document.documentElement.insertBefore(cssNode, this.windowApi.document.documentElement.childNodes[0]);
5510
- }
5511
- return cssNode;
5512
- }
5483
+ version = version;
5513
5484
  /**
5514
5485
  * JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
5515
5486
  * @param target 目标数据
@@ -5527,7 +5498,7 @@ class Utils {
5527
5498
  */
5528
5499
  assign = commonUtil.assign.bind(commonUtil);
5529
5500
  async asyncReplaceAll(string, pattern, asyncFn) {
5530
- const UtilsContext = this;
5501
+ const that = this;
5531
5502
  if (typeof string !== "string") {
5532
5503
  throw new TypeError("string必须是字符串");
5533
5504
  }
@@ -5536,7 +5507,7 @@ class Utils {
5536
5507
  }
5537
5508
  let reg;
5538
5509
  if (typeof pattern === "string") {
5539
- reg = new RegExp(UtilsContext.parseStringToRegExpString(pattern), "g");
5510
+ reg = new RegExp(that.toRegExpStr(pattern), "g");
5540
5511
  }
5541
5512
  else if (pattern instanceof RegExp) {
5542
5513
  if (!pattern.global) {
@@ -5568,7 +5539,7 @@ class Utils {
5568
5539
  * ajax劫持库,支持xhr和fetch劫持。
5569
5540
  * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5570
5541
  * + 作者:cxxjackie
5571
- * + 版本:1.4.7
5542
+ * + 版本:1.4.8
5572
5543
  * + 旧版本:1.2.4
5573
5544
  * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
5574
5545
  * @param useOldVersion 是否使用旧版本,默认false
@@ -5598,41 +5569,6 @@ class Utils {
5598
5569
  canvasElement.dispatchEvent(new MouseEvent("mousedown", eventInit));
5599
5570
  canvasElement.dispatchEvent(new MouseEvent("mouseup", eventInit));
5600
5571
  }
5601
- checkUserClickInNode(element) {
5602
- const UtilsContext = this;
5603
- if (!UtilsContext.isDOM(element)) {
5604
- throw new Error("Utils.checkUserClickInNode 参数 targetNode 必须为 Element|Node 类型");
5605
- }
5606
- const clickEvent = UtilsContext.windowApi.window.event;
5607
- const touchEvent = UtilsContext.windowApi.window.event;
5608
- const $click = clickEvent?.composedPath()?.[0];
5609
- // 点击的x坐标
5610
- const clickPosX = clickEvent?.clientX != null ? clickEvent.clientX : touchEvent.touches[0].clientX;
5611
- // 点击的y坐标
5612
- const clickPosY = clickEvent?.clientY != null ? clickEvent.clientY : touchEvent.touches[0].clientY;
5613
- const {
5614
- /* 要检测的元素的相对屏幕的横坐标最左边 */
5615
- left: elementPosXLeft,
5616
- /* 要检测的元素的相对屏幕的横坐标最右边 */
5617
- right: elementPosXRight,
5618
- /* 要检测的元素的相对屏幕的纵坐标最上边 */
5619
- top: elementPosYTop,
5620
- /* 要检测的元素的相对屏幕的纵坐标最下边 */
5621
- bottom: elementPosYBottom, } = element.getBoundingClientRect();
5622
- if (clickPosX >= elementPosXLeft &&
5623
- clickPosX <= elementPosXRight &&
5624
- clickPosY >= elementPosYTop &&
5625
- clickPosY <= elementPosYBottom) {
5626
- return true;
5627
- }
5628
- else if (($click && element.contains($click)) || $click == element) {
5629
- /* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
5630
- return true;
5631
- }
5632
- else {
5633
- return false;
5634
- }
5635
- }
5636
5572
  /**
5637
5573
  * 复制formData数据
5638
5574
  * @param formData 需要clone的数据
@@ -5680,33 +5616,14 @@ class Utils {
5680
5616
  deepClone = commonUtil.deepClone.bind(commonUtil);
5681
5617
  debounce(fn, delay = 0) {
5682
5618
  let timer = null;
5683
- const UtilsContext = this;
5619
+ const that = this;
5684
5620
  return function (...args) {
5685
- UtilsContext.workerClearTimeout(timer);
5686
- timer = UtilsContext.workerSetTimeout(function () {
5687
- fn.apply(UtilsContext, args);
5621
+ that.workerClearTimeout(timer);
5622
+ timer = that.workerSetTimeout(function () {
5623
+ fn.apply(that, args);
5688
5624
  }, delay);
5689
5625
  };
5690
5626
  }
5691
- deleteParentNode(element, targetSelector) {
5692
- const UtilsContext = this;
5693
- if (element == null) {
5694
- return;
5695
- }
5696
- if (!UtilsContext.isDOM(element)) {
5697
- throw new Error("Utils.deleteParentNode 参数 target 必须为 Node|HTMLElement 类型");
5698
- }
5699
- if (typeof targetSelector !== "string") {
5700
- throw new Error("Utils.deleteParentNode 参数 targetSelector 必须为 string 类型");
5701
- }
5702
- let result = false;
5703
- const needRemoveDOM = domUtils.closest(element, targetSelector);
5704
- if (needRemoveDOM) {
5705
- needRemoveDOM.remove();
5706
- result = true;
5707
- }
5708
- return result;
5709
- }
5710
5627
  /**
5711
5628
  * 字典
5712
5629
  * @example
@@ -5720,25 +5637,8 @@ class Utils {
5720
5637
  * dictionary.concat(dictionary2);
5721
5638
  **/
5722
5639
  Dictionary = UtilsDictionary;
5723
- dispatchEvent(element, eventName, details) {
5724
- // let UtilsContext = this;
5725
- let eventNameList = [];
5726
- if (typeof eventName === "string") {
5727
- eventNameList = [eventName];
5728
- }
5729
- if (Array.isArray(eventName)) {
5730
- eventNameList = [...eventName];
5731
- }
5732
- eventNameList.forEach((_eventName_) => {
5733
- const event = new Event(_eventName_);
5734
- if (details) {
5735
- Object.assign(event, details);
5736
- }
5737
- element.dispatchEvent(event);
5738
- });
5739
- }
5740
5640
  downloadBase64(base64Data, fileName, isIFrame = false) {
5741
- const UtilsContext = this;
5641
+ const that = this;
5742
5642
  if (typeof base64Data !== "string") {
5743
5643
  throw new Error("Utils.downloadBase64 参数 base64Data 必须为 string 类型");
5744
5644
  }
@@ -5747,13 +5647,13 @@ class Utils {
5747
5647
  }
5748
5648
  if (isIFrame) {
5749
5649
  /* 使用iframe */
5750
- const iframeElement = this.windowApi.document.createElement("iframe");
5751
- iframeElement.style.display = "none";
5752
- iframeElement.src = base64Data;
5753
- this.windowApi.document.body.appendChild(iframeElement);
5754
- UtilsContext.workerSetTimeout(() => {
5755
- iframeElement.contentWindow.document.execCommand("SaveAs", true, fileName);
5756
- this.windowApi.document.body.removeChild(iframeElement);
5650
+ const $iframe = this.windowApi.document.createElement("iframe");
5651
+ $iframe.style.display = "none";
5652
+ $iframe.src = base64Data;
5653
+ (this.windowApi.document.body || this.windowApi.document.documentElement).appendChild($iframe);
5654
+ that.workerSetTimeout(() => {
5655
+ $iframe.contentWindow.document.execCommand("SaveAs", true, fileName);
5656
+ (this.windowApi.document.body || this.windowApi.document.documentElement).removeChild($iframe);
5757
5657
  }, 100);
5758
5658
  }
5759
5659
  else {
@@ -5803,50 +5703,6 @@ class Utils {
5803
5703
  }
5804
5704
  return strFound ? true : false;
5805
5705
  }
5806
- *findElementsWithText(element, text, filter) {
5807
- const that = this;
5808
- if (element.outerHTML.includes(text)) {
5809
- if (element.children.length === 0) {
5810
- const filterResult = typeof filter === "function" ? filter(element) : false;
5811
- if (!filterResult) {
5812
- yield element;
5813
- }
5814
- }
5815
- else {
5816
- const textElement = Array.from(element.childNodes).filter((ele) => ele.nodeType === Node.TEXT_NODE);
5817
- for (const $child of textElement) {
5818
- if ($child.textContent.includes(text)) {
5819
- const filterResult = typeof filter === "function" ? filter(element) : false;
5820
- if (!filterResult) {
5821
- yield $child;
5822
- }
5823
- }
5824
- }
5825
- }
5826
- }
5827
- for (let index = 0; index < element.children.length; index++) {
5828
- const $child = element.children[index];
5829
- yield* that.findElementsWithText($child, text, filter);
5830
- }
5831
- }
5832
- /**
5833
- * 判断该元素是否可见,如果不可见,向上找它的父元素直至找到可见的元素
5834
- * @param element
5835
- * @example
5836
- * let visibleElement = Utils.findVisibleElement(document.querySelector("a.xx"));
5837
- * > <HTMLElement>
5838
- */
5839
- findVisibleElement(element) {
5840
- let currentElement = element;
5841
- while (currentElement) {
5842
- const elementRect = currentElement.getBoundingClientRect();
5843
- if (elementRect.length) {
5844
- return currentElement;
5845
- }
5846
- currentElement = currentElement.parentElement;
5847
- }
5848
- return null;
5849
- }
5850
5706
  formatByteToSize(byteSize, addType = true) {
5851
5707
  byteSize = parseInt(byteSize.toString());
5852
5708
  if (isNaN(byteSize)) {
@@ -5895,9 +5751,9 @@ class Utils {
5895
5751
  }
5896
5752
  getNonNullValue(...args) {
5897
5753
  let resultValue = args[args.length - 1];
5898
- const UtilsContext = this;
5754
+ const that = this;
5899
5755
  for (const argValue of args) {
5900
- if (UtilsContext.isNotNull(argValue)) {
5756
+ if (that.isNotNull(argValue)) {
5901
5757
  resultValue = argValue;
5902
5758
  break;
5903
5759
  }
@@ -5971,20 +5827,8 @@ class Utils {
5971
5827
  * > 测试
5972
5828
  */
5973
5829
  GBKEncoder = GBKEncoder;
5974
- /**
5975
- * 获取 transitionend 的在各个浏览器的兼容名
5976
- */
5977
- getTransitionEndNameList() {
5978
- return ["webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend"];
5979
- }
5980
- /**
5981
- * 获取 animationend 的在各个浏览器的兼容名
5982
- */
5983
- getAnimationEndNameList() {
5984
- return ["webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend"];
5985
- }
5986
- getArrayLastValue(targetObj) {
5987
- return targetObj[targetObj.length - 1];
5830
+ getArrayLastValue(target) {
5831
+ return target[target.length - 1];
5988
5832
  }
5989
5833
  getArrayRealValue(...args) {
5990
5834
  let result = null;
@@ -6069,30 +5913,6 @@ class Utils {
6069
5913
  }
6070
5914
  return diffValue;
6071
5915
  }
6072
- getElementSelector(element) {
6073
- const UtilsContext = this;
6074
- if (!element)
6075
- return void 0;
6076
- if (!element.parentElement)
6077
- return void 0;
6078
- /* 如果元素有id属性,则直接返回id选择器 */
6079
- if (element.id)
6080
- return `#${element.id}`;
6081
- /* 递归地获取父元素的选择器 */
6082
- let selector = UtilsContext.getElementSelector(element.parentElement);
6083
- if (!selector) {
6084
- return element.tagName.toLowerCase();
6085
- }
6086
- /* 如果有多个相同类型的兄弟元素,则需要添加索引 */
6087
- if (element.parentElement.querySelectorAll(element.tagName).length > 1) {
6088
- const index = Array.prototype.indexOf.call(element.parentElement.children, element) + 1;
6089
- selector += ` > ${element.tagName.toLowerCase()}:nth-child(${index})`;
6090
- }
6091
- else {
6092
- selector += ` > ${element.tagName.toLowerCase()}`;
6093
- }
6094
- return selector;
6095
- }
6096
5916
  /**
6097
5917
  * 获取最大值
6098
5918
  * @example
@@ -6133,7 +5953,7 @@ class Utils {
6133
5953
  }
6134
5954
  getMaxZIndexNodeInfo(deviation = 1, target = this.windowApi.document, ignoreCallBack) {
6135
5955
  deviation = Number.isNaN(deviation) ? 1 : deviation;
6136
- const UtilsContext = this;
5956
+ const that = this;
6137
5957
  // 最大值 2147483647
6138
5958
  // const maxZIndex = Math.pow(2, 31) - 1;
6139
5959
  // 比较值 2000000000
@@ -6162,7 +5982,7 @@ class Utils {
6162
5982
  }
6163
5983
  }
6164
5984
  /** 元素的样式 */
6165
- const nodeStyle = UtilsContext.windowApi.window.getComputedStyle($ele);
5985
+ const nodeStyle = that.windowApi.window.getComputedStyle($ele);
6166
5986
  /* 不对position为static和display为none的元素进行获取它们的z-index */
6167
5987
  if (isVisibleNode(nodeStyle)) {
6168
5988
  const nodeZIndex = parseInt(nodeStyle.zIndex);
@@ -6236,7 +6056,7 @@ class Utils {
6236
6056
  * > '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'
6237
6057
  **/
6238
6058
  getRandomAndroidUA() {
6239
- const UtilsContext = this;
6059
+ const that = this;
6240
6060
  const mobileNameList = [
6241
6061
  "LDN-LX3",
6242
6062
  "RNE-L03",
@@ -6254,14 +6074,14 @@ class Utils {
6254
6074
  "MI 13 Build/OPR1.170623.027; wv",
6255
6075
  ];
6256
6076
  /* 安卓版本 */
6257
- const androidVersion = UtilsContext.getRandomValue(12, 14);
6077
+ const androidVersion = that.getRandomValue(12, 14);
6258
6078
  /* 手机型号 */
6259
- const randomMobile = UtilsContext.getRandomValue(mobileNameList);
6079
+ const randomMobile = that.getRandomValue(mobileNameList);
6260
6080
  /* chrome大版本号 */
6261
- const chromeVersion1 = UtilsContext.getRandomValue(120, 132);
6262
- const chromeVersion2 = UtilsContext.getRandomValue(0, 0);
6263
- const chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
6264
- const chromeVersion4 = UtilsContext.getRandomValue(1, 218);
6081
+ const chromeVersion1 = that.getRandomValue(130, 140);
6082
+ const chromeVersion2 = that.getRandomValue(0, 0);
6083
+ const chromeVersion3 = that.getRandomValue(2272, 6099);
6084
+ const chromeVersion4 = that.getRandomValue(1, 218);
6265
6085
  return `Mozilla/5.0 (Linux; Android ${androidVersion}; ${randomMobile}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion1}.${chromeVersion2}.${chromeVersion3}.${chromeVersion4} Mobile Safari/537.36`;
6266
6086
  }
6267
6087
  /**
@@ -6283,12 +6103,12 @@ class Utils {
6283
6103
  * > 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.5068.19 Safari/537.36'
6284
6104
  **/
6285
6105
  getRandomPCUA() {
6286
- const UtilsContext = this;
6106
+ const that = this;
6287
6107
  /* chrome大版本号 */
6288
- const chromeVersion1 = UtilsContext.getRandomValue(120, 132);
6289
- const chromeVersion2 = UtilsContext.getRandomValue(0, 0);
6290
- const chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
6291
- const chromeVersion4 = UtilsContext.getRandomValue(1, 218);
6108
+ const chromeVersion1 = that.getRandomValue(130, 140);
6109
+ const chromeVersion2 = that.getRandomValue(0, 0);
6110
+ const chromeVersion3 = that.getRandomValue(2272, 6099);
6111
+ const chromeVersion4 = that.getRandomValue(1, 218);
6292
6112
  return `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion1}.${chromeVersion2}.${chromeVersion3}.${chromeVersion4} Safari/537.36`;
6293
6113
  }
6294
6114
  getRandomValue(...args) {
@@ -6322,9 +6142,9 @@ class Utils {
6322
6142
  * @param element 需要获取的目标元素
6323
6143
  * @returns
6324
6144
  * @example
6325
- * Utils.getReactObj(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
6145
+ * Utils.getReactInstance(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
6326
6146
  */
6327
- getReactObj(element) {
6147
+ getReactInstance(element) {
6328
6148
  const result = {};
6329
6149
  if (element == null) {
6330
6150
  return result;
@@ -6334,7 +6154,9 @@ class Utils {
6334
6154
  if (domPropsName.startsWith("__react")) {
6335
6155
  const propsName = domPropsName.replace(/__(.+)\$.+/i, "$1");
6336
6156
  const propsValue = Reflect.get(element, domPropsName);
6337
- if (propsName in result) ;
6157
+ if (propsName in result) {
6158
+ console.error(`重复属性 ${domPropsName}`);
6159
+ }
6338
6160
  else {
6339
6161
  Reflect.set(result, propsName, propsValue);
6340
6162
  }
@@ -6389,8 +6211,8 @@ class Utils {
6389
6211
  return bytes.length;
6390
6212
  }
6391
6213
  getTextStorageSize(text, addType = true) {
6392
- const UtilsContext = this;
6393
- return UtilsContext.formatByteToSize(UtilsContext.getTextLength(text), addType);
6214
+ const that = this;
6215
+ return that.formatByteToSize(that.getTextLength(text), addType);
6394
6216
  }
6395
6217
  getThunderUrl(url) {
6396
6218
  if (url == null) {
@@ -6964,12 +6786,12 @@ class Utils {
6964
6786
  }
6965
6787
  isWebView_Via() {
6966
6788
  let result = true;
6967
- const UtilsContext = this;
6789
+ const that = this;
6968
6790
  if (typeof this.windowApi.top.window.via === "object") {
6969
6791
  for (const key in Object.values(this.windowApi.top.window.via)) {
6970
6792
  if (Reflect.has(this.windowApi.top.window.via, key)) {
6971
6793
  const objValueFunc = this.windowApi.top.window.via[key];
6972
- if (typeof objValueFunc === "function" && UtilsContext.isNativeFunc(objValueFunc)) {
6794
+ if (typeof objValueFunc === "function" && that.isNativeFunc(objValueFunc)) {
6973
6795
  result = true;
6974
6796
  }
6975
6797
  else {
@@ -6986,12 +6808,12 @@ class Utils {
6986
6808
  }
6987
6809
  isWebView_X() {
6988
6810
  let result = true;
6989
- const UtilsContext = this;
6811
+ const that = this;
6990
6812
  if (typeof this.windowApi.top.window.mbrowser === "object") {
6991
6813
  for (const key in Object.values(this.windowApi.top.window.mbrowser)) {
6992
6814
  if (Reflect.has(this.windowApi.top.window.mbrowser, key)) {
6993
6815
  const objValueFunc = this.windowApi.top.window.mbrowser[key];
6994
- if (typeof objValueFunc === "function" && UtilsContext.isNativeFunc(objValueFunc)) {
6816
+ if (typeof objValueFunc === "function" && that.isNativeFunc(objValueFunc)) {
6995
6817
  result = true;
6996
6818
  }
6997
6819
  else {
@@ -7011,8 +6833,10 @@ class Utils {
7011
6833
  throw new Error("Utils.parseObjectToArray 参数 target 必须为 object 类型");
7012
6834
  }
7013
6835
  let result = [];
7014
- Object.keys(target).forEach(function (keyName) {
7015
- result = result.concat(target[keyName]);
6836
+ const keys = Object.keys(target);
6837
+ keys.forEach(function (keyName) {
6838
+ const value = Reflect.get(target, keyName);
6839
+ result = result.concat(value);
7016
6840
  });
7017
6841
  return result;
7018
6842
  }
@@ -7085,7 +6909,7 @@ class Utils {
7085
6909
  return content;
7086
6910
  }
7087
6911
  mutationObserver(target, observer_config) {
7088
- const UtilsContext = this;
6912
+ const that = this;
7089
6913
  const default_obverser_config = {
7090
6914
  /* 监听到元素有反馈,需执行的函数 */
7091
6915
  callback: () => { },
@@ -7127,7 +6951,7 @@ class Utils {
7127
6951
  },
7128
6952
  immediate: false,
7129
6953
  };
7130
- observer_config = UtilsContext.assign(default_obverser_config, observer_config);
6954
+ observer_config = that.assign(default_obverser_config, observer_config);
7131
6955
  const windowMutationObserver = this.windowApi.window.MutationObserver ||
7132
6956
  this.windowApi.window.webkitMutationObserver ||
7133
6957
  this.windowApi.window.MozMutationObserver;
@@ -7143,7 +6967,7 @@ class Utils {
7143
6967
  mutationObserver.observe(item, observer_config.config);
7144
6968
  });
7145
6969
  }
7146
- else if (UtilsContext.isJQuery(target)) {
6970
+ else if (that.isJQuery(target)) {
7147
6971
  /* 传入的参数是jQuery对象 */
7148
6972
  target.each((_, item) => {
7149
6973
  mutationObserver.observe(item, observer_config.config);
@@ -7213,7 +7037,7 @@ class Utils {
7213
7037
  return utils;
7214
7038
  }
7215
7039
  noConflictFunc(needReleaseObject, needReleaseName, functionNameList = [], release = true) {
7216
- const UtilsContext = this;
7040
+ const that = this;
7217
7041
  if (typeof needReleaseObject !== "object") {
7218
7042
  throw new Error("Utils.noConflictFunc 参数 needReleaseObject 必须为 object 类型");
7219
7043
  }
@@ -7228,11 +7052,11 @@ class Utils {
7228
7052
  * 释放所有
7229
7053
  */
7230
7054
  function releaseAll() {
7231
- if (typeof UtilsContext.windowApi.window[needReleaseKey] !== "undefined") {
7055
+ if (typeof that.windowApi.window[needReleaseKey] !== "undefined") {
7232
7056
  /* 已存在 */
7233
7057
  return;
7234
7058
  }
7235
- UtilsContext.windowApi.window[needReleaseKey] = UtilsContext.deepClone(needReleaseObject);
7059
+ that.windowApi.window[needReleaseKey] = that.deepClone(needReleaseObject);
7236
7060
  Object.values(needReleaseObject).forEach((value) => {
7237
7061
  if (typeof value === "function") {
7238
7062
  needReleaseObject[value.name] = () => { };
@@ -7246,11 +7070,11 @@ class Utils {
7246
7070
  Array.from(functionNameList).forEach((item) => {
7247
7071
  Object.values(needReleaseObject).forEach((value) => {
7248
7072
  if (typeof value === "function") {
7249
- if (typeof UtilsContext.windowApi.window[needReleaseKey] === "undefined") {
7250
- UtilsContext.windowApi.window[needReleaseKey] = {};
7073
+ if (typeof that.windowApi.window[needReleaseKey] === "undefined") {
7074
+ that.windowApi.window[needReleaseKey] = {};
7251
7075
  }
7252
7076
  if (item === value.name) {
7253
- UtilsContext.windowApi.window[needReleaseKey][value.name] = needReleaseObject[value.name];
7077
+ that.windowApi.window[needReleaseKey][value.name] = needReleaseObject[value.name];
7254
7078
  needReleaseObject[value.name] = () => { };
7255
7079
  }
7256
7080
  }
@@ -7261,26 +7085,26 @@ class Utils {
7261
7085
  * 恢复所有
7262
7086
  */
7263
7087
  function recoveryAll() {
7264
- if (typeof UtilsContext.windowApi.window[needReleaseKey] === "undefined") {
7088
+ if (typeof that.windowApi.window[needReleaseKey] === "undefined") {
7265
7089
  /* 未存在 */
7266
7090
  return;
7267
7091
  }
7268
- Object.assign(needReleaseObject, UtilsContext.windowApi.window[needReleaseKey]);
7269
- Reflect.deleteProperty(UtilsContext.windowApi.window, "needReleaseKey");
7092
+ Object.assign(needReleaseObject, that.windowApi.window[needReleaseKey]);
7093
+ Reflect.deleteProperty(that.windowApi.window, "needReleaseKey");
7270
7094
  }
7271
7095
  /**
7272
7096
  * 恢复单个
7273
7097
  */
7274
7098
  function recoveryOne() {
7275
- if (typeof UtilsContext.windowApi.window[needReleaseKey] === "undefined") {
7099
+ if (typeof that.windowApi.window[needReleaseKey] === "undefined") {
7276
7100
  /* 未存在 */
7277
7101
  return;
7278
7102
  }
7279
7103
  Array.from(functionNameList).forEach((item) => {
7280
- if (UtilsContext.windowApi.window[needReleaseKey][item]) {
7281
- needReleaseObject[item] = UtilsContext.windowApi.window[needReleaseKey][item];
7282
- Reflect.deleteProperty(UtilsContext.windowApi.window[needReleaseKey], item);
7283
- if (Object.keys(UtilsContext.windowApi.window[needReleaseKey]).length === 0) {
7104
+ if (that.windowApi.window[needReleaseKey][item]) {
7105
+ needReleaseObject[item] = that.windowApi.window[needReleaseKey][item];
7106
+ Reflect.deleteProperty(that.windowApi.window[needReleaseKey], item);
7107
+ if (Object.keys(that.windowApi.window[needReleaseKey]).length === 0) {
7284
7108
  Reflect.deleteProperty(window, needReleaseKey);
7285
7109
  }
7286
7110
  }
@@ -7384,39 +7208,44 @@ class Utils {
7384
7208
  const parser = new DOMParser();
7385
7209
  return parser.parseFromString(text, mimeType);
7386
7210
  }
7387
- parseStringToRegExpString(text) {
7388
- if (typeof text !== "string") {
7389
- throw new TypeError("string必须是字符串");
7390
- }
7391
- const regString = text.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
7392
- return regString;
7393
- }
7394
- preventEvent(element, eventNameList = [], capture) {
7395
- function stopEvent(event) {
7396
- /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
7397
- event?.preventDefault();
7398
- /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
7399
- event?.stopPropagation();
7400
- /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
7401
- event?.stopImmediatePropagation();
7402
- return false;
7211
+ /**
7212
+ * 字符串转正则,用于把字符串中不规范的字符进行转义
7213
+ * @param text 需要进行转换的字符串
7214
+ * @param flags (可选)正则标志,默认`gi`
7215
+ * @example
7216
+ * Utils.toRegExp("^替换$");
7217
+ * > /^替换$/gi
7218
+ */
7219
+ toRegExp(text, flags = "gi") {
7220
+ let regExp;
7221
+ flags = flags.toLowerCase();
7222
+ if (typeof text === "string") {
7223
+ const pattern = this.toRegExpStr(text);
7224
+ regExp = new RegExp(pattern, flags);
7403
7225
  }
7404
- if (arguments.length === 1) {
7405
- /* 直接阻止事件 */
7406
- // eslint-disable-next-line prefer-rest-params
7407
- return stopEvent(arguments[0]);
7226
+ else if (text instanceof RegExp) {
7227
+ regExp = text;
7408
7228
  }
7409
7229
  else {
7410
- /* 添加对应的事件来阻止触发 */
7411
- if (typeof eventNameList === "string") {
7412
- eventNameList = [eventNameList];
7413
- }
7414
- eventNameList.forEach((eventName) => {
7415
- element.addEventListener(eventName, stopEvent, {
7416
- capture: Boolean(capture),
7417
- });
7418
- });
7230
+ throw new Error("Utils.toRegExp 参数text必须是string|Regexp类型");
7231
+ }
7232
+ return regExp;
7233
+ }
7234
+ /**
7235
+ * 将字符串进行正则转义
7236
+ * 例如:^替换$
7237
+ * 转换:\^替换\$
7238
+ * @param text 需要转义的字符串
7239
+ * @example
7240
+ * Utils.toRegExpStr("^替换$");
7241
+ * > \^替换\$
7242
+ */
7243
+ toRegExpStr(text) {
7244
+ if (typeof text !== "string") {
7245
+ throw new TypeError("toRegExpStr 参数text必须是string类型");
7419
7246
  }
7247
+ const regExpStr = text.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
7248
+ return regExpStr;
7420
7249
  }
7421
7250
  /**
7422
7251
  * 在canvas元素节点上绘制进度圆圈
@@ -7425,7 +7254,7 @@ class Utils {
7425
7254
  progress.draw();
7426
7255
  * **/
7427
7256
  Progress = Progress;
7428
- registerTrustClickEvent(isTrustValue = true, filter) {
7257
+ hookEvent_isTrusted(isTrustValue = true, filter) {
7429
7258
  function trustEvent(event) {
7430
7259
  return new Proxy(event, {
7431
7260
  get: function (target, property) {
@@ -7488,25 +7317,7 @@ class Utils {
7488
7317
  }
7489
7318
  return isNegative ? -reversedNum : reversedNum;
7490
7319
  }
7491
- selectElementText(element, childTextNode, startIndex, endIndex) {
7492
- const range = this.windowApi.document.createRange();
7493
- range.selectNodeContents(element);
7494
- if (childTextNode) {
7495
- if (childTextNode.nodeType !== Node.TEXT_NODE) {
7496
- throw new TypeError("childTextNode必须是#text元素");
7497
- }
7498
- if (startIndex != null && endIndex != null) {
7499
- range.setStart(childTextNode, startIndex);
7500
- range.setEnd(childTextNode, endIndex);
7501
- }
7502
- }
7503
- const selection = this.windowApi.globalThis.getSelection();
7504
- if (selection) {
7505
- selection.removeAllRanges();
7506
- selection.addRange(range);
7507
- }
7508
- }
7509
- setClip(data, info = {
7320
+ copy(data, info = {
7510
7321
  type: "text",
7511
7322
  mimetype: "text/plain",
7512
7323
  }) {
@@ -7528,7 +7339,7 @@ class Utils {
7528
7339
  else {
7529
7340
  textType = "text/plain";
7530
7341
  }
7531
- const UtilsContext = this;
7342
+ const that = this;
7532
7343
  class UtilsClipboard {
7533
7344
  #resolve;
7534
7345
  #copyData;
@@ -7579,15 +7390,15 @@ class Utils {
7579
7390
  */
7580
7391
  copyTextByTextArea() {
7581
7392
  try {
7582
- const copyElement = UtilsContext.windowApi.document.createElement("textarea");
7393
+ const copyElement = that.windowApi.document.createElement("textarea");
7583
7394
  copyElement.value = this.#copyData;
7584
7395
  copyElement.setAttribute("type", "text");
7585
7396
  copyElement.setAttribute("style", "opacity:0;position:absolute;");
7586
7397
  copyElement.setAttribute("readonly", "readonly");
7587
- UtilsContext.windowApi.document.body.appendChild(copyElement);
7398
+ that.windowApi.document.body.appendChild(copyElement);
7588
7399
  copyElement.select();
7589
- UtilsContext.windowApi.document.execCommand("copy");
7590
- UtilsContext.windowApi.document.body.removeChild(copyElement);
7400
+ that.windowApi.document.execCommand("copy");
7401
+ that.windowApi.document.body.removeChild(copyElement);
7591
7402
  return true;
7592
7403
  }
7593
7404
  catch (error) {
@@ -7657,18 +7468,88 @@ class Utils {
7657
7468
  }
7658
7469
  return new Promise((resolve) => {
7659
7470
  const utilsClipboard = new UtilsClipboard(resolve, data, textType);
7660
- if (UtilsContext.windowApi.document.hasFocus()) {
7471
+ if (that.windowApi.document.hasFocus()) {
7661
7472
  utilsClipboard.init();
7662
7473
  }
7663
7474
  else {
7664
- UtilsContext.windowApi.window.addEventListener("focus", () => {
7475
+ that.windowApi.window.addEventListener("focus", () => {
7665
7476
  utilsClipboard.init();
7666
7477
  }, { once: true });
7667
7478
  }
7668
7479
  });
7669
7480
  }
7481
+ /**
7482
+ * 获取剪贴板信息
7483
+ * @example
7484
+ * await Utils.getClipboardInfo();
7485
+ * > { error: null, content: "剪贴板内容" }
7486
+ */
7487
+ async getClipboardInfo() {
7488
+ return new Promise((resolve) => {
7489
+ /** 读取剪贴板 */
7490
+ function readClipboardText() {
7491
+ navigator.clipboard
7492
+ .readText()
7493
+ .then((clipboardText) => {
7494
+ resolve({
7495
+ error: null,
7496
+ content: clipboardText,
7497
+ });
7498
+ })
7499
+ .catch((error) => {
7500
+ resolve({
7501
+ error: error,
7502
+ content: "",
7503
+ });
7504
+ });
7505
+ }
7506
+ /** 申请读取剪贴板的权限 */
7507
+ function requestPermissionsWithClipboard() {
7508
+ navigator.permissions
7509
+ .query({
7510
+ name: "clipboard-read",
7511
+ })
7512
+ .then(() => {
7513
+ readClipboardText();
7514
+ })
7515
+ .catch(() => {
7516
+ /* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
7517
+ readClipboardText();
7518
+ });
7519
+ }
7520
+ /**
7521
+ * 检查当前环境是否支持读取剪贴板Api
7522
+ */
7523
+ function checkClipboardApi() {
7524
+ if (typeof navigator?.clipboard?.readText !== "function") {
7525
+ return false;
7526
+ }
7527
+ if (typeof navigator?.permissions?.query !== "function") {
7528
+ return false;
7529
+ }
7530
+ return true;
7531
+ }
7532
+ if (!checkClipboardApi()) {
7533
+ resolve({
7534
+ error: new Error("当前环境不支持读取剪贴板Api"),
7535
+ content: "",
7536
+ });
7537
+ return;
7538
+ }
7539
+ if (document.hasFocus()) {
7540
+ requestPermissionsWithClipboard();
7541
+ }
7542
+ else {
7543
+ window.addEventListener("focus", () => {
7544
+ requestPermissionsWithClipboard();
7545
+ }, {
7546
+ once: true,
7547
+ });
7548
+ }
7549
+ });
7550
+ }
7670
7551
  setTimeout(callback, delayTime = 0) {
7671
- const UtilsContext = this;
7552
+ const that = this;
7672
7553
  if (typeof callback !== "function" && typeof callback !== "string") {
7673
7554
  throw new TypeError("Utils.setTimeout 参数 callback 必须为 function|string 类型");
7674
7555
  }
@@ -7676,27 +7557,27 @@ class Utils {
7676
7557
  throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
7677
7558
  }
7678
7559
  return new Promise((resolve) => {
7679
- UtilsContext.workerSetTimeout(() => {
7680
- resolve(UtilsContext.tryCatch().run(callback));
7560
+ that.workerSetTimeout(() => {
7561
+ resolve(that.tryCatch().run(callback));
7681
7562
  }, delayTime);
7682
7563
  });
7683
7564
  }
7684
7565
  sleep(delayTime = 0) {
7685
- const UtilsContext = this;
7566
+ const that = this;
7686
7567
  if (typeof delayTime !== "number") {
7687
7568
  throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
7688
7569
  }
7689
7570
  return new Promise((resolve) => {
7690
- UtilsContext.workerSetTimeout(() => {
7571
+ that.workerSetTimeout(() => {
7691
7572
  resolve(void 0);
7692
7573
  }, delayTime);
7693
7574
  });
7694
7575
  }
7695
7576
  dragSlider(selector, offsetX = this.windowApi.window.innerWidth) {
7696
- const UtilsContext = this;
7577
+ const that = this;
7697
7578
  function initMouseEvent(eventName, offSetX, offSetY) {
7698
7579
  const win = typeof unsafeWindow === "undefined" ? globalThis : unsafeWindow;
7699
- const mouseEvent = UtilsContext.windowApi.document.createEvent("MouseEvents");
7580
+ const mouseEvent = that.windowApi.document.createEvent("MouseEvents");
7700
7581
  mouseEvent.initMouseEvent(eventName, true, true, win, 0, offSetX, offSetY, offSetX, offSetY, false, false, false, false, 0, null);
7701
7582
  return mouseEvent;
7702
7583
  }
@@ -7752,7 +7633,7 @@ class Utils {
7752
7633
  }
7753
7634
  }
7754
7635
  sortListByProperty(data, getPropertyValueFunc, sortByDesc = true) {
7755
- const UtilsContext = this;
7636
+ const that = this;
7756
7637
  if (typeof getPropertyValueFunc !== "function" && typeof getPropertyValueFunc !== "string") {
7757
7638
  throw new Error("Utils.sortListByProperty 参数 getPropertyValueFunc 必须为 function|string 类型");
7758
7639
  }
@@ -7764,9 +7645,8 @@ class Utils {
7764
7645
  };
7765
7646
  /**
7766
7647
  * 排序方法
7767
- * @param {any} after_obj
7768
- * @param {any} before_obj
7769
- * @returns
7648
+ * @param after_obj
7649
+ * @param before_obj
7770
7650
  */
7771
7651
  const sortFunc = function (after_obj, before_obj) {
7772
7652
  const beforeValue = getObjValue(before_obj); /* 前 */
@@ -7796,7 +7676,7 @@ class Utils {
7796
7676
  };
7797
7677
  /**
7798
7678
  * 排序元素方法
7799
- * @param nodeList 元素列表
7679
+ * @param nodeList 元素列表
7800
7680
  * @param getNodeListFunc 获取元素列表的函数
7801
7681
  */
7802
7682
  const sortNodeFunc = function (nodeList, getNodeListFunc) {
@@ -7834,7 +7714,7 @@ class Utils {
7834
7714
  if (Array.isArray(data)) {
7835
7715
  data.sort(sortFunc);
7836
7716
  }
7837
- else if (data instanceof NodeList || UtilsContext.isJQuery(data)) {
7717
+ else if (data instanceof NodeList || that.isJQuery(data)) {
7838
7718
  sortNodeFunc(data, getDataFunc);
7839
7719
  result = getDataFunc();
7840
7720
  }
@@ -7843,20 +7723,6 @@ class Utils {
7843
7723
  }
7844
7724
  return result;
7845
7725
  }
7846
- stringToRegular(targetString, flags = "ig") {
7847
- let reg;
7848
- flags = flags.toLowerCase();
7849
- if (typeof targetString === "string") {
7850
- reg = new RegExp(targetString.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"), flags);
7851
- }
7852
- else if (targetString instanceof RegExp) {
7853
- reg = targetString;
7854
- }
7855
- else {
7856
- throw new Error("Utils.stringToRegular 参数targetString必须是string|Regexp类型");
7857
- }
7858
- return reg;
7859
- }
7860
7726
  stringTitleToUpperCase(targetString, otherStrToLowerCase = false) {
7861
7727
  let newTargetString = targetString.slice(0, 1).toUpperCase();
7862
7728
  if (otherStrToLowerCase) {
@@ -7868,7 +7734,7 @@ class Utils {
7868
7734
  return newTargetString;
7869
7735
  }
7870
7736
  startsWith(target, searchString, position = 0) {
7871
- const UtilsContext = this;
7737
+ const that = this;
7872
7738
  if (position > target.length) {
7873
7739
  /* 超出目标字符串的长度 */
7874
7740
  return false;
@@ -7883,7 +7749,7 @@ class Utils {
7883
7749
  else if (Array.isArray(searchString)) {
7884
7750
  let flag = false;
7885
7751
  for (const searcStr of searchString) {
7886
- if (!UtilsContext.startsWith(target, searcStr, position)) {
7752
+ if (!that.startsWith(target, searcStr, position)) {
7887
7753
  flag = true;
7888
7754
  break;
7889
7755
  }
@@ -7892,13 +7758,18 @@ class Utils {
7892
7758
  }
7893
7759
  return Boolean(target.match(searchStringRegexp));
7894
7760
  }
7895
- stringTitleToLowerCase(targetString, otherStrToUpperCase = false) {
7896
- let newTargetString = targetString.slice(0, 1).toLowerCase();
7897
- if (otherStrToUpperCase) {
7898
- newTargetString = newTargetString + targetString.slice(1).toUpperCase();
7761
+ /**
7762
+ * 字符串首字母转小写
7763
+ * @param text 目标字符串
7764
+ * @param otherStrToLowerCase (可选)剩余部分字符串转大写,默认false
7765
+ */
7766
+ firstLetterToLowercase(text, otherToUpperCase = false) {
7767
+ let newTargetString = text.slice(0, 1).toLowerCase();
7768
+ if (otherToUpperCase) {
7769
+ newTargetString = newTargetString + text.slice(1).toUpperCase();
7899
7770
  }
7900
7771
  else {
7901
- newTargetString = newTargetString + targetString.slice(1);
7772
+ newTargetString = newTargetString + text.slice(1);
7902
7773
  }
7903
7774
  return newTargetString;
7904
7775
  }
@@ -7912,15 +7783,15 @@ class Utils {
7912
7783
  */
7913
7784
  toJSON = commonUtil.toJSON.bind(commonUtil);
7914
7785
  toSearchParamsStr(obj, addPrefix) {
7915
- const UtilsContext = this;
7786
+ const that = this;
7916
7787
  let searhParamsStr = "";
7917
7788
  if (Array.isArray(obj)) {
7918
7789
  obj.forEach((item) => {
7919
7790
  if (searhParamsStr === "") {
7920
- searhParamsStr += UtilsContext.toSearchParamsStr(item);
7791
+ searhParamsStr += that.toSearchParamsStr(item);
7921
7792
  }
7922
7793
  else {
7923
- searhParamsStr += `&${UtilsContext.toSearchParamsStr(item)}`;
7794
+ searhParamsStr += `&${that.toSearchParamsStr(item)}`;
7924
7795
  }
7925
7796
  });
7926
7797
  }
@@ -7975,341 +7846,33 @@ class Utils {
7975
7846
  }));
7976
7847
  }
7977
7848
  }
7849
+ /**
7850
+ * 等待函数数组全部执行完毕,注意,每个函数的顺序不是同步
7851
+ * @param data 需要遍历的数组
7852
+ * @param handleFunc 对该数组进行操作的函数,该函数的参数为数组格式的参数,[数组下标,数组项]
7853
+ * @example
7854
+ * await Utils.waitArrayLoopToEnd([callback,callback,callback],xxxcallback);
7855
+ **/
7978
7856
  waitArrayLoopToEnd(data, handleFunc) {
7979
- const UtilsContext = this;
7857
+ const that = this;
7980
7858
  if (typeof handleFunc !== "function" && typeof handleFunc !== "string") {
7981
7859
  throw new Error("Utils.waitArrayLoopToEnd 参数 handleDataFunction 必须为 function|string 类型");
7982
7860
  }
7983
7861
  return Promise.all(Array.from(data).map(async (item, index) => {
7984
- await UtilsContext.tryCatch(index, item).run(handleFunc);
7862
+ await that.tryCatch(index, item).run(handleFunc);
7985
7863
  }));
7986
7864
  }
7987
- wait(checkFn, timeout, parent) {
7988
- const UtilsContext = this;
7989
- const __timeout__ = typeof timeout === "number" ? timeout : 0;
7865
+ waitProperty(target, propertyName) {
7990
7866
  return new Promise((resolve) => {
7991
- const observer = UtilsContext.mutationObserver(parent || UtilsContext.windowApi.document, {
7992
- config: {
7993
- subtree: true,
7994
- childList: true,
7995
- attributes: true,
7996
- },
7997
- immediate: true,
7998
- callback(_, __observer__) {
7999
- const result = checkFn();
8000
- if (result.success) {
8001
- // 取消观察器
8002
- if (typeof __observer__?.disconnect === "function") {
8003
- __observer__.disconnect();
8004
- }
8005
- resolve(result.data);
8006
- }
8007
- },
8008
- });
8009
- if (__timeout__ > 0) {
8010
- UtilsContext.workerSetTimeout(() => {
8011
- // 取消观察器
8012
- if (typeof observer?.disconnect === "function") {
8013
- observer.disconnect();
8014
- }
8015
- resolve(null);
8016
- }, __timeout__);
8017
- }
8018
- });
8019
- }
8020
- waitNode(...args) {
8021
- // 过滤掉undefined
8022
- args = args.filter((arg) => arg !== void 0);
8023
- const UtilsContext = this;
8024
- // 选择器
8025
- const selector = args[0];
8026
- // 父元素(监听的元素)
8027
- let parent = UtilsContext.windowApi.document;
8028
- // 超时时间
8029
- let timeout = 0;
8030
- if (typeof args[0] !== "string" && !Array.isArray(args[0]) && typeof args[0] !== "function") {
8031
- throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]|Function");
8032
- }
8033
- if (args.length === 1) ;
8034
- else if (args.length === 2) {
8035
- const secondParam = args[1];
8036
- if (typeof secondParam === "number") {
8037
- // "div",10000
8038
- timeout = secondParam;
8039
- }
8040
- else if (typeof secondParam === "object" && secondParam instanceof Node) {
8041
- // "div",document
8042
- parent = secondParam;
8043
- }
8044
- else {
8045
- throw new TypeError("Utils.waitNode 第二个参数必须是number|Node");
8046
- }
8047
- }
8048
- else if (args.length === 3) {
8049
- // "div",document,10000
8050
- // 第二个参数,parent
8051
- const secondParam = args[1];
8052
- // 第三个参数,timeout
8053
- const thirdParam = args[2];
8054
- if (typeof secondParam === "object" && secondParam instanceof Node) {
8055
- parent = secondParam;
8056
- if (typeof thirdParam === "number") {
8057
- timeout = thirdParam;
8058
- }
8059
- else {
8060
- throw new TypeError("Utils.waitNode 第三个参数必须是number");
8061
- }
8062
- }
8063
- else {
8064
- throw new TypeError("Utils.waitNode 第二个参数必须是Node");
8065
- }
8066
- }
8067
- else {
8068
- throw new TypeError("Utils.waitNode 参数个数错误");
8069
- }
8070
- function getNode() {
8071
- if (Array.isArray(selector)) {
8072
- const result = [];
8073
- for (let index = 0; index < selector.length; index++) {
8074
- const node = domUtils.selector(selector[index]);
8075
- if (node) {
8076
- result.push(node);
8077
- }
8078
- }
8079
- if (result.length === selector.length) {
8080
- return result;
8081
- }
8082
- }
8083
- else if (typeof selector === "function") {
8084
- return selector();
8085
- }
8086
- else {
8087
- return domUtils.selector(selector, parent);
8088
- }
8089
- }
8090
- return UtilsContext.wait(() => {
8091
- const node = getNode();
8092
- if (node) {
8093
- return {
8094
- success: true,
8095
- data: node,
8096
- };
8097
- }
8098
- else {
8099
- return {
8100
- success: false,
8101
- data: node,
8102
- };
8103
- }
8104
- }, timeout, parent);
8105
- }
8106
- waitAnyNode(...args) {
8107
- // 过滤掉undefined
8108
- args = args.filter((arg) => arg !== void 0);
8109
- const UtilsContext = this;
8110
- // 选择器
8111
- const selectorList = args[0];
8112
- // 父元素(监听的元素)
8113
- let parent = UtilsContext.windowApi.document;
8114
- // 超时时间
8115
- let timeout = 0;
8116
- if (typeof args[0] !== "object" && !Array.isArray(args[0])) {
8117
- throw new TypeError("Utils.waitAnyNode 第一个参数必须是string[]");
8118
- }
8119
- if (args.length === 1) ;
8120
- else if (args.length === 2) {
8121
- const secondParam = args[1];
8122
- if (typeof secondParam === "number") {
8123
- // "div",10000
8124
- timeout = secondParam;
8125
- }
8126
- else if (typeof secondParam === "object" && secondParam instanceof Node) {
8127
- // "div",document
8128
- parent = secondParam;
8129
- }
8130
- else {
8131
- throw new TypeError("Utils.waitAnyNode 第二个参数必须是number|Node");
8132
- }
8133
- }
8134
- else if (args.length === 3) {
8135
- // "div",document,10000
8136
- // 第二个参数,parent
8137
- const secondParam = args[1];
8138
- // 第三个参数,timeout
8139
- const thirdParam = args[2];
8140
- if (typeof secondParam === "object" && secondParam instanceof Node) {
8141
- parent = secondParam;
8142
- if (typeof thirdParam === "number") {
8143
- timeout = thirdParam;
8144
- }
8145
- else {
8146
- throw new TypeError("Utils.waitAnyNode 第三个参数必须是number");
8147
- }
8148
- }
8149
- else {
8150
- throw new TypeError("Utils.waitAnyNode 第二个参数必须是Node");
8151
- }
8152
- }
8153
- else {
8154
- throw new TypeError("Utils.waitAnyNode 参数个数错误");
8155
- }
8156
- const promiseList = selectorList.map((selector) => {
8157
- return UtilsContext.waitNode(selector, parent, timeout);
8158
- });
8159
- return Promise.any(promiseList);
8160
- }
8161
- waitNodeList(...args) {
8162
- // 过滤掉undefined
8163
- args = args.filter((arg) => arg !== void 0);
8164
- const UtilsContext = this;
8165
- // 选择器数组
8166
- const selector = args[0];
8167
- // 父元素(监听的元素)
8168
- let parent = UtilsContext.windowApi.document;
8169
- // 超时时间
8170
- let timeout = 0;
8171
- if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
8172
- throw new TypeError("Utils.waitNodeList 第一个参数必须是string|string[]");
8173
- }
8174
- if (args.length === 1) ;
8175
- else if (args.length === 2) {
8176
- const secondParam = args[1];
8177
- if (typeof secondParam === "number") {
8178
- // "div",10000
8179
- timeout = secondParam;
8180
- }
8181
- else if (typeof secondParam === "object" && secondParam instanceof Node) {
8182
- // "div",document
8183
- parent = secondParam;
7867
+ let obj = target;
7868
+ if (typeof target === "function") {
7869
+ obj = target();
8184
7870
  }
8185
- else {
8186
- throw new TypeError("Utils.waitNodeList 第二个参数必须是number|Node");
8187
- }
8188
- }
8189
- else if (args.length === 3) {
8190
- // "div",document,10000
8191
- // 第二个参数,parent
8192
- const secondParam = args[1];
8193
- // 第三个参数,timeout
8194
- const thirdParam = args[2];
8195
- if (typeof secondParam === "object" && secondParam instanceof Node) {
8196
- parent = secondParam;
8197
- if (typeof thirdParam === "number") {
8198
- timeout = thirdParam;
8199
- }
8200
- else {
8201
- throw new TypeError("Utils.waitNodeList 第三个参数必须是number");
8202
- }
7871
+ if (Reflect.has(obj, propertyName)) {
7872
+ resolve(obj[propertyName]);
8203
7873
  }
8204
7874
  else {
8205
- throw new TypeError("Utils.waitNodeList 第二个参数必须是Node");
8206
- }
8207
- }
8208
- else {
8209
- throw new TypeError("Utils.waitNodeList 参数个数错误");
8210
- }
8211
- function getNodeList() {
8212
- if (Array.isArray(selector)) {
8213
- const result = [];
8214
- for (let index = 0; index < selector.length; index++) {
8215
- const nodeList = domUtils.selectorAll(selector[index], parent);
8216
- if (nodeList.length) {
8217
- result.push(nodeList);
8218
- }
8219
- }
8220
- if (result.length === selector.length) {
8221
- return result;
8222
- }
8223
- }
8224
- else {
8225
- const nodeList = domUtils.selectorAll(selector, parent);
8226
- if (nodeList.length) {
8227
- return nodeList;
8228
- }
8229
- }
8230
- }
8231
- return UtilsContext.wait(() => {
8232
- const node = getNodeList();
8233
- if (node) {
8234
- return {
8235
- success: true,
8236
- data: node,
8237
- };
8238
- }
8239
- else {
8240
- return {
8241
- success: false,
8242
- data: node,
8243
- };
8244
- }
8245
- }, timeout, parent);
8246
- }
8247
- waitAnyNodeList(...args) {
8248
- // 过滤掉undefined
8249
- args = args.filter((arg) => arg !== void 0);
8250
- const UtilsContext = this;
8251
- // 选择器数组
8252
- const selectorList = args[0];
8253
- // 父元素(监听的元素)
8254
- let parent = UtilsContext.windowApi.document;
8255
- // 超时时间
8256
- let timeout = 0;
8257
- if (!Array.isArray(args[0])) {
8258
- throw new TypeError("Utils.waitAnyNodeList 第一个参数必须是string[]");
8259
- }
8260
- if (args.length === 1) ;
8261
- else if (args.length === 2) {
8262
- const secondParam = args[1];
8263
- if (typeof secondParam === "number") {
8264
- // "div",10000
8265
- timeout = secondParam;
8266
- }
8267
- else if (typeof secondParam === "object" && secondParam instanceof Node) {
8268
- // "div",document
8269
- parent = secondParam;
8270
- }
8271
- else {
8272
- throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是number|Node");
8273
- }
8274
- }
8275
- else if (args.length === 3) {
8276
- // "div",document,10000
8277
- // 第二个参数,parent
8278
- const secondParam = args[1];
8279
- // 第三个参数,timeout
8280
- const thirdParam = args[2];
8281
- if (typeof secondParam === "object" && secondParam instanceof Node) {
8282
- parent = secondParam;
8283
- if (typeof thirdParam === "number") {
8284
- timeout = thirdParam;
8285
- }
8286
- else {
8287
- throw new TypeError("Utils.waitAnyNodeList 第三个参数必须是number");
8288
- }
8289
- }
8290
- else {
8291
- throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是Node");
8292
- }
8293
- }
8294
- else {
8295
- throw new TypeError("Utils.waitAnyNodeList 参数个数错误");
8296
- }
8297
- const promiseList = selectorList.map((selector) => {
8298
- return UtilsContext.waitNodeList(selector, parent, timeout);
8299
- });
8300
- return Promise.any(promiseList);
8301
- }
8302
- waitProperty(checkObj, checkPropertyName) {
8303
- return new Promise((resolve) => {
8304
- let obj = checkObj;
8305
- if (typeof checkObj === "function") {
8306
- obj = checkObj();
8307
- }
8308
- if (Reflect.has(obj, checkPropertyName)) {
8309
- resolve(obj[checkPropertyName]);
8310
- }
8311
- else {
8312
- Object.defineProperty(obj, checkPropertyName, {
7875
+ Object.defineProperty(obj, propertyName, {
8313
7876
  set: function (value) {
8314
7877
  try {
8315
7878
  resolve(value);
@@ -8322,49 +7885,48 @@ class Utils {
8322
7885
  }
8323
7886
  });
8324
7887
  }
8325
- waitPropertyByInterval(checkObj, checkPropertyName, intervalTimer = 250, maxTime = -1) {
8326
- const UtilsContext = this;
8327
- if (checkObj == null) {
7888
+ waitPropertyByInterval(checkFn, propertyName, intervalTimer = 250, maxTime = -1) {
7889
+ const that = this;
7890
+ if (checkFn == null) {
8328
7891
  throw new TypeError("checkObj 不能为空对象 ");
8329
7892
  }
8330
7893
  let isResolve = false;
8331
7894
  return new Promise((resolve, reject) => {
8332
- const interval = UtilsContext.workerSetInterval(() => {
8333
- let obj = checkObj;
8334
- if (typeof checkObj === "function") {
8335
- obj = checkObj();
7895
+ const interval = that.workerSetInterval(() => {
7896
+ let inst = checkFn;
7897
+ if (typeof checkFn === "function") {
7898
+ inst = checkFn();
8336
7899
  }
8337
- if (typeof obj !== "object") {
7900
+ if (typeof inst !== "object") {
8338
7901
  return;
8339
7902
  }
8340
- if (obj == null) {
7903
+ if (inst == null) {
8341
7904
  return;
8342
7905
  }
8343
- if ((typeof checkPropertyName === "function" && checkPropertyName(obj)) ||
8344
- Reflect.has(obj, checkPropertyName)) {
7906
+ if ((typeof propertyName === "function" && propertyName(inst)) || Reflect.has(inst, propertyName)) {
8345
7907
  isResolve = true;
8346
- UtilsContext.workerClearInterval(interval);
8347
- resolve(obj[checkPropertyName]);
7908
+ that.workerClearInterval(interval);
7909
+ resolve(inst[propertyName]);
8348
7910
  }
8349
7911
  }, intervalTimer);
8350
7912
  if (maxTime !== -1) {
8351
- UtilsContext.workerSetTimeout(() => {
7913
+ that.workerSetTimeout(() => {
8352
7914
  if (!isResolve) {
8353
- UtilsContext.workerClearInterval(interval);
7915
+ that.workerClearInterval(interval);
8354
7916
  reject();
8355
7917
  }
8356
7918
  }, maxTime);
8357
7919
  }
8358
7920
  });
8359
7921
  }
8360
- async waitVueByInterval(element, propertyName, timer = 250, maxTime = -1, vueName = "__vue__") {
8361
- if (element == null) {
7922
+ async waitVueByInterval($el, propertyName, timer = 250, maxTime = -1, vueName = "__vue__") {
7923
+ if ($el == null) {
8362
7924
  throw new Error("Utils.waitVueByInterval 参数element 不能为空");
8363
7925
  }
8364
7926
  let flag = false;
8365
- const UtilsContext = this;
7927
+ const that = this;
8366
7928
  try {
8367
- await UtilsContext.waitPropertyByInterval(element, function (targetElement) {
7929
+ await that.waitPropertyByInterval($el, function (targetElement) {
8368
7930
  if (targetElement == null) {
8369
7931
  return false;
8370
7932
  }
@@ -8374,25 +7936,24 @@ class Utils {
8374
7936
  if (propertyName == null) {
8375
7937
  return true;
8376
7938
  }
8377
- const vueObject = targetElement[vueName];
7939
+ const $vueEl = targetElement[vueName];
8378
7940
  if (typeof propertyName === "string") {
8379
- if (propertyName in vueObject) {
7941
+ if (propertyName in $vueEl) {
8380
7942
  flag = true;
8381
7943
  return true;
8382
7944
  }
8383
7945
  }
8384
7946
  else {
8385
7947
  /* Function */
8386
- if (propertyName(vueObject)) {
7948
+ if (propertyName($vueEl)) {
8387
7949
  flag = true;
8388
7950
  return true;
8389
7951
  }
8390
7952
  }
8391
7953
  return false;
8392
7954
  }, timer, maxTime);
8393
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8394
7955
  }
8395
- catch (error) {
7956
+ catch {
8396
7957
  return flag;
8397
7958
  }
8398
7959
  return flag;
@@ -8629,73 +8190,6 @@ class Utils {
8629
8190
  this.windowApi.clearInterval(timeId);
8630
8191
  }
8631
8192
  }
8632
- /**
8633
- * 获取剪贴板信息
8634
- */
8635
- async getClipboardInfo() {
8636
- return new Promise((resolve) => {
8637
- /** 读取剪贴板 */
8638
- function readClipboardText() {
8639
- navigator.clipboard
8640
- .readText()
8641
- .then((clipboardText) => {
8642
- resolve({
8643
- error: null,
8644
- content: clipboardText,
8645
- });
8646
- })
8647
- .catch((error) => {
8648
- resolve({
8649
- error: error,
8650
- content: "",
8651
- });
8652
- });
8653
- }
8654
- /** 申请读取剪贴板的权限 */
8655
- function requestPermissionsWithClipboard() {
8656
- navigator.permissions
8657
- .query({
8658
- name: "clipboard-read",
8659
- })
8660
- .then(() => {
8661
- readClipboardText();
8662
- })
8663
- .catch(() => {
8664
- /* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
8665
- readClipboardText();
8666
- });
8667
- }
8668
- /**
8669
- * 检查当前环境是否支持读取剪贴板Api
8670
- */
8671
- function checkClipboardApi() {
8672
- if (typeof navigator?.clipboard?.readText !== "function") {
8673
- return false;
8674
- }
8675
- if (typeof navigator?.permissions?.query !== "function") {
8676
- return false;
8677
- }
8678
- return true;
8679
- }
8680
- if (!checkClipboardApi()) {
8681
- resolve({
8682
- error: new Error("当前环境不支持读取剪贴板Api"),
8683
- content: "",
8684
- });
8685
- return;
8686
- }
8687
- if (document.hasFocus()) {
8688
- requestPermissionsWithClipboard();
8689
- }
8690
- else {
8691
- window.addEventListener("focus", () => {
8692
- requestPermissionsWithClipboard();
8693
- }, {
8694
- once: true,
8695
- });
8696
- }
8697
- });
8698
- }
8699
8193
  }
8700
8194
  const utils = new Utils();
8701
8195