@whitesev/utils 2.9.13 → 2.10.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.
- package/dist/index.amd.js +79 -33
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +79 -33
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +79 -33
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +79 -33
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +79 -33
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +79 -33
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/Utils.d.ts +4 -0
- package/package.json +1 -1
- package/src/Utils.ts +73 -26
|
@@ -1590,6 +1590,10 @@ declare class Utils {
|
|
|
1590
1590
|
}) => Promise<any> : (...args: {
|
|
1591
1591
|
[K in keyof P]: any;
|
|
1592
1592
|
}) => any;
|
|
1593
|
+
/**
|
|
1594
|
+
* 判断页面中是否存在`worker-src`的CSP规则
|
|
1595
|
+
*/
|
|
1596
|
+
hasWorkerCSP(): Promise<boolean>;
|
|
1593
1597
|
}
|
|
1594
1598
|
declare const utils: Utils;
|
|
1595
1599
|
export { utils as Utils };
|
package/package.json
CHANGED
package/src/Utils.ts
CHANGED
|
@@ -234,8 +234,8 @@ class Utils {
|
|
|
234
234
|
let timer: any = null as any;
|
|
235
235
|
const that = this;
|
|
236
236
|
return function (...args: A) {
|
|
237
|
-
|
|
238
|
-
timer =
|
|
237
|
+
clearTimeout(timer);
|
|
238
|
+
timer = setTimeout(function () {
|
|
239
239
|
fn.apply(that, args);
|
|
240
240
|
}, delay);
|
|
241
241
|
};
|
|
@@ -263,7 +263,6 @@ class Utils {
|
|
|
263
263
|
**/
|
|
264
264
|
downloadBase64(base64Data: string, fileName: string, isIFrame?: boolean): void;
|
|
265
265
|
downloadBase64(base64Data: string, fileName: string, isIFrame = false) {
|
|
266
|
-
const that = this;
|
|
267
266
|
if (typeof base64Data !== "string") {
|
|
268
267
|
throw new Error("Utils.downloadBase64 参数 base64Data 必须为 string 类型");
|
|
269
268
|
}
|
|
@@ -276,7 +275,7 @@ class Utils {
|
|
|
276
275
|
$iframe.style.display = "none";
|
|
277
276
|
$iframe.src = base64Data;
|
|
278
277
|
(this.windowApi.document.body || this.windowApi.document.documentElement).appendChild($iframe);
|
|
279
|
-
|
|
278
|
+
setTimeout(() => {
|
|
280
279
|
$iframe!.contentWindow!.document.execCommand("SaveAs", true, fileName);
|
|
281
280
|
(this.windowApi.document.body || this.windowApi.document.documentElement).removeChild($iframe);
|
|
282
281
|
}, 100);
|
|
@@ -2841,7 +2840,6 @@ class Utils {
|
|
|
2841
2840
|
**/
|
|
2842
2841
|
setTimeout(callback: (() => void) | string, delayTime?: number): Promise<any>;
|
|
2843
2842
|
setTimeout(callback: (() => void) | string, delayTime: number = 0): Promise<any> {
|
|
2844
|
-
const that = this;
|
|
2845
2843
|
if (typeof callback !== "function" && typeof callback !== "string") {
|
|
2846
2844
|
throw new TypeError("Utils.setTimeout 参数 callback 必须为 function|string 类型");
|
|
2847
2845
|
}
|
|
@@ -2849,8 +2847,8 @@ class Utils {
|
|
|
2849
2847
|
throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
|
|
2850
2848
|
}
|
|
2851
2849
|
return new Promise((resolve) => {
|
|
2852
|
-
|
|
2853
|
-
resolve(
|
|
2850
|
+
setTimeout(() => {
|
|
2851
|
+
resolve(this.tryCatch().run(callback));
|
|
2854
2852
|
}, delayTime);
|
|
2855
2853
|
});
|
|
2856
2854
|
}
|
|
@@ -2862,12 +2860,11 @@ class Utils {
|
|
|
2862
2860
|
**/
|
|
2863
2861
|
sleep(delayTime?: number): Promise<void>;
|
|
2864
2862
|
sleep(delayTime: number = 0): Promise<void> {
|
|
2865
|
-
const that = this;
|
|
2866
2863
|
if (typeof delayTime !== "number") {
|
|
2867
2864
|
throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
|
|
2868
2865
|
}
|
|
2869
2866
|
return new Promise((resolve) => {
|
|
2870
|
-
|
|
2867
|
+
setTimeout(() => {
|
|
2871
2868
|
resolve(void 0);
|
|
2872
2869
|
}, delayTime);
|
|
2873
2870
|
});
|
|
@@ -3397,13 +3394,12 @@ class Utils {
|
|
|
3397
3394
|
intervalTimer: number = 250,
|
|
3398
3395
|
maxTime: number = -1
|
|
3399
3396
|
): Promise<T> {
|
|
3400
|
-
const that = this;
|
|
3401
3397
|
if (checkFn == null) {
|
|
3402
3398
|
throw new TypeError("checkObj 不能为空对象 ");
|
|
3403
3399
|
}
|
|
3404
3400
|
let isResolve = false;
|
|
3405
3401
|
return new Promise((resolve, reject) => {
|
|
3406
|
-
const interval =
|
|
3402
|
+
const interval = setInterval(() => {
|
|
3407
3403
|
let inst = checkFn;
|
|
3408
3404
|
if (typeof checkFn === "function") {
|
|
3409
3405
|
inst = checkFn();
|
|
@@ -3416,14 +3412,14 @@ class Utils {
|
|
|
3416
3412
|
}
|
|
3417
3413
|
if ((typeof propertyName === "function" && propertyName(inst)) || Reflect.has(inst, propertyName as string)) {
|
|
3418
3414
|
isResolve = true;
|
|
3419
|
-
|
|
3415
|
+
clearInterval(interval);
|
|
3420
3416
|
resolve((inst as any)[propertyName as string]);
|
|
3421
3417
|
}
|
|
3422
3418
|
}, intervalTimer);
|
|
3423
3419
|
if (maxTime !== -1) {
|
|
3424
|
-
|
|
3420
|
+
setTimeout(() => {
|
|
3425
3421
|
if (!isResolve) {
|
|
3426
|
-
|
|
3422
|
+
clearInterval(interval);
|
|
3427
3423
|
reject();
|
|
3428
3424
|
}
|
|
3429
3425
|
}, maxTime);
|
|
@@ -3752,8 +3748,7 @@ class Utils {
|
|
|
3752
3748
|
workerSetTimeout(callback: (...args: any[]) => any, timeout: number = 0) {
|
|
3753
3749
|
try {
|
|
3754
3750
|
return WorkerSetTimeout(callback, timeout);
|
|
3755
|
-
|
|
3756
|
-
} catch (error) {
|
|
3751
|
+
} catch {
|
|
3757
3752
|
return this.windowApi.setTimeout(callback, timeout);
|
|
3758
3753
|
}
|
|
3759
3754
|
}
|
|
@@ -3766,10 +3761,7 @@ class Utils {
|
|
|
3766
3761
|
if (timeId != null) {
|
|
3767
3762
|
WorkerClearTimeout(timeId);
|
|
3768
3763
|
}
|
|
3769
|
-
|
|
3770
|
-
} catch (error) {
|
|
3771
|
-
// console.log(error);
|
|
3772
|
-
} finally {
|
|
3764
|
+
} catch {
|
|
3773
3765
|
this.windowApi.clearTimeout(timeId);
|
|
3774
3766
|
}
|
|
3775
3767
|
}
|
|
@@ -3781,8 +3773,7 @@ class Utils {
|
|
|
3781
3773
|
workerSetInterval(callback: (...args: any[]) => any, timeout: number = 0) {
|
|
3782
3774
|
try {
|
|
3783
3775
|
return WorkerSetInterval(callback, timeout);
|
|
3784
|
-
|
|
3785
|
-
} catch (error) {
|
|
3776
|
+
} catch {
|
|
3786
3777
|
return this.windowApi.setInterval(callback, timeout);
|
|
3787
3778
|
}
|
|
3788
3779
|
}
|
|
@@ -3795,10 +3786,7 @@ class Utils {
|
|
|
3795
3786
|
if (timeId != null) {
|
|
3796
3787
|
WorkerClearInterval(timeId);
|
|
3797
3788
|
}
|
|
3798
|
-
|
|
3799
|
-
} catch (error) {
|
|
3800
|
-
// console.log(error);
|
|
3801
|
-
} finally {
|
|
3789
|
+
} catch {
|
|
3802
3790
|
this.windowApi.clearInterval(timeId);
|
|
3803
3791
|
}
|
|
3804
3792
|
}
|
|
@@ -3853,6 +3841,65 @@ class Utils {
|
|
|
3853
3841
|
return new FunctionConstructor(...args);
|
|
3854
3842
|
}
|
|
3855
3843
|
}
|
|
3844
|
+
/**
|
|
3845
|
+
* 判断页面中是否存在`worker-src`的CSP规则
|
|
3846
|
+
*/
|
|
3847
|
+
hasWorkerCSP() {
|
|
3848
|
+
return new Promise<boolean>((resolve) => {
|
|
3849
|
+
let flag = true;
|
|
3850
|
+
let workerBlobUrl: string | undefined = void 0;
|
|
3851
|
+
|
|
3852
|
+
const workerJs = /*js*/ `
|
|
3853
|
+
(() => {
|
|
3854
|
+
this.addEventListener(
|
|
3855
|
+
"message",
|
|
3856
|
+
function () {
|
|
3857
|
+
this.postMessage({
|
|
3858
|
+
success: true,
|
|
3859
|
+
});
|
|
3860
|
+
},
|
|
3861
|
+
{
|
|
3862
|
+
capture: true,
|
|
3863
|
+
}
|
|
3864
|
+
);
|
|
3865
|
+
})();`;
|
|
3866
|
+
try {
|
|
3867
|
+
const workerScript = new Blob([workerJs], {
|
|
3868
|
+
type: "application/javascript",
|
|
3869
|
+
});
|
|
3870
|
+
workerBlobUrl = window.URL.createObjectURL(workerScript);
|
|
3871
|
+
// @ts-expect-error
|
|
3872
|
+
if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
|
|
3873
|
+
// 使用这个后虽然不报错,但是仍会有blob错误
|
|
3874
|
+
// violates the following Content Security Policy directive: "worker-src 'self'". The action has been blocked.
|
|
3875
|
+
// 且这个错误无法使用try/catch捕捉,导致本该提醒使用手动匹配的结果并无提醒弹窗
|
|
3876
|
+
// @ts-expect-error
|
|
3877
|
+
const workerPolicy = globalThis.trustedTypes.createPolicy("workerPolicy", {
|
|
3878
|
+
createScriptURL: (url: string) => url,
|
|
3879
|
+
});
|
|
3880
|
+
workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
|
|
3881
|
+
}
|
|
3882
|
+
const worker = new Worker(workerBlobUrl!);
|
|
3883
|
+
worker.onmessage = (data) => {
|
|
3884
|
+
if (data.data.success) {
|
|
3885
|
+
flag = false;
|
|
3886
|
+
}
|
|
3887
|
+
};
|
|
3888
|
+
setTimeout(() => {
|
|
3889
|
+
worker.terminate();
|
|
3890
|
+
resolve(flag);
|
|
3891
|
+
}, 500);
|
|
3892
|
+
worker.postMessage("test");
|
|
3893
|
+
} catch {
|
|
3894
|
+
flag = true;
|
|
3895
|
+
} finally {
|
|
3896
|
+
// 释放
|
|
3897
|
+
if (typeof workerBlobUrl === "string") {
|
|
3898
|
+
globalThis.URL.revokeObjectURL(workerBlobUrl);
|
|
3899
|
+
}
|
|
3900
|
+
}
|
|
3901
|
+
});
|
|
3902
|
+
}
|
|
3856
3903
|
}
|
|
3857
3904
|
|
|
3858
3905
|
const utils = new Utils();
|