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