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