@whitesev/utils 2.11.0 → 2.11.2

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.2";
241
241
 
242
242
  /* eslint-disable */
243
243
  // ==UserScript==
@@ -7989,9 +7989,22 @@ class Utils {
7989
7989
  return result;
7990
7990
  }
7991
7991
  else {
7992
- return Array.from(uniqueArrayData).filter((item) => !Array.from(compareArrayData).some(function (item2) {
7993
- return compareFun(item, item2);
7994
- }));
7992
+ const compareSet = new Set(compareArrayData);
7993
+ const result = [];
7994
+ for (let i = 0; i < uniqueArrayData.length; i++) {
7995
+ const item = uniqueArrayData[i];
7996
+ let flag = false;
7997
+ for (const compareItem of compareSet) {
7998
+ if (compareFun(item, compareItem)) {
7999
+ flag = true;
8000
+ break;
8001
+ }
8002
+ }
8003
+ if (!flag) {
8004
+ result.push(item);
8005
+ }
8006
+ }
8007
+ return result;
7995
8008
  }
7996
8009
  }
7997
8010
  /**
@@ -8371,10 +8384,13 @@ class Utils {
8371
8384
  }
8372
8385
  /**
8373
8386
  * 判断页面中是否存在`worker-src`的CSP规则
8387
+ * @param timeout 超时时间,默认为`1500ms`
8374
8388
  */
8375
- hasWorkerCSP() {
8389
+ hasWorkerCSP(timeout = 1500) {
8376
8390
  return new Promise((resolve) => {
8377
8391
  let flag = true;
8392
+ let timeId = void 0;
8393
+ let worker = void 0;
8378
8394
  let workerBlobUrl = void 0;
8379
8395
  const workerJs = /*js*/ `
8380
8396
  (() => {
@@ -8390,11 +8406,26 @@ class Utils {
8390
8406
  }
8391
8407
  );
8392
8408
  })();`;
8409
+ /**
8410
+ * 返回结果
8411
+ */
8412
+ const finishCallBack = () => {
8413
+ clearTimeout(timeId);
8414
+ if (worker != null) {
8415
+ worker.terminate();
8416
+ }
8417
+ // 释放
8418
+ if (typeof workerBlobUrl === "string") {
8419
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
8420
+ workerBlobUrl = void 0;
8421
+ }
8422
+ resolve(flag);
8423
+ };
8393
8424
  try {
8394
8425
  const workerScript = new Blob([workerJs], {
8395
8426
  type: "application/javascript",
8396
8427
  });
8397
- workerBlobUrl = window.URL.createObjectURL(workerScript);
8428
+ workerBlobUrl = globalThis.URL.createObjectURL(workerScript);
8398
8429
  // @ts-expect-error
8399
8430
  if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
8400
8431
  // 使用这个后虽然不报错,但是仍会有blob错误
@@ -8406,25 +8437,27 @@ class Utils {
8406
8437
  });
8407
8438
  workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
8408
8439
  }
8409
- const worker = new Worker(workerBlobUrl);
8440
+ worker = new Worker(workerBlobUrl);
8410
8441
  worker.onmessage = (data) => {
8411
8442
  if (data.data.success) {
8412
8443
  flag = false;
8444
+ finishCallBack();
8413
8445
  }
8414
8446
  };
8415
- setTimeout(() => {
8416
- worker.terminate();
8417
- resolve(flag);
8418
- }, 500);
8447
+ timeId = setTimeout(() => {
8448
+ finishCallBack();
8449
+ }, timeout);
8419
8450
  worker.postMessage("test");
8420
8451
  }
8421
8452
  catch {
8422
8453
  flag = true;
8454
+ finishCallBack();
8423
8455
  }
8424
8456
  finally {
8425
8457
  // 释放
8426
8458
  if (typeof workerBlobUrl === "string") {
8427
8459
  globalThis.URL.revokeObjectURL(workerBlobUrl);
8460
+ workerBlobUrl = void 0;
8428
8461
  }
8429
8462
  }
8430
8463
  });