@whitesev/utils 2.9.13 → 2.11.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.11.0";
241
241
 
242
242
  /* eslint-disable */
243
243
  // ==UserScript==
@@ -5334,7 +5334,6 @@ class GMMenu {
5334
5334
  }
5335
5335
  }
5336
5336
 
5337
- // @ts-nocheck
5338
5337
  const VueUtils = {
5339
5338
  /** 标签 */
5340
5339
  ReactiveFlags: {
@@ -5374,9 +5373,11 @@ class ReactiveEffect {
5374
5373
  active = true;
5375
5374
  fn;
5376
5375
  scheduler;
5377
- constructor(fn, scheduler) {
5376
+ options;
5377
+ constructor(fn, scheduler, options) {
5378
5378
  this.fn = fn;
5379
5379
  this.scheduler = scheduler;
5380
+ this.options = options; // 默认值为'same'
5380
5381
  }
5381
5382
  run(cb) {
5382
5383
  if (!this.active) {
@@ -5394,6 +5395,18 @@ class ReactiveEffect {
5394
5395
  }
5395
5396
  }
5396
5397
  }
5398
+ stop() {
5399
+ if (this.active) {
5400
+ // 清除依赖关系
5401
+ if (this.deps && this.deps.length) {
5402
+ this.deps.forEach((dep) => {
5403
+ dep.delete(this);
5404
+ });
5405
+ this.deps.length = 0;
5406
+ }
5407
+ this.active = false;
5408
+ }
5409
+ }
5397
5410
  }
5398
5411
  class RefImpl {
5399
5412
  _value;
@@ -5463,9 +5476,7 @@ class Vue {
5463
5476
  set(target, key, value, receiver) {
5464
5477
  const oldValue = target[key];
5465
5478
  const result = Reflect.set(target, key, value, receiver);
5466
- if (oldValue !== value) {
5467
- that.trigger(target, "set", key, oldValue, value);
5468
- }
5479
+ that.trigger(target, "set", key, oldValue, value);
5469
5480
  return result;
5470
5481
  },
5471
5482
  });
@@ -5476,8 +5487,9 @@ class Vue {
5476
5487
  * 观察被reactive的对象值改变
5477
5488
  * @param source 被观察的对象,这里采用函数返回对象
5478
5489
  * @param changeCallBack 值改变的回调
5490
+ * @param options 配置项
5479
5491
  */
5480
- watch(source, changeCallBack) {
5492
+ watch(source, changeCallBack, options) {
5481
5493
  let getter;
5482
5494
  if (VueUtils.isReactive(source)) {
5483
5495
  getter = () => this.traversal(source);
@@ -5489,17 +5501,35 @@ class Vue {
5489
5501
  return;
5490
5502
  }
5491
5503
  let oldValue;
5504
+ const unwatch = () => {
5505
+ effect.stop();
5506
+ };
5492
5507
  const job = () => {
5493
5508
  const newValue = effect.run((activeEffect) => {
5494
5509
  this.activeEffect = activeEffect;
5495
5510
  });
5496
5511
  changeCallBack(newValue, oldValue);
5512
+ if (options?.once) {
5513
+ // 仅触发一次
5514
+ unwatch();
5515
+ }
5497
5516
  oldValue = newValue;
5498
5517
  };
5499
- const effect = new ReactiveEffect(getter, job);
5518
+ const effect = new ReactiveEffect(getter, job, {
5519
+ triggerMethod: "not-same",
5520
+ ...(options ?? {}),
5521
+ });
5500
5522
  oldValue = effect.run((activeEffect) => {
5501
5523
  this.activeEffect = activeEffect;
5502
5524
  });
5525
+ if (options) {
5526
+ if (options.immediate) {
5527
+ job();
5528
+ }
5529
+ }
5530
+ return {
5531
+ unwatch,
5532
+ };
5503
5533
  }
5504
5534
  toReactive(value) {
5505
5535
  return VueUtils.isObject(value) ? this.reactive(value) : value;
@@ -5517,28 +5547,40 @@ class Vue {
5517
5547
  }
5518
5548
  return result;
5519
5549
  }
5520
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5521
5550
  trigger(target, type, key, oldValue, value) {
5522
5551
  const depsMap = this.targetMap.get(target);
5523
5552
  if (!depsMap)
5524
5553
  return;
5525
5554
  const effects = depsMap.get(key);
5526
- this.triggerEffect(effects, "effects");
5555
+ this.triggerEffect(effects, type, "effects", oldValue, value);
5527
5556
  }
5528
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5529
- triggerEffect(effects, name) {
5557
+ triggerEffect(effects, _type, _name, oldValue, value) {
5530
5558
  if (effects) {
5559
+ const isSame = oldValue === value;
5531
5560
  effects.forEach((effect) => {
5532
- if (effect.scheduler) {
5533
- effect.scheduler();
5561
+ if (effect.options.triggerMethod === "not-same") {
5562
+ if (isSame) {
5563
+ return;
5564
+ }
5565
+ if (effect.scheduler) {
5566
+ effect.scheduler();
5567
+ }
5568
+ else {
5569
+ effect.run();
5570
+ }
5534
5571
  }
5535
- else {
5536
- effect.run();
5572
+ else if (effect.options.triggerMethod === "set") {
5573
+ if (effect.scheduler) {
5574
+ effect.scheduler();
5575
+ }
5576
+ else {
5577
+ effect.run();
5578
+ }
5537
5579
  }
5538
5580
  });
5539
5581
  }
5540
5582
  }
5541
- track(target, type, key) {
5583
+ track(target, _type, key) {
5542
5584
  if (!this.activeEffect)
5543
5585
  return;
5544
5586
  let depsMap = this.targetMap.get(target);
@@ -5718,8 +5760,8 @@ class Utils {
5718
5760
  let timer = null;
5719
5761
  const that = this;
5720
5762
  return function (...args) {
5721
- that.workerClearTimeout(timer);
5722
- timer = that.workerSetTimeout(function () {
5763
+ clearTimeout(timer);
5764
+ timer = setTimeout(function () {
5723
5765
  fn.apply(that, args);
5724
5766
  }, delay);
5725
5767
  };
@@ -5738,7 +5780,6 @@ class Utils {
5738
5780
  **/
5739
5781
  Dictionary = UtilsDictionary;
5740
5782
  downloadBase64(base64Data, fileName, isIFrame = false) {
5741
- const that = this;
5742
5783
  if (typeof base64Data !== "string") {
5743
5784
  throw new Error("Utils.downloadBase64 参数 base64Data 必须为 string 类型");
5744
5785
  }
@@ -5751,7 +5792,7 @@ class Utils {
5751
5792
  $iframe.style.display = "none";
5752
5793
  $iframe.src = base64Data;
5753
5794
  (this.windowApi.document.body || this.windowApi.document.documentElement).appendChild($iframe);
5754
- that.workerSetTimeout(() => {
5795
+ setTimeout(() => {
5755
5796
  $iframe.contentWindow.document.execCommand("SaveAs", true, fileName);
5756
5797
  (this.windowApi.document.body || this.windowApi.document.documentElement).removeChild($iframe);
5757
5798
  }, 100);
@@ -7650,7 +7691,6 @@ class Utils {
7650
7691
  });
7651
7692
  }
7652
7693
  setTimeout(callback, delayTime = 0) {
7653
- const that = this;
7654
7694
  if (typeof callback !== "function" && typeof callback !== "string") {
7655
7695
  throw new TypeError("Utils.setTimeout 参数 callback 必须为 function|string 类型");
7656
7696
  }
@@ -7658,18 +7698,17 @@ class Utils {
7658
7698
  throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
7659
7699
  }
7660
7700
  return new Promise((resolve) => {
7661
- that.workerSetTimeout(() => {
7662
- resolve(that.tryCatch().run(callback));
7701
+ setTimeout(() => {
7702
+ resolve(this.tryCatch().run(callback));
7663
7703
  }, delayTime);
7664
7704
  });
7665
7705
  }
7666
7706
  sleep(delayTime = 0) {
7667
- const that = this;
7668
7707
  if (typeof delayTime !== "number") {
7669
7708
  throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
7670
7709
  }
7671
7710
  return new Promise((resolve) => {
7672
- that.workerSetTimeout(() => {
7711
+ setTimeout(() => {
7673
7712
  resolve(void 0);
7674
7713
  }, delayTime);
7675
7714
  });
@@ -7995,13 +8034,12 @@ class Utils {
7995
8034
  });
7996
8035
  }
7997
8036
  waitPropertyByInterval(checkFn, propertyName, intervalTimer = 250, maxTime = -1) {
7998
- const that = this;
7999
8037
  if (checkFn == null) {
8000
8038
  throw new TypeError("checkObj 不能为空对象 ");
8001
8039
  }
8002
8040
  let isResolve = false;
8003
8041
  return new Promise((resolve, reject) => {
8004
- const interval = that.workerSetInterval(() => {
8042
+ const interval = setInterval(() => {
8005
8043
  let inst = checkFn;
8006
8044
  if (typeof checkFn === "function") {
8007
8045
  inst = checkFn();
@@ -8014,14 +8052,14 @@ class Utils {
8014
8052
  }
8015
8053
  if ((typeof propertyName === "function" && propertyName(inst)) || Reflect.has(inst, propertyName)) {
8016
8054
  isResolve = true;
8017
- that.workerClearInterval(interval);
8055
+ clearInterval(interval);
8018
8056
  resolve(inst[propertyName]);
8019
8057
  }
8020
8058
  }, intervalTimer);
8021
8059
  if (maxTime !== -1) {
8022
- that.workerSetTimeout(() => {
8060
+ setTimeout(() => {
8023
8061
  if (!isResolve) {
8024
- that.workerClearInterval(interval);
8062
+ clearInterval(interval);
8025
8063
  reject();
8026
8064
  }
8027
8065
  }, maxTime);
@@ -8224,14 +8262,39 @@ class Utils {
8224
8262
  /**
8225
8263
  * 自定义的动态响应对象
8226
8264
  * @example
8227
- * let vue = new Utils.Vue();
8228
- * let reactive = new vue.reactive({});
8229
- * vue.watch(()=>reactive["name"], (newValue, oldValue)=>{
8265
+ * const vue = new Utils.Vue();
8266
+ * const reactive = vue.reactive({
8267
+ * name: "",
8268
+ * });
8269
+ * vue.watch(()=>reactive.name, (newValue, oldValue)=>{
8230
8270
  * console.log("newValue ==> " + newValue);
8231
8271
  * console.log("oldValue ==> " + oldValue);
8232
8272
  * })
8233
- * vue["name"] = "测试";
8234
- * > "测试"
8273
+ * reactive.name = "测试";
8274
+ * > newValue ==> 测试
8275
+ * > oldValue ==>
8276
+ * reactive.name = "null";
8277
+ * > newValue ==> null
8278
+ * > oldValue ==> 测试
8279
+ * reactive.name = "null";
8280
+ * @example
8281
+ * const vue = new Utils.Vue();
8282
+ * const reactive = vue.reactive({
8283
+ * name: "",
8284
+ * });
8285
+ * vue.watch(()=>reactive.name, (newValue, oldValue)=>{
8286
+ * console.log("newValue ==> " + newValue);
8287
+ * console.log("oldValue ==> " + oldValue);
8288
+ * },{
8289
+ * triggerMethod: "set",
8290
+ * })
8291
+ * reactive.name = "测试";
8292
+ * > newValue ==> 测试
8293
+ * > oldValue ==>
8294
+ * reactive.name = "测试";
8295
+ * > newValue ==> 测试
8296
+ * > oldValue ==> 测试
8297
+ *
8235
8298
  */
8236
8299
  Vue = Vue;
8237
8300
  ModuleRaid = ModuleRaid;
@@ -8243,9 +8306,8 @@ class Utils {
8243
8306
  workerSetTimeout(callback, timeout = 0) {
8244
8307
  try {
8245
8308
  return setTimeout$1(callback, timeout);
8246
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8247
8309
  }
8248
- catch (error) {
8310
+ catch {
8249
8311
  return this.windowApi.setTimeout(callback, timeout);
8250
8312
  }
8251
8313
  }
@@ -8258,12 +8320,8 @@ class Utils {
8258
8320
  if (timeId != null) {
8259
8321
  clearTimeout$1(timeId);
8260
8322
  }
8261
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8262
- }
8263
- catch (error) {
8264
- // console.log(error);
8265
8323
  }
8266
- finally {
8324
+ catch {
8267
8325
  this.windowApi.clearTimeout(timeId);
8268
8326
  }
8269
8327
  }
@@ -8274,10 +8332,9 @@ class Utils {
8274
8332
  */
8275
8333
  workerSetInterval(callback, timeout = 0) {
8276
8334
  try {
8277
- return setInterval(callback, timeout);
8278
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8335
+ return setInterval$1(callback, timeout);
8279
8336
  }
8280
- catch (error) {
8337
+ catch {
8281
8338
  return this.windowApi.setInterval(callback, timeout);
8282
8339
  }
8283
8340
  }
@@ -8288,14 +8345,10 @@ class Utils {
8288
8345
  workerClearInterval(timeId) {
8289
8346
  try {
8290
8347
  if (timeId != null) {
8291
- clearInterval(timeId);
8348
+ clearInterval$1(timeId);
8292
8349
  }
8293
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8294
- }
8295
- catch (error) {
8296
- // console.log(error);
8297
8350
  }
8298
- finally {
8351
+ catch {
8299
8352
  this.windowApi.clearInterval(timeId);
8300
8353
  }
8301
8354
  }
@@ -8316,6 +8369,66 @@ class Utils {
8316
8369
  return new FunctionConstructor(...args);
8317
8370
  }
8318
8371
  }
8372
+ /**
8373
+ * 判断页面中是否存在`worker-src`的CSP规则
8374
+ */
8375
+ hasWorkerCSP() {
8376
+ return new Promise((resolve) => {
8377
+ let flag = true;
8378
+ let workerBlobUrl = void 0;
8379
+ const workerJs = /*js*/ `
8380
+ (() => {
8381
+ this.addEventListener(
8382
+ "message",
8383
+ function () {
8384
+ this.postMessage({
8385
+ success: true,
8386
+ });
8387
+ },
8388
+ {
8389
+ capture: true,
8390
+ }
8391
+ );
8392
+ })();`;
8393
+ try {
8394
+ const workerScript = new Blob([workerJs], {
8395
+ type: "application/javascript",
8396
+ });
8397
+ workerBlobUrl = window.URL.createObjectURL(workerScript);
8398
+ // @ts-expect-error
8399
+ if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
8400
+ // 使用这个后虽然不报错,但是仍会有blob错误
8401
+ // violates the following Content Security Policy directive: "worker-src 'self'". The action has been blocked.
8402
+ // 且这个错误无法使用try/catch捕捉,导致本该提醒使用手动匹配的结果并无提醒弹窗
8403
+ // @ts-expect-error
8404
+ const workerPolicy = globalThis.trustedTypes.createPolicy("workerPolicy", {
8405
+ createScriptURL: (url) => url,
8406
+ });
8407
+ workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
8408
+ }
8409
+ const worker = new Worker(workerBlobUrl);
8410
+ worker.onmessage = (data) => {
8411
+ if (data.data.success) {
8412
+ flag = false;
8413
+ }
8414
+ };
8415
+ setTimeout(() => {
8416
+ worker.terminate();
8417
+ resolve(flag);
8418
+ }, 500);
8419
+ worker.postMessage("test");
8420
+ }
8421
+ catch {
8422
+ flag = true;
8423
+ }
8424
+ finally {
8425
+ // 释放
8426
+ if (typeof workerBlobUrl === "string") {
8427
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
8428
+ }
8429
+ }
8430
+ });
8431
+ }
8319
8432
  }
8320
8433
  const utils = new Utils();
8321
8434