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