@whitesev/utils 2.11.10 → 2.11.12

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.
@@ -242,7 +242,7 @@ System.register('Utils', [], (function (exports) {
242
242
  const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
243
243
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
244
244
 
245
- const version = "2.11.10";
245
+ const version = "2.11.12";
246
246
 
247
247
  /* eslint-disable */
248
248
  // ==UserScript==
@@ -377,8 +377,12 @@ System.register('Utils', [], (function (exports) {
377
377
  }));
378
378
  }
379
379
  waitForRequestKeys() {
380
+ /**
381
+ * @type {Set<typeof hookInst>}
382
+ */
383
+ const winHookInsts = win.__ajaxHooker.hookInsts;
380
384
  if (!this.request.async) {
381
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
385
+ winHookInsts.forEach(({ hookFns, filters }) => {
382
386
  if (this.shouldFilter(filters))
383
387
  return;
384
388
  hookFns.forEach((fn) => {
@@ -394,10 +398,10 @@ System.register('Utils', [], (function (exports) {
394
398
  }
395
399
  const promises = [];
396
400
  const ignoreKeys = new Set(["type", "async", "response"]);
397
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
401
+ winHookInsts.forEach(({ hookFns, filters }) => {
398
402
  if (this.shouldFilter(filters))
399
403
  return;
400
- promises.push(Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(() => {
404
+ promises.push(Promise.all(hookFns.map((fn, index) => catchError(fn, this.request, index))).then(() => {
401
405
  const requestKeys = [];
402
406
  for (const key in this.request)
403
407
  !ignoreKeys.has(key) && requestKeys.push(key);
@@ -819,11 +823,52 @@ System.register('Utils', [], (function (exports) {
819
823
  });
820
824
  });
821
825
  }
826
+ /**
827
+ *
828
+ * @type {import("./../types/ajaxHooker.d.ts").UtilsAjaxHookResult["removeHook"]}
829
+ */
830
+ const removeHook = (fn, onlyRemove = false) => {
831
+ let flag = false;
832
+ for (let index = hookInst.hookFns.length - 1; index >= 0; index--) {
833
+ const __fn__ = hookInst.hookFns[index];
834
+ if (fn === __fn__) {
835
+ hookInst.hookFns.splice(index, 1);
836
+ flag = true;
837
+ if (onlyRemove) {
838
+ break;
839
+ }
840
+ }
841
+ }
842
+ return flag;
843
+ };
844
+ /**
845
+ *
846
+ * @type {import("./../types/ajaxHooker.d.ts").UtilsAjaxHookResult["removeFilter"]}
847
+ */
848
+ const removeFilter = () => {
849
+ if (Array.isArray(hookInst.filters)) {
850
+ hookInst.filters.length = 0;
851
+ }
852
+ else {
853
+ hookInst.filters = [];
854
+ }
855
+ };
822
856
  return {
823
- hook: (fn) => hookInst.hookFns.push(fn),
857
+ hook: (fn) => {
858
+ hookInst.hookFns.push(fn);
859
+ return {
860
+ remove: () => {
861
+ return removeHook(fn, true);
862
+ }
863
+ };
864
+ },
824
865
  filter: (arr) => {
825
- if (Array.isArray(arr))
866
+ if (Array.isArray(arr)) {
826
867
  hookInst.filters = arr;
868
+ }
869
+ return {
870
+ remove: removeFilter
871
+ };
827
872
  },
828
873
  protect: () => {
829
874
  readonly(win, "XMLHttpRequest", winAh.fakeXHR);
@@ -839,6 +884,8 @@ System.register('Utils', [], (function (exports) {
839
884
  delete win.__ajaxHooker;
840
885
  }
841
886
  },
887
+ removeHook: removeHook,
888
+ removeFilter: removeFilter
842
889
  };
843
890
  };
844
891
 
@@ -1237,10 +1284,50 @@ System.register('Utils', [], (function (exports) {
1237
1284
  Object.keys(realXhr).forEach((key) => (fakeXhr[key] = realXhr[key]));
1238
1285
  fakeXhr.prototype = realXhr.prototype;
1239
1286
  win.fetch = fakeFetch;
1287
+ /**
1288
+ *
1289
+ * @type {import("./../types/ajaxHooker.d.ts").UtilsAjaxHookResult["removeHook"]}
1290
+ */
1291
+ const removeHook = (fn, onlyRemove = false) => {
1292
+ let flag = false;
1293
+ for (let index = hookFns.length - 1; index >= 0; index--) {
1294
+ const __fn__ = hookFns[index];
1295
+ if (fn === __fn__) {
1296
+ hookFns.splice(index, 1);
1297
+ flag = true;
1298
+ if (onlyRemove) {
1299
+ break;
1300
+ }
1301
+ }
1302
+ }
1303
+ return flag;
1304
+ };
1305
+ /**
1306
+ *
1307
+ * @type {import("./../types/ajaxHooker.d.ts").UtilsAjaxHookResult["removeFilter"]}
1308
+ */
1309
+ const removeFilter = () => {
1310
+ if (Array.isArray(filter)) {
1311
+ filter.length = 0;
1312
+ }
1313
+ else {
1314
+ filter = void 0;
1315
+ }
1316
+ };
1240
1317
  return {
1241
- hook: (fn) => hookFns.push(fn),
1318
+ hook: (fn) => {
1319
+ hookFns.push(fn);
1320
+ return {
1321
+ remove: () => {
1322
+ return removeHook(fn, true);
1323
+ }
1324
+ };
1325
+ },
1242
1326
  filter: (arr) => {
1243
1327
  filter = Array.isArray(arr) && arr.map(toFilterObj);
1328
+ return {
1329
+ remove: removeFilter
1330
+ };
1244
1331
  },
1245
1332
  protect: () => {
1246
1333
  readonly(win, "XMLHttpRequest", fakeXhr);
@@ -1250,6 +1337,8 @@ System.register('Utils', [], (function (exports) {
1250
1337
  writable(win, "XMLHttpRequest", realXhr);
1251
1338
  writable(win, "fetch", realFetch);
1252
1339
  },
1340
+ removeHook: removeHook,
1341
+ removeFilter: removeFilter
1253
1342
  };
1254
1343
  })();
1255
1344
  };
@@ -2674,42 +2763,42 @@ System.register('Utils', [], (function (exports) {
2674
2763
  const method = requestOption.method;
2675
2764
  if (method === "GET" || method === "HEAD") {
2676
2765
  // GET类型,data如果有,那么需要转为searchParams
2677
- const urlObj = new URL(requestOption.url);
2766
+ const urlInst = new URL(requestOption.url);
2678
2767
  let urlSearch = "";
2679
- let isHandler = false;
2768
+ let deleteData = false;
2680
2769
  if (typeof requestOption.data === "string") {
2681
- isHandler = true;
2770
+ deleteData = true;
2682
2771
  urlSearch = requestOption.data;
2683
2772
  }
2684
2773
  else if (typeof requestOption.data === "object") {
2685
- isHandler = true;
2774
+ deleteData = true;
2686
2775
  // URLSearchParams参数可以转普通的string:string,包括FormData
2687
2776
  const searchParams = new URLSearchParams(requestOption.data);
2688
2777
  urlSearch = searchParams.toString();
2689
2778
  }
2690
- if (isHandler) {
2779
+ if (deleteData) {
2691
2780
  // GET/HEAD请求不支持data参数
2692
2781
  // 对data进行处理了才可以删除
2693
2782
  Reflect.deleteProperty(requestOption, "data");
2694
2783
  }
2695
- if (urlSearch != "") {
2696
- if (urlObj.search === "") {
2784
+ if (urlSearch.trim() != "") {
2785
+ if (urlInst.search.trim() === "") {
2697
2786
  // url没有search参数,直接覆盖
2698
- urlObj.search = urlSearch;
2787
+ urlInst.search = urlSearch;
2699
2788
  }
2700
2789
  else {
2701
2790
  // 有search参数
2702
- if (urlObj.search.endsWith("&")) {
2791
+ if (urlInst.search.trim().endsWith("&")) {
2703
2792
  // xxx=xxx&
2704
- urlObj.search = urlObj.search + urlSearch;
2793
+ urlInst.search = urlInst.search + urlSearch;
2705
2794
  }
2706
2795
  else {
2707
2796
  // xxx=xxx&xxx=
2708
- urlObj.search = `${urlObj.search}&${urlSearch}`;
2797
+ urlInst.search = `${urlInst.search}&${urlSearch}`;
2709
2798
  }
2710
2799
  }
2711
2800
  }
2712
- requestOption.url = urlObj.toString();
2801
+ requestOption.url = urlInst.toString();
2713
2802
  }
2714
2803
  else if (method === "POST" && requestOption.headers != null) {
2715
2804
  // POST类型,data如果是FormData,那么需要转为string
@@ -2719,10 +2808,10 @@ System.register('Utils', [], (function (exports) {
2719
2808
  typeof requestOption.headers[headerKey] === "string");
2720
2809
  });
2721
2810
  if (ContentTypeIndex !== -1) {
2722
- const ContentTypeKey = headersKeyList[ContentTypeIndex];
2723
- const ContentType = requestOption.headers[ContentTypeKey];
2811
+ const contentTypeKey = headersKeyList[ContentTypeIndex];
2812
+ const contentType = requestOption.headers[contentTypeKey].toLowerCase();
2724
2813
  // 设置了Content-Type
2725
- if (ContentType.includes("application/json")) {
2814
+ if (contentType.includes("application/json")) {
2726
2815
  // application/json
2727
2816
  if (requestOption.data instanceof FormData) {
2728
2817
  const entries = {};
@@ -2735,16 +2824,16 @@ System.register('Utils', [], (function (exports) {
2735
2824
  requestOption.data = JSON.stringify(requestOption.data);
2736
2825
  }
2737
2826
  }
2738
- else if (ContentType.includes("application/x-www-form-urlencoded")) {
2827
+ else if (contentType.includes("application/x-www-form-urlencoded")) {
2739
2828
  // application/x-www-form-urlencoded
2740
2829
  if (typeof requestOption.data === "object") {
2741
2830
  requestOption.data = new URLSearchParams(requestOption.data).toString();
2742
2831
  }
2743
2832
  }
2744
- else if (ContentType.includes("multipart/form-data")) {
2833
+ else if (contentType.includes("multipart/form-data")) {
2745
2834
  // multipart/form-data
2746
2835
  if (requestOption.data instanceof FormData) {
2747
- Reflect.deleteProperty(requestOption.headers, ContentTypeKey);
2836
+ Reflect.deleteProperty(requestOption.headers, contentTypeKey);
2748
2837
  }
2749
2838
  }
2750
2839
  }
@@ -5702,20 +5791,25 @@ System.register('Utils', [], (function (exports) {
5702
5791
  }
5703
5792
  /**
5704
5793
  * ajax劫持库,支持xhr和fetch劫持。
5705
- * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5706
- * + 作者:cxxjackie
5707
- * + 版本:1.4.8
5708
- * + 旧版本:1.2.4
5709
- * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
5710
- * @param useOldVersion 是否使用旧版本,默认false
5711
- */
5712
- ajaxHooker = (useOldVersion = false) => {
5713
- if (useOldVersion) {
5714
- return AjaxHooker1_2_4();
5715
- }
5716
- else {
5717
- return ajaxHooker();
5718
- }
5794
+ * + 来源: https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5795
+ * + 作者: cxxjackie
5796
+ * + 实现方式: Proxy
5797
+ * + 版本: `1.4.8`
5798
+ * + 文档: https://scriptcat.org/zh-CN/script-show-page/637/
5799
+ */
5800
+ ajaxHooker = () => {
5801
+ return ajaxHooker();
5802
+ };
5803
+ /**
5804
+ * ajax劫持库,支持xhr和fetch劫持。
5805
+ * + 来源: https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5806
+ * + 作者: cxxjackie
5807
+ * + 实现方式: Object.defineProperty
5808
+ * + 版本: `1.2.4`
5809
+ * + 文档: https://scriptcat.org/zh-CN/script-show-page/637/
5810
+ */
5811
+ oldAjaxHooker = () => {
5812
+ return AjaxHooker1_2_4();
5719
5813
  };
5720
5814
  canvasClickByPosition(canvasElement, clientX = 0, clientY = 0, view = this.windowApi.window) {
5721
5815
  if (!(canvasElement instanceof HTMLCanvasElement)) {
@@ -6309,10 +6403,11 @@ System.register('Utils', [], (function (exports) {
6309
6403
  left: maxRect.left,
6310
6404
  };
6311
6405
  }
6312
- let calcZIndex = zIndex + deviation;
6406
+ const calcZIndex = zIndex + deviation;
6313
6407
  if (calcZIndex >= maxZIndexCompare) {
6314
6408
  // 不要超过最大值
6315
- calcZIndex = maxZIndexCompare;
6409
+ // 超过就忽略
6410
+ return;
6316
6411
  }
6317
6412
  return {
6318
6413
  /** 计算偏移量后的z-index值 */
@@ -7239,6 +7334,26 @@ System.register('Utils', [], (function (exports) {
7239
7334
  }
7240
7335
  return content;
7241
7336
  }
7337
+ /**
7338
+ * 监听页面元素改变并处理
7339
+ * @param target 需要监听的元素,如果不存在,可以等待它出现
7340
+ * @param observer_config MutationObserver的配置
7341
+ * @example
7342
+ Utils.mutationObserver(document.querySelector("div.xxxx"),{
7343
+ "callback":(mutations, observer)=>{},
7344
+ "config":{childList:true,attributes:true}
7345
+ });
7346
+ * @example
7347
+ Utils.mutationObserver(document.querySelectorAll("div.xxxx"),{
7348
+ "callback":(mutations, observer)=>{},
7349
+ "config":{childList:true,attributes:true}}
7350
+ );
7351
+ * @example
7352
+ Utils.mutationObserver($("div.xxxx"),{
7353
+ "callback":(mutations, observer)=>{},
7354
+ "config":{childList:true,attributes:true}}
7355
+ );
7356
+ **/
7242
7357
  mutationObserver(target, observer_config) {
7243
7358
  const that = this;
7244
7359
  const default_obverser_config = {
@@ -7281,16 +7396,24 @@ System.register('Utils', [], (function (exports) {
7281
7396
  characterDataOldValue: void 0,
7282
7397
  },
7283
7398
  immediate: false,
7399
+ once: false,
7284
7400
  };
7285
7401
  observer_config = that.assign(default_obverser_config, observer_config);
7286
7402
  const windowMutationObserver = this.windowApi.window.MutationObserver ||
7287
7403
  this.windowApi.window.webkitMutationObserver ||
7288
7404
  this.windowApi.window.MozMutationObserver;
7289
7405
  // 观察者对象
7290
- const mutationObserver = new windowMutationObserver(function (mutations, observer) {
7406
+ const handler = (mutations, observer) => {
7407
+ if (observer_config.once) {
7408
+ // 仅触发一次
7409
+ observer.disconnect();
7410
+ }
7291
7411
  if (typeof observer_config.callback === "function") {
7292
7412
  observer_config.callback(mutations, observer);
7293
7413
  }
7414
+ };
7415
+ const mutationObserver = new windowMutationObserver(function (mutations, observer) {
7416
+ handler(mutations, observer);
7294
7417
  });
7295
7418
  if (Array.isArray(target) || target instanceof NodeList) {
7296
7419
  // 传入的是数组或者元素数组
@@ -7299,7 +7422,7 @@ System.register('Utils', [], (function (exports) {
7299
7422
  });
7300
7423
  }
7301
7424
  else if (that.isJQuery(target)) {
7302
- /* 传入的参数是jQuery对象 */
7425
+ // 传入的参数是jQuery对象
7303
7426
  target.each((_, item) => {
7304
7427
  mutationObserver.observe(item, observer_config.config);
7305
7428
  });
@@ -7308,16 +7431,14 @@ System.register('Utils', [], (function (exports) {
7308
7431
  mutationObserver.observe(target, observer_config.config);
7309
7432
  }
7310
7433
  if (observer_config.immediate) {
7311
- /* 主动触发一次 */
7312
- if (typeof observer_config.callback === "function") {
7313
- observer_config.callback([], mutationObserver);
7314
- }
7434
+ // 主动触发一次
7435
+ handler([], mutationObserver);
7315
7436
  }
7316
7437
  return mutationObserver;
7317
7438
  }
7318
7439
  /**
7319
7440
  * 使用观察器观察元素出现在视图内,出现的话触发回调
7320
- * @param target 目标元素
7441
+ * @param $el 目标元素
7321
7442
  * @param callback 触发的回调
7322
7443
  * @param options 观察器配置
7323
7444
  * @example
@@ -7325,34 +7446,47 @@ System.register('Utils', [], (function (exports) {
7325
7446
  * console.log("该元素出现在视图内");
7326
7447
  * }))
7327
7448
  */
7328
- mutationVisible(target, callback, options) {
7449
+ mutationVisible($el, callback, options) {
7329
7450
  if (typeof IntersectionObserver === "undefined") {
7330
7451
  throw new TypeError("IntersectionObserver is not defined");
7331
7452
  }
7332
- if (target == null) {
7453
+ if ($el == null) {
7333
7454
  throw new TypeError("mutatuinVisible target is null");
7334
7455
  }
7456
+ options = options || {};
7335
7457
  let defaultOptions = {
7336
7458
  root: null,
7337
7459
  rootMargin: "0px 0px 0px 0px",
7338
7460
  threshold: [0.01, 0.99],
7339
7461
  };
7340
- defaultOptions = this.assign(defaultOptions, options || {});
7462
+ defaultOptions = this.assign(defaultOptions, options);
7463
+ const handler = (entries, observer) => {
7464
+ if (options.once) {
7465
+ // 仅触发一次
7466
+ observer.disconnect();
7467
+ }
7468
+ if (typeof callback === "function") {
7469
+ callback(entries, observer);
7470
+ }
7471
+ };
7341
7472
  const intersectionObserver = new IntersectionObserver((entries, observer) => {
7342
7473
  if (entries[0].isIntersecting) {
7343
- if (typeof callback === "function") {
7344
- callback(entries, observer);
7345
- }
7474
+ handler(entries, observer);
7346
7475
  }
7347
7476
  }, defaultOptions);
7348
- if (Array.isArray(target)) {
7349
- target.forEach((item) => {
7350
- intersectionObserver.observe(item);
7477
+ if (Array.isArray($el)) {
7478
+ $el.forEach(($elItem) => {
7479
+ intersectionObserver.observe($elItem);
7351
7480
  });
7352
7481
  }
7353
7482
  else {
7354
- intersectionObserver.observe(target);
7483
+ intersectionObserver.observe($el);
7355
7484
  }
7485
+ if (options.immediate) {
7486
+ // 立即触发
7487
+ handler([], intersectionObserver);
7488
+ }
7489
+ return intersectionObserver;
7356
7490
  }
7357
7491
  /**
7358
7492
  * 去除全局window下的Utils,返回控制权
@@ -8141,12 +8275,25 @@ System.register('Utils', [], (function (exports) {
8141
8275
  }
8142
8276
  /**
8143
8277
  * 将UrlSearchParams格式的字符串转为对象
8278
+ * @param searhParamsStr 字符串或对象
8279
+ * @example
8280
+ * Utils.searchParamStrToObj("xxx=xx&xx2=xx2")
8281
+ * @example
8282
+ * Utils.searchParamStrToObj(new URLSearchParams({
8283
+ * test1: 1,
8284
+ * test2: 2
8285
+ * }))
8144
8286
  */
8145
8287
  searchParamStrToObj(searhParamsStr) {
8146
- if (typeof searhParamsStr !== "string") {
8147
- return {};
8288
+ const params = {};
8289
+ if (searhParamsStr == null) {
8290
+ return params;
8148
8291
  }
8149
- return Object.fromEntries(new URLSearchParams(searhParamsStr));
8292
+ const urlSearchParams = searhParamsStr instanceof URLSearchParams ? searhParamsStr : new URLSearchParams(searhParamsStr);
8293
+ urlSearchParams.forEach((value, key) => {
8294
+ Reflect.set(params, key, value);
8295
+ });
8296
+ return params;
8150
8297
  }
8151
8298
  /**
8152
8299
  * 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。