@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.cjs.js CHANGED
@@ -234,12 +234,12 @@ const createLoadOrReturnBroker = (loadBroker, worker) => {
234
234
  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
235
235
 
236
236
  const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
237
- const clearInterval = (timerId) => loadOrReturnBroker().clearInterval(timerId);
237
+ const clearInterval$1 = (timerId) => loadOrReturnBroker().clearInterval(timerId);
238
238
  const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
239
- const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
239
+ const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
240
240
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
241
241
 
242
- const version = "2.9.13";
242
+ const version = "2.10.0";
243
243
 
244
244
  /* eslint-disable */
245
245
  // ==UserScript==
@@ -5720,8 +5720,8 @@ class Utils {
5720
5720
  let timer = null;
5721
5721
  const that = this;
5722
5722
  return function (...args) {
5723
- that.workerClearTimeout(timer);
5724
- timer = that.workerSetTimeout(function () {
5723
+ clearTimeout(timer);
5724
+ timer = setTimeout(function () {
5725
5725
  fn.apply(that, args);
5726
5726
  }, delay);
5727
5727
  };
@@ -5740,7 +5740,6 @@ class Utils {
5740
5740
  **/
5741
5741
  Dictionary = UtilsDictionary;
5742
5742
  downloadBase64(base64Data, fileName, isIFrame = false) {
5743
- const that = this;
5744
5743
  if (typeof base64Data !== "string") {
5745
5744
  throw new Error("Utils.downloadBase64 参数 base64Data 必须为 string 类型");
5746
5745
  }
@@ -5753,7 +5752,7 @@ class Utils {
5753
5752
  $iframe.style.display = "none";
5754
5753
  $iframe.src = base64Data;
5755
5754
  (this.windowApi.document.body || this.windowApi.document.documentElement).appendChild($iframe);
5756
- that.workerSetTimeout(() => {
5755
+ setTimeout(() => {
5757
5756
  $iframe.contentWindow.document.execCommand("SaveAs", true, fileName);
5758
5757
  (this.windowApi.document.body || this.windowApi.document.documentElement).removeChild($iframe);
5759
5758
  }, 100);
@@ -7652,7 +7651,6 @@ class Utils {
7652
7651
  });
7653
7652
  }
7654
7653
  setTimeout(callback, delayTime = 0) {
7655
- const that = this;
7656
7654
  if (typeof callback !== "function" && typeof callback !== "string") {
7657
7655
  throw new TypeError("Utils.setTimeout 参数 callback 必须为 function|string 类型");
7658
7656
  }
@@ -7660,18 +7658,17 @@ class Utils {
7660
7658
  throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
7661
7659
  }
7662
7660
  return new Promise((resolve) => {
7663
- that.workerSetTimeout(() => {
7664
- resolve(that.tryCatch().run(callback));
7661
+ setTimeout(() => {
7662
+ resolve(this.tryCatch().run(callback));
7665
7663
  }, delayTime);
7666
7664
  });
7667
7665
  }
7668
7666
  sleep(delayTime = 0) {
7669
- const that = this;
7670
7667
  if (typeof delayTime !== "number") {
7671
7668
  throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
7672
7669
  }
7673
7670
  return new Promise((resolve) => {
7674
- that.workerSetTimeout(() => {
7671
+ setTimeout(() => {
7675
7672
  resolve(void 0);
7676
7673
  }, delayTime);
7677
7674
  });
@@ -7997,13 +7994,12 @@ class Utils {
7997
7994
  });
7998
7995
  }
7999
7996
  waitPropertyByInterval(checkFn, propertyName, intervalTimer = 250, maxTime = -1) {
8000
- const that = this;
8001
7997
  if (checkFn == null) {
8002
7998
  throw new TypeError("checkObj 不能为空对象 ");
8003
7999
  }
8004
8000
  let isResolve = false;
8005
8001
  return new Promise((resolve, reject) => {
8006
- const interval = that.workerSetInterval(() => {
8002
+ const interval = setInterval(() => {
8007
8003
  let inst = checkFn;
8008
8004
  if (typeof checkFn === "function") {
8009
8005
  inst = checkFn();
@@ -8016,14 +8012,14 @@ class Utils {
8016
8012
  }
8017
8013
  if ((typeof propertyName === "function" && propertyName(inst)) || Reflect.has(inst, propertyName)) {
8018
8014
  isResolve = true;
8019
- that.workerClearInterval(interval);
8015
+ clearInterval(interval);
8020
8016
  resolve(inst[propertyName]);
8021
8017
  }
8022
8018
  }, intervalTimer);
8023
8019
  if (maxTime !== -1) {
8024
- that.workerSetTimeout(() => {
8020
+ setTimeout(() => {
8025
8021
  if (!isResolve) {
8026
- that.workerClearInterval(interval);
8022
+ clearInterval(interval);
8027
8023
  reject();
8028
8024
  }
8029
8025
  }, maxTime);
@@ -8245,9 +8241,8 @@ class Utils {
8245
8241
  workerSetTimeout(callback, timeout = 0) {
8246
8242
  try {
8247
8243
  return setTimeout$1(callback, timeout);
8248
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8249
8244
  }
8250
- catch (error) {
8245
+ catch {
8251
8246
  return this.windowApi.setTimeout(callback, timeout);
8252
8247
  }
8253
8248
  }
@@ -8260,12 +8255,8 @@ class Utils {
8260
8255
  if (timeId != null) {
8261
8256
  clearTimeout$1(timeId);
8262
8257
  }
8263
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8264
- }
8265
- catch (error) {
8266
- // console.log(error);
8267
8258
  }
8268
- finally {
8259
+ catch {
8269
8260
  this.windowApi.clearTimeout(timeId);
8270
8261
  }
8271
8262
  }
@@ -8276,10 +8267,9 @@ class Utils {
8276
8267
  */
8277
8268
  workerSetInterval(callback, timeout = 0) {
8278
8269
  try {
8279
- return setInterval(callback, timeout);
8280
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8270
+ return setInterval$1(callback, timeout);
8281
8271
  }
8282
- catch (error) {
8272
+ catch {
8283
8273
  return this.windowApi.setInterval(callback, timeout);
8284
8274
  }
8285
8275
  }
@@ -8290,14 +8280,10 @@ class Utils {
8290
8280
  workerClearInterval(timeId) {
8291
8281
  try {
8292
8282
  if (timeId != null) {
8293
- clearInterval(timeId);
8283
+ clearInterval$1(timeId);
8294
8284
  }
8295
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8296
- }
8297
- catch (error) {
8298
- // console.log(error);
8299
8285
  }
8300
- finally {
8286
+ catch {
8301
8287
  this.windowApi.clearInterval(timeId);
8302
8288
  }
8303
8289
  }
@@ -8318,6 +8304,66 @@ class Utils {
8318
8304
  return new FunctionConstructor(...args);
8319
8305
  }
8320
8306
  }
8307
+ /**
8308
+ * 判断页面中是否存在`worker-src`的CSP规则
8309
+ */
8310
+ hasWorkerCSP() {
8311
+ return new Promise((resolve) => {
8312
+ let flag = true;
8313
+ let workerBlobUrl = void 0;
8314
+ const workerJs = /*js*/ `
8315
+ (() => {
8316
+ this.addEventListener(
8317
+ "message",
8318
+ function () {
8319
+ this.postMessage({
8320
+ success: true,
8321
+ });
8322
+ },
8323
+ {
8324
+ capture: true,
8325
+ }
8326
+ );
8327
+ })();`;
8328
+ try {
8329
+ const workerScript = new Blob([workerJs], {
8330
+ type: "application/javascript",
8331
+ });
8332
+ workerBlobUrl = window.URL.createObjectURL(workerScript);
8333
+ // @ts-expect-error
8334
+ if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
8335
+ // 使用这个后虽然不报错,但是仍会有blob错误
8336
+ // violates the following Content Security Policy directive: "worker-src 'self'". The action has been blocked.
8337
+ // 且这个错误无法使用try/catch捕捉,导致本该提醒使用手动匹配的结果并无提醒弹窗
8338
+ // @ts-expect-error
8339
+ const workerPolicy = globalThis.trustedTypes.createPolicy("workerPolicy", {
8340
+ createScriptURL: (url) => url,
8341
+ });
8342
+ workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
8343
+ }
8344
+ const worker = new Worker(workerBlobUrl);
8345
+ worker.onmessage = (data) => {
8346
+ if (data.data.success) {
8347
+ flag = false;
8348
+ }
8349
+ };
8350
+ setTimeout(() => {
8351
+ worker.terminate();
8352
+ resolve(flag);
8353
+ }, 500);
8354
+ worker.postMessage("test");
8355
+ }
8356
+ catch {
8357
+ flag = true;
8358
+ }
8359
+ finally {
8360
+ // 释放
8361
+ if (typeof workerBlobUrl === "string") {
8362
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
8363
+ }
8364
+ }
8365
+ });
8366
+ }
8321
8367
  }
8322
8368
  const utils = new Utils();
8323
8369