@whitesev/utils 2.5.5 → 2.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -435,6 +435,27 @@ class UtilsGMCookie {
435
435
  }
436
436
  }
437
437
  }
438
+ /**
439
+ * 解析cookie字符串
440
+ * 例如:document.cookie
441
+ * @param cookieStr
442
+ */
443
+ parseCookie(cookieStr) {
444
+ let cookies = cookieStr.split(";");
445
+ let result = [];
446
+ for (const cookieItem of cookies) {
447
+ let item = cookieItem.trim();
448
+ let itemSplit = item.split("=");
449
+ let itemName = itemSplit[0];
450
+ itemSplit.splice(0, 1);
451
+ let itemValue = decodeURIComponent(itemSplit.join(""));
452
+ result.push({
453
+ key: itemName,
454
+ value: itemValue,
455
+ });
456
+ }
457
+ return result;
458
+ }
438
459
  }
439
460
 
440
461
  // @name ajaxHooker
@@ -4118,7 +4139,7 @@ class Utils {
4118
4139
  this.windowApi = new WindowApi(option);
4119
4140
  }
4120
4141
  /** 版本号 */
4121
- version = "2024.12.3";
4142
+ version = "2025.1.1";
4122
4143
  addStyle(cssText) {
4123
4144
  if (typeof cssText !== "string") {
4124
4145
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -4836,6 +4857,8 @@ class Utils {
4836
4857
  getMaxZIndexNodeInfo(deviation = 1, target = this.windowApi.document, ignoreCallBack) {
4837
4858
  deviation = Number.isNaN(deviation) ? 1 : deviation;
4838
4859
  const UtilsContext = this;
4860
+ // 最大值 2147483647
4861
+ // const maxZIndex = Math.pow(2, 31) - 1;
4839
4862
  // 比较值 2000000000
4840
4863
  const maxZIndexCompare = 2 * Math.pow(10, 9);
4841
4864
  // 当前页面最大的z-index
@@ -6730,6 +6753,39 @@ class Utils {
6730
6753
  await UtilsContext.tryCatch(index, item).run(handleFunc);
6731
6754
  }));
6732
6755
  }
6756
+ wait(checkFn, timeout, parent) {
6757
+ const UtilsContext = this;
6758
+ let __timeout__ = typeof timeout === "number" ? timeout : 0;
6759
+ return new Promise((resolve) => {
6760
+ let observer = UtilsContext.mutationObserver(parent || UtilsContext.windowApi.document, {
6761
+ config: {
6762
+ subtree: true,
6763
+ childList: true,
6764
+ attributes: true,
6765
+ },
6766
+ immediate: true,
6767
+ callback(mutations, __observer__) {
6768
+ let result = checkFn();
6769
+ if (result.success) {
6770
+ // 取消观察器
6771
+ if (typeof __observer__?.disconnect === "function") {
6772
+ __observer__.disconnect();
6773
+ }
6774
+ resolve(result.data);
6775
+ }
6776
+ },
6777
+ });
6778
+ if (__timeout__ > 0) {
6779
+ setTimeout(() => {
6780
+ // 取消观察器
6781
+ if (typeof observer?.disconnect === "function") {
6782
+ observer.disconnect();
6783
+ }
6784
+ resolve(null);
6785
+ }, __timeout__);
6786
+ }
6787
+ });
6788
+ }
6733
6789
  waitNode(...args) {
6734
6790
  // 过滤掉undefined
6735
6791
  args = args.filter((arg) => arg !== void 0);
@@ -6740,8 +6796,10 @@ class Utils {
6740
6796
  let parent = UtilsContext.windowApi.document;
6741
6797
  // 超时时间
6742
6798
  let timeout = 0;
6743
- if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
6744
- throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]");
6799
+ if (typeof args[0] !== "string" &&
6800
+ !Array.isArray(args[0]) &&
6801
+ typeof args[0] !== "function") {
6802
+ throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]|Function");
6745
6803
  }
6746
6804
  if (args.length === 1) ;
6747
6805
  else if (args.length === 2) {
@@ -6781,53 +6839,41 @@ class Utils {
6781
6839
  else {
6782
6840
  throw new TypeError("Utils.waitNode 参数个数错误");
6783
6841
  }
6784
- return new Promise((resolve) => {
6785
- function getNode() {
6786
- if (Array.isArray(selector)) {
6787
- let result = [];
6788
- for (let index = 0; index < selector.length; index++) {
6789
- let node = parent.querySelector(selector[index]);
6790
- if (node) {
6791
- result.push(node);
6792
- }
6793
- }
6794
- if (result.length === selector.length) {
6795
- return result;
6842
+ function getNode() {
6843
+ if (Array.isArray(selector)) {
6844
+ let result = [];
6845
+ for (let index = 0; index < selector.length; index++) {
6846
+ let node = parent.querySelector(selector[index]);
6847
+ if (node) {
6848
+ result.push(node);
6796
6849
  }
6797
6850
  }
6798
- else {
6799
- return parent.querySelector(selector);
6851
+ if (result.length === selector.length) {
6852
+ return result;
6800
6853
  }
6801
6854
  }
6802
- var observer = UtilsContext.mutationObserver(parent, {
6803
- config: {
6804
- subtree: true,
6805
- childList: true,
6806
- attributes: true,
6807
- },
6808
- callback() {
6809
- let node = getNode();
6810
- if (node) {
6811
- // 取消观察器
6812
- if (typeof observer?.disconnect === "function") {
6813
- observer.disconnect();
6814
- }
6815
- resolve(node);
6816
- return;
6817
- }
6818
- },
6819
- immediate: true,
6820
- });
6821
- if (timeout > 0) {
6822
- setTimeout(() => {
6823
- // 取消观察器
6824
- if (typeof observer?.disconnect === "function") {
6825
- observer.disconnect();
6826
- }
6827
- resolve(null);
6828
- }, timeout);
6855
+ else if (typeof selector === "function") {
6856
+ return selector();
6829
6857
  }
6830
- });
6858
+ else {
6859
+ return parent.querySelector(selector);
6860
+ }
6861
+ }
6862
+ return UtilsContext.wait(() => {
6863
+ let node = getNode();
6864
+ if (node) {
6865
+ return {
6866
+ success: true,
6867
+ data: node,
6868
+ };
6869
+ }
6870
+ else {
6871
+ return {
6872
+ success: false,
6873
+ data: node,
6874
+ };
6875
+ }
6876
+ }, timeout, parent);
6831
6877
  }
6832
6878
  waitAnyNode(...args) {
6833
6879
  // 过滤掉undefined
@@ -6936,57 +6982,41 @@ class Utils {
6936
6982
  else {
6937
6983
  throw new TypeError("Utils.waitNodeList 参数个数错误");
6938
6984
  }
6939
- return new Promise((resolve) => {
6940
- function getNodeList() {
6941
- if (Array.isArray(selector)) {
6942
- let result = [];
6943
- for (let index = 0; index < selector.length; index++) {
6944
- let nodeList = parent.querySelectorAll(selector[index]);
6945
- if (nodeList.length) {
6946
- result.push(nodeList);
6947
- }
6948
- }
6949
- if (result.length === selector.length) {
6950
- return result;
6951
- }
6952
- }
6953
- else {
6954
- let nodeList = parent.querySelectorAll(selector);
6985
+ function getNodeList() {
6986
+ if (Array.isArray(selector)) {
6987
+ let result = [];
6988
+ for (let index = 0; index < selector.length; index++) {
6989
+ let nodeList = parent.querySelectorAll(selector[index]);
6955
6990
  if (nodeList.length) {
6956
- return nodeList;
6991
+ result.push(nodeList);
6957
6992
  }
6958
6993
  }
6994
+ if (result.length === selector.length) {
6995
+ return result;
6996
+ }
6959
6997
  }
6960
- var observer = UtilsContext.mutationObserver(parent, {
6961
- config: {
6962
- subtree: true,
6963
- childList: true,
6964
- attributes: true,
6965
- },
6966
- callback() {
6967
- let node = getNodeList();
6968
- if (node) {
6969
- // 取消观察器
6970
- try {
6971
- observer.disconnect();
6972
- }
6973
- catch (error) { }
6974
- resolve(node);
6975
- return;
6976
- }
6977
- },
6978
- immediate: true,
6979
- });
6980
- if (timeout > 0) {
6981
- setTimeout(() => {
6982
- // 取消观察器
6983
- if (typeof observer?.disconnect === "function") {
6984
- observer.disconnect();
6985
- }
6986
- resolve(null);
6987
- }, timeout);
6998
+ else {
6999
+ let nodeList = parent.querySelectorAll(selector);
7000
+ if (nodeList.length) {
7001
+ return nodeList;
7002
+ }
6988
7003
  }
6989
- });
7004
+ }
7005
+ return UtilsContext.wait(() => {
7006
+ let node = getNodeList();
7007
+ if (node) {
7008
+ return {
7009
+ success: true,
7010
+ data: node,
7011
+ };
7012
+ }
7013
+ else {
7014
+ return {
7015
+ success: false,
7016
+ data: node,
7017
+ };
7018
+ }
7019
+ }, timeout, parent);
6990
7020
  }
6991
7021
  waitAnyNodeList(...args) {
6992
7022
  // 过滤掉undefined