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