@whitesev/utils 2.11.0 → 2.11.1

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
@@ -237,7 +237,7 @@ const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
237
237
  const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
238
238
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
239
239
 
240
- const version = "2.11.0";
240
+ const version = "2.11.1";
241
241
 
242
242
  /* eslint-disable */
243
243
  // ==UserScript==
@@ -8371,10 +8371,13 @@ class Utils {
8371
8371
  }
8372
8372
  /**
8373
8373
  * 判断页面中是否存在`worker-src`的CSP规则
8374
+ * @param timeout 超时时间,默认为`1500ms`
8374
8375
  */
8375
- hasWorkerCSP() {
8376
+ hasWorkerCSP(timeout = 1500) {
8376
8377
  return new Promise((resolve) => {
8377
8378
  let flag = true;
8379
+ let timeId = void 0;
8380
+ let worker = void 0;
8378
8381
  let workerBlobUrl = void 0;
8379
8382
  const workerJs = /*js*/ `
8380
8383
  (() => {
@@ -8390,11 +8393,26 @@ class Utils {
8390
8393
  }
8391
8394
  );
8392
8395
  })();`;
8396
+ /**
8397
+ * 返回结果
8398
+ */
8399
+ const finishCallBack = () => {
8400
+ clearTimeout(timeId);
8401
+ if (worker != null) {
8402
+ worker.terminate();
8403
+ }
8404
+ // 释放
8405
+ if (typeof workerBlobUrl === "string") {
8406
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
8407
+ workerBlobUrl = void 0;
8408
+ }
8409
+ resolve(flag);
8410
+ };
8393
8411
  try {
8394
8412
  const workerScript = new Blob([workerJs], {
8395
8413
  type: "application/javascript",
8396
8414
  });
8397
- workerBlobUrl = window.URL.createObjectURL(workerScript);
8415
+ workerBlobUrl = globalThis.URL.createObjectURL(workerScript);
8398
8416
  // @ts-expect-error
8399
8417
  if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
8400
8418
  // 使用这个后虽然不报错,但是仍会有blob错误
@@ -8406,25 +8424,27 @@ class Utils {
8406
8424
  });
8407
8425
  workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
8408
8426
  }
8409
- const worker = new Worker(workerBlobUrl);
8427
+ worker = new Worker(workerBlobUrl);
8410
8428
  worker.onmessage = (data) => {
8411
8429
  if (data.data.success) {
8412
8430
  flag = false;
8431
+ finishCallBack();
8413
8432
  }
8414
8433
  };
8415
- setTimeout(() => {
8416
- worker.terminate();
8417
- resolve(flag);
8418
- }, 500);
8434
+ timeId = setTimeout(() => {
8435
+ finishCallBack();
8436
+ }, timeout);
8419
8437
  worker.postMessage("test");
8420
8438
  }
8421
8439
  catch {
8422
8440
  flag = true;
8441
+ finishCallBack();
8423
8442
  }
8424
8443
  finally {
8425
8444
  // 释放
8426
8445
  if (typeof workerBlobUrl === "string") {
8427
8446
  globalThis.URL.revokeObjectURL(workerBlobUrl);
8447
+ workerBlobUrl = void 0;
8428
8448
  }
8429
8449
  }
8430
8450
  });