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