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