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