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