@whitesev/utils 2.5.6 → 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.amd.js +98 -91
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +98 -91
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +98 -91
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +98 -91
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +98 -91
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +98 -91
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Utils.d.ts +65 -18
- package/dist/types/src/types/global.d.ts +1 -0
- package/package.json +1 -1
- package/src/Utils.ts +203 -113
- package/src/types/global.d.ts +1 -0
package/dist/index.esm.js
CHANGED
|
@@ -4139,7 +4139,7 @@ class Utils {
|
|
|
4139
4139
|
this.windowApi = new WindowApi(option);
|
|
4140
4140
|
}
|
|
4141
4141
|
/** 版本号 */
|
|
4142
|
-
version = "
|
|
4142
|
+
version = "2025.1.1";
|
|
4143
4143
|
addStyle(cssText) {
|
|
4144
4144
|
if (typeof cssText !== "string") {
|
|
4145
4145
|
throw new Error("Utils.addStyle 参数cssText 必须为String类型");
|
|
@@ -6753,6 +6753,39 @@ class Utils {
|
|
|
6753
6753
|
await UtilsContext.tryCatch(index, item).run(handleFunc);
|
|
6754
6754
|
}));
|
|
6755
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
|
+
}
|
|
6756
6789
|
waitNode(...args) {
|
|
6757
6790
|
// 过滤掉undefined
|
|
6758
6791
|
args = args.filter((arg) => arg !== void 0);
|
|
@@ -6763,8 +6796,10 @@ class Utils {
|
|
|
6763
6796
|
let parent = UtilsContext.windowApi.document;
|
|
6764
6797
|
// 超时时间
|
|
6765
6798
|
let timeout = 0;
|
|
6766
|
-
if (typeof args[0] !== "string" &&
|
|
6767
|
-
|
|
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");
|
|
6768
6803
|
}
|
|
6769
6804
|
if (args.length === 1) ;
|
|
6770
6805
|
else if (args.length === 2) {
|
|
@@ -6804,53 +6839,41 @@ class Utils {
|
|
|
6804
6839
|
else {
|
|
6805
6840
|
throw new TypeError("Utils.waitNode 参数个数错误");
|
|
6806
6841
|
}
|
|
6807
|
-
|
|
6808
|
-
|
|
6809
|
-
|
|
6810
|
-
|
|
6811
|
-
|
|
6812
|
-
|
|
6813
|
-
|
|
6814
|
-
result.push(node);
|
|
6815
|
-
}
|
|
6816
|
-
}
|
|
6817
|
-
if (result.length === selector.length) {
|
|
6818
|
-
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);
|
|
6819
6849
|
}
|
|
6820
6850
|
}
|
|
6821
|
-
|
|
6822
|
-
return
|
|
6851
|
+
if (result.length === selector.length) {
|
|
6852
|
+
return result;
|
|
6823
6853
|
}
|
|
6824
6854
|
}
|
|
6825
|
-
|
|
6826
|
-
|
|
6827
|
-
subtree: true,
|
|
6828
|
-
childList: true,
|
|
6829
|
-
attributes: true,
|
|
6830
|
-
},
|
|
6831
|
-
callback() {
|
|
6832
|
-
let node = getNode();
|
|
6833
|
-
if (node) {
|
|
6834
|
-
// 取消观察器
|
|
6835
|
-
if (typeof observer?.disconnect === "function") {
|
|
6836
|
-
observer.disconnect();
|
|
6837
|
-
}
|
|
6838
|
-
resolve(node);
|
|
6839
|
-
return;
|
|
6840
|
-
}
|
|
6841
|
-
},
|
|
6842
|
-
immediate: true,
|
|
6843
|
-
});
|
|
6844
|
-
if (timeout > 0) {
|
|
6845
|
-
setTimeout(() => {
|
|
6846
|
-
// 取消观察器
|
|
6847
|
-
if (typeof observer?.disconnect === "function") {
|
|
6848
|
-
observer.disconnect();
|
|
6849
|
-
}
|
|
6850
|
-
resolve(null);
|
|
6851
|
-
}, timeout);
|
|
6855
|
+
else if (typeof selector === "function") {
|
|
6856
|
+
return selector();
|
|
6852
6857
|
}
|
|
6853
|
-
|
|
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);
|
|
6854
6877
|
}
|
|
6855
6878
|
waitAnyNode(...args) {
|
|
6856
6879
|
// 过滤掉undefined
|
|
@@ -6959,57 +6982,41 @@ class Utils {
|
|
|
6959
6982
|
else {
|
|
6960
6983
|
throw new TypeError("Utils.waitNodeList 参数个数错误");
|
|
6961
6984
|
}
|
|
6962
|
-
|
|
6963
|
-
|
|
6964
|
-
|
|
6965
|
-
|
|
6966
|
-
|
|
6967
|
-
let nodeList = parent.querySelectorAll(selector[index]);
|
|
6968
|
-
if (nodeList.length) {
|
|
6969
|
-
result.push(nodeList);
|
|
6970
|
-
}
|
|
6971
|
-
}
|
|
6972
|
-
if (result.length === selector.length) {
|
|
6973
|
-
return result;
|
|
6974
|
-
}
|
|
6975
|
-
}
|
|
6976
|
-
else {
|
|
6977
|
-
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]);
|
|
6978
6990
|
if (nodeList.length) {
|
|
6979
|
-
|
|
6991
|
+
result.push(nodeList);
|
|
6980
6992
|
}
|
|
6981
6993
|
}
|
|
6994
|
+
if (result.length === selector.length) {
|
|
6995
|
+
return result;
|
|
6996
|
+
}
|
|
6982
6997
|
}
|
|
6983
|
-
|
|
6984
|
-
|
|
6985
|
-
|
|
6986
|
-
|
|
6987
|
-
|
|
6988
|
-
},
|
|
6989
|
-
callback() {
|
|
6990
|
-
let node = getNodeList();
|
|
6991
|
-
if (node) {
|
|
6992
|
-
// 取消观察器
|
|
6993
|
-
try {
|
|
6994
|
-
observer.disconnect();
|
|
6995
|
-
}
|
|
6996
|
-
catch (error) { }
|
|
6997
|
-
resolve(node);
|
|
6998
|
-
return;
|
|
6999
|
-
}
|
|
7000
|
-
},
|
|
7001
|
-
immediate: true,
|
|
7002
|
-
});
|
|
7003
|
-
if (timeout > 0) {
|
|
7004
|
-
setTimeout(() => {
|
|
7005
|
-
// 取消观察器
|
|
7006
|
-
if (typeof observer?.disconnect === "function") {
|
|
7007
|
-
observer.disconnect();
|
|
7008
|
-
}
|
|
7009
|
-
resolve(null);
|
|
7010
|
-
}, timeout);
|
|
6998
|
+
else {
|
|
6999
|
+
let nodeList = parent.querySelectorAll(selector);
|
|
7000
|
+
if (nodeList.length) {
|
|
7001
|
+
return nodeList;
|
|
7002
|
+
}
|
|
7011
7003
|
}
|
|
7012
|
-
}
|
|
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);
|
|
7013
7020
|
}
|
|
7014
7021
|
waitAnyNodeList(...args) {
|
|
7015
7022
|
// 过滤掉undefined
|