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