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