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