@whitesev/utils 2.9.13 → 2.10.0

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
@@ -232,12 +232,12 @@ const createLoadOrReturnBroker = (loadBroker, worker) => {
232
232
  const worker = `(()=>{var e={455(e,t){!function(e){"use strict";var t=function(e){return function(t){var r=e(t);return t.add(r),r}},r=function(e){return function(t,r){return e.set(t,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,s=2*o,a=function(e,t){return function(r){var a=t.get(r),i=void 0===a?r.size:a<s?a+1:0;if(!r.has(i))return e(r,i);if(r.size<o){for(;r.has(i);)i=Math.floor(Math.random()*s);return e(r,i)}if(r.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;r.has(i);)i=Math.floor(Math.random()*n);return e(r,i)}},i=new WeakMap,u=r(i),c=a(u,i),l=t(c);e.addUniqueNumber=l,e.generateUniqueNumber=c}(t)}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n].call(s.exports,s,s.exports,r),s.exports}(()=>{"use strict";const e=-32603,t=-32602,n=-32601,o=(e,t)=>Object.assign(new Error(e),{status:t}),s=t=>o('The handler of the method called "'.concat(t,'" returned an unexpected result.'),e),a=(t,r)=>async({data:{id:a,method:i,params:u}})=>{const c=r[i];try{if(void 0===c)throw(e=>o('The requested method called "'.concat(e,'" is not supported.'),n))(i);const r=void 0===u?c():c(u);if(void 0===r)throw(t=>o('The handler of the method called "'.concat(t,'" returned no required result.'),e))(i);const l=r instanceof Promise?await r:r;if(null===a){if(void 0!==l.result)throw s(i)}else{if(void 0===l.result)throw s(i);const{result:e,transferables:r=[]}=l;t.postMessage({id:a,result:e},r)}}catch(e){const{message:r,status:n=-32603}=e;t.postMessage({error:{code:n,message:r},id:a})}};var i=r(455);const u=new Map,c=(e,r,n)=>({...r,connect:({port:t})=>{t.start();const n=e(t,r),o=(0,i.generateUniqueNumber)(u);return u.set(o,()=>{n(),t.close(),u.delete(o)}),{result:o}},disconnect:({portId:e})=>{const r=u.get(e);if(void 0===r)throw(e=>o('The specified parameter called "portId" with the given value "'.concat(e,'" does not identify a port connected to this worker.'),t))(e);return r(),{result:null}},isSupported:async()=>{if(await new Promise(e=>{const t=new ArrayBuffer(0),{port1:r,port2:n}=new MessageChannel;r.onmessage=({data:t})=>e(null!==t),n.postMessage(t,[t])})){const e=n();return{result:e instanceof Promise?await e:e}}return{result:!1}}}),l=(e,t,r=()=>!0)=>{const n=c(l,t,r),o=a(e,n);return e.addEventListener("message",o),()=>e.removeEventListener("message",o)},d=(e,t)=>r=>{const n=t.get(r);if(void 0===n)return Promise.resolve(!1);const[o,s]=n;return e(o),t.delete(r),s(!1),Promise.resolve(!0)},m=(e,t,r,n)=>(o,s,a)=>{const i=o+s-t.timeOrigin,u=i-t.now();return new Promise(t=>{e.set(a,[r(n,u,i,e,t,a),t])})},f=new Map,h=d(globalThis.clearTimeout,f),p=new Map,v=d(globalThis.clearTimeout,p),w=((e,t)=>{const r=(n,o,s,a)=>{const i=n-e.now();i>0?o.set(a,[t(r,i,n,o,s,a),s]):(o.delete(a),s(!0))};return r})(performance,globalThis.setTimeout),g=m(f,performance,globalThis.setTimeout,w),T=m(p,performance,globalThis.setTimeout,w);l(self,{clear:async({timerId:e,timerType:t})=>({result:await("interval"===t?h(e):v(e))}),set:async({delay:e,now:t,timerId:r,timerType:n})=>({result:await("interval"===n?g:T)(e,t,r)})})})()})();`; // tslint:disable-line:max-line-length
233
233
 
234
234
  const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
235
- const clearInterval = (timerId) => loadOrReturnBroker().clearInterval(timerId);
235
+ const clearInterval$1 = (timerId) => loadOrReturnBroker().clearInterval(timerId);
236
236
  const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
237
- const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
237
+ const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
238
238
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
239
239
 
240
- const version = "2.9.13";
240
+ const version = "2.10.0";
241
241
 
242
242
  /* eslint-disable */
243
243
  // ==UserScript==
@@ -5718,8 +5718,8 @@ class Utils {
5718
5718
  let timer = null;
5719
5719
  const that = this;
5720
5720
  return function (...args) {
5721
- that.workerClearTimeout(timer);
5722
- timer = that.workerSetTimeout(function () {
5721
+ clearTimeout(timer);
5722
+ timer = setTimeout(function () {
5723
5723
  fn.apply(that, args);
5724
5724
  }, delay);
5725
5725
  };
@@ -5738,7 +5738,6 @@ class Utils {
5738
5738
  **/
5739
5739
  Dictionary = UtilsDictionary;
5740
5740
  downloadBase64(base64Data, fileName, isIFrame = false) {
5741
- const that = this;
5742
5741
  if (typeof base64Data !== "string") {
5743
5742
  throw new Error("Utils.downloadBase64 参数 base64Data 必须为 string 类型");
5744
5743
  }
@@ -5751,7 +5750,7 @@ class Utils {
5751
5750
  $iframe.style.display = "none";
5752
5751
  $iframe.src = base64Data;
5753
5752
  (this.windowApi.document.body || this.windowApi.document.documentElement).appendChild($iframe);
5754
- that.workerSetTimeout(() => {
5753
+ setTimeout(() => {
5755
5754
  $iframe.contentWindow.document.execCommand("SaveAs", true, fileName);
5756
5755
  (this.windowApi.document.body || this.windowApi.document.documentElement).removeChild($iframe);
5757
5756
  }, 100);
@@ -7650,7 +7649,6 @@ class Utils {
7650
7649
  });
7651
7650
  }
7652
7651
  setTimeout(callback, delayTime = 0) {
7653
- const that = this;
7654
7652
  if (typeof callback !== "function" && typeof callback !== "string") {
7655
7653
  throw new TypeError("Utils.setTimeout 参数 callback 必须为 function|string 类型");
7656
7654
  }
@@ -7658,18 +7656,17 @@ class Utils {
7658
7656
  throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
7659
7657
  }
7660
7658
  return new Promise((resolve) => {
7661
- that.workerSetTimeout(() => {
7662
- resolve(that.tryCatch().run(callback));
7659
+ setTimeout(() => {
7660
+ resolve(this.tryCatch().run(callback));
7663
7661
  }, delayTime);
7664
7662
  });
7665
7663
  }
7666
7664
  sleep(delayTime = 0) {
7667
- const that = this;
7668
7665
  if (typeof delayTime !== "number") {
7669
7666
  throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
7670
7667
  }
7671
7668
  return new Promise((resolve) => {
7672
- that.workerSetTimeout(() => {
7669
+ setTimeout(() => {
7673
7670
  resolve(void 0);
7674
7671
  }, delayTime);
7675
7672
  });
@@ -7995,13 +7992,12 @@ class Utils {
7995
7992
  });
7996
7993
  }
7997
7994
  waitPropertyByInterval(checkFn, propertyName, intervalTimer = 250, maxTime = -1) {
7998
- const that = this;
7999
7995
  if (checkFn == null) {
8000
7996
  throw new TypeError("checkObj 不能为空对象 ");
8001
7997
  }
8002
7998
  let isResolve = false;
8003
7999
  return new Promise((resolve, reject) => {
8004
- const interval = that.workerSetInterval(() => {
8000
+ const interval = setInterval(() => {
8005
8001
  let inst = checkFn;
8006
8002
  if (typeof checkFn === "function") {
8007
8003
  inst = checkFn();
@@ -8014,14 +8010,14 @@ class Utils {
8014
8010
  }
8015
8011
  if ((typeof propertyName === "function" && propertyName(inst)) || Reflect.has(inst, propertyName)) {
8016
8012
  isResolve = true;
8017
- that.workerClearInterval(interval);
8013
+ clearInterval(interval);
8018
8014
  resolve(inst[propertyName]);
8019
8015
  }
8020
8016
  }, intervalTimer);
8021
8017
  if (maxTime !== -1) {
8022
- that.workerSetTimeout(() => {
8018
+ setTimeout(() => {
8023
8019
  if (!isResolve) {
8024
- that.workerClearInterval(interval);
8020
+ clearInterval(interval);
8025
8021
  reject();
8026
8022
  }
8027
8023
  }, maxTime);
@@ -8243,9 +8239,8 @@ class Utils {
8243
8239
  workerSetTimeout(callback, timeout = 0) {
8244
8240
  try {
8245
8241
  return setTimeout$1(callback, timeout);
8246
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8247
8242
  }
8248
- catch (error) {
8243
+ catch {
8249
8244
  return this.windowApi.setTimeout(callback, timeout);
8250
8245
  }
8251
8246
  }
@@ -8258,12 +8253,8 @@ class Utils {
8258
8253
  if (timeId != null) {
8259
8254
  clearTimeout$1(timeId);
8260
8255
  }
8261
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8262
- }
8263
- catch (error) {
8264
- // console.log(error);
8265
8256
  }
8266
- finally {
8257
+ catch {
8267
8258
  this.windowApi.clearTimeout(timeId);
8268
8259
  }
8269
8260
  }
@@ -8274,10 +8265,9 @@ class Utils {
8274
8265
  */
8275
8266
  workerSetInterval(callback, timeout = 0) {
8276
8267
  try {
8277
- return setInterval(callback, timeout);
8278
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8268
+ return setInterval$1(callback, timeout);
8279
8269
  }
8280
- catch (error) {
8270
+ catch {
8281
8271
  return this.windowApi.setInterval(callback, timeout);
8282
8272
  }
8283
8273
  }
@@ -8288,14 +8278,10 @@ class Utils {
8288
8278
  workerClearInterval(timeId) {
8289
8279
  try {
8290
8280
  if (timeId != null) {
8291
- clearInterval(timeId);
8281
+ clearInterval$1(timeId);
8292
8282
  }
8293
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8294
- }
8295
- catch (error) {
8296
- // console.log(error);
8297
8283
  }
8298
- finally {
8284
+ catch {
8299
8285
  this.windowApi.clearInterval(timeId);
8300
8286
  }
8301
8287
  }
@@ -8316,6 +8302,66 @@ class Utils {
8316
8302
  return new FunctionConstructor(...args);
8317
8303
  }
8318
8304
  }
8305
+ /**
8306
+ * 判断页面中是否存在`worker-src`的CSP规则
8307
+ */
8308
+ hasWorkerCSP() {
8309
+ return new Promise((resolve) => {
8310
+ let flag = true;
8311
+ let workerBlobUrl = void 0;
8312
+ const workerJs = /*js*/ `
8313
+ (() => {
8314
+ this.addEventListener(
8315
+ "message",
8316
+ function () {
8317
+ this.postMessage({
8318
+ success: true,
8319
+ });
8320
+ },
8321
+ {
8322
+ capture: true,
8323
+ }
8324
+ );
8325
+ })();`;
8326
+ try {
8327
+ const workerScript = new Blob([workerJs], {
8328
+ type: "application/javascript",
8329
+ });
8330
+ workerBlobUrl = window.URL.createObjectURL(workerScript);
8331
+ // @ts-expect-error
8332
+ if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
8333
+ // 使用这个后虽然不报错,但是仍会有blob错误
8334
+ // violates the following Content Security Policy directive: "worker-src 'self'". The action has been blocked.
8335
+ // 且这个错误无法使用try/catch捕捉,导致本该提醒使用手动匹配的结果并无提醒弹窗
8336
+ // @ts-expect-error
8337
+ const workerPolicy = globalThis.trustedTypes.createPolicy("workerPolicy", {
8338
+ createScriptURL: (url) => url,
8339
+ });
8340
+ workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
8341
+ }
8342
+ const worker = new Worker(workerBlobUrl);
8343
+ worker.onmessage = (data) => {
8344
+ if (data.data.success) {
8345
+ flag = false;
8346
+ }
8347
+ };
8348
+ setTimeout(() => {
8349
+ worker.terminate();
8350
+ resolve(flag);
8351
+ }, 500);
8352
+ worker.postMessage("test");
8353
+ }
8354
+ catch {
8355
+ flag = true;
8356
+ }
8357
+ finally {
8358
+ // 释放
8359
+ if (typeof workerBlobUrl === "string") {
8360
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
8361
+ }
8362
+ }
8363
+ });
8364
+ }
8319
8365
  }
8320
8366
  const utils = new Utils();
8321
8367