@whitesev/utils 2.11.11 → 2.11.13

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.
Files changed (39) hide show
  1. package/dist/index.amd.js +220 -62
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.amd.min.js +1 -1
  4. package/dist/index.amd.min.js.map +1 -1
  5. package/dist/index.cjs.js +220 -62
  6. package/dist/index.cjs.js.map +1 -1
  7. package/dist/index.cjs.min.js +1 -1
  8. package/dist/index.cjs.min.js.map +1 -1
  9. package/dist/index.esm.js +220 -62
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/index.esm.min.js +1 -1
  12. package/dist/index.esm.min.js.map +1 -1
  13. package/dist/index.iife.js +220 -62
  14. package/dist/index.iife.js.map +1 -1
  15. package/dist/index.iife.min.js +1 -1
  16. package/dist/index.iife.min.js.map +1 -1
  17. package/dist/index.system.js +220 -62
  18. package/dist/index.system.js.map +1 -1
  19. package/dist/index.system.min.js +1 -1
  20. package/dist/index.system.min.js.map +1 -1
  21. package/dist/index.umd.js +220 -62
  22. package/dist/index.umd.js.map +1 -1
  23. package/dist/index.umd.min.js +1 -1
  24. package/dist/index.umd.min.js.map +1 -1
  25. package/dist/types/src/Dictionary.d.ts +13 -2
  26. package/dist/types/src/Utils.d.ts +37 -11
  27. package/dist/types/src/ajaxHooker/ajaxHooker.d.ts +8 -2
  28. package/dist/types/src/ajaxHooker/ajaxHooker1.2.4.d.ts +8 -2
  29. package/dist/types/src/types/Httpx.d.ts +16 -16
  30. package/dist/types/src/types/ajaxHooker.d.ts +92 -56
  31. package/package.json +3 -4
  32. package/src/Dictionary.ts +24 -3
  33. package/src/Httpx.ts +18 -18
  34. package/src/LockFunction.ts +3 -6
  35. package/src/Utils.ts +85 -53
  36. package/src/ajaxHooker/ajaxHooker.js +55 -5
  37. package/src/ajaxHooker/ajaxHooker1.2.4.js +58 -14
  38. package/src/types/Httpx.d.ts +16 -16
  39. package/src/types/ajaxHooker.d.ts +92 -56
package/dist/index.esm.js CHANGED
@@ -237,7 +237,7 @@ const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
237
237
  const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
238
238
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
239
239
 
240
- const version = "2.11.11";
240
+ const version = "2.11.13";
241
241
 
242
242
  /* eslint-disable */
243
243
  // ==UserScript==
@@ -372,8 +372,12 @@ const ajaxHooker = function () {
372
372
  }));
373
373
  }
374
374
  waitForRequestKeys() {
375
+ /**
376
+ * @type {Set<typeof hookInst>}
377
+ */
378
+ const winHookInsts = win.__ajaxHooker.hookInsts;
375
379
  if (!this.request.async) {
376
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
380
+ winHookInsts.forEach(({ hookFns, filters }) => {
377
381
  if (this.shouldFilter(filters))
378
382
  return;
379
383
  hookFns.forEach((fn) => {
@@ -389,10 +393,10 @@ const ajaxHooker = function () {
389
393
  }
390
394
  const promises = [];
391
395
  const ignoreKeys = new Set(["type", "async", "response"]);
392
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
396
+ winHookInsts.forEach(({ hookFns, filters }) => {
393
397
  if (this.shouldFilter(filters))
394
398
  return;
395
- promises.push(Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(() => {
399
+ promises.push(Promise.all(hookFns.map((fn, index) => catchError(fn, this.request, index))).then(() => {
396
400
  const requestKeys = [];
397
401
  for (const key in this.request)
398
402
  !ignoreKeys.has(key) && requestKeys.push(key);
@@ -814,11 +818,52 @@ const ajaxHooker = function () {
814
818
  });
815
819
  });
816
820
  }
821
+ /**
822
+ *
823
+ * @type {import("./../types/ajaxHooker.d.ts").UtilsAjaxHookResult["removeHook"]}
824
+ */
825
+ const removeHook = (fn, onlyRemove = false) => {
826
+ let flag = false;
827
+ for (let index = hookInst.hookFns.length - 1; index >= 0; index--) {
828
+ const __fn__ = hookInst.hookFns[index];
829
+ if (fn === __fn__) {
830
+ hookInst.hookFns.splice(index, 1);
831
+ flag = true;
832
+ if (onlyRemove) {
833
+ break;
834
+ }
835
+ }
836
+ }
837
+ return flag;
838
+ };
839
+ /**
840
+ *
841
+ * @type {import("./../types/ajaxHooker.d.ts").UtilsAjaxHookResult["removeFilter"]}
842
+ */
843
+ const removeFilter = () => {
844
+ if (Array.isArray(hookInst.filters)) {
845
+ hookInst.filters.length = 0;
846
+ }
847
+ else {
848
+ hookInst.filters = [];
849
+ }
850
+ };
817
851
  return {
818
- hook: (fn) => hookInst.hookFns.push(fn),
852
+ hook: (fn) => {
853
+ hookInst.hookFns.push(fn);
854
+ return {
855
+ remove: () => {
856
+ return removeHook(fn, true);
857
+ }
858
+ };
859
+ },
819
860
  filter: (arr) => {
820
- if (Array.isArray(arr))
861
+ if (Array.isArray(arr)) {
821
862
  hookInst.filters = arr;
863
+ }
864
+ return {
865
+ remove: removeFilter
866
+ };
822
867
  },
823
868
  protect: () => {
824
869
  readonly(win, "XMLHttpRequest", winAh.fakeXHR);
@@ -834,6 +879,8 @@ const ajaxHooker = function () {
834
879
  delete win.__ajaxHooker;
835
880
  }
836
881
  },
882
+ removeHook: removeHook,
883
+ removeFilter: removeFilter
837
884
  };
838
885
  };
839
886
 
@@ -1232,10 +1279,50 @@ const AjaxHooker1_2_4 = function () {
1232
1279
  Object.keys(realXhr).forEach((key) => (fakeXhr[key] = realXhr[key]));
1233
1280
  fakeXhr.prototype = realXhr.prototype;
1234
1281
  win.fetch = fakeFetch;
1282
+ /**
1283
+ *
1284
+ * @type {import("./../types/ajaxHooker.d.ts").UtilsAjaxHookResult["removeHook"]}
1285
+ */
1286
+ const removeHook = (fn, onlyRemove = false) => {
1287
+ let flag = false;
1288
+ for (let index = hookFns.length - 1; index >= 0; index--) {
1289
+ const __fn__ = hookFns[index];
1290
+ if (fn === __fn__) {
1291
+ hookFns.splice(index, 1);
1292
+ flag = true;
1293
+ if (onlyRemove) {
1294
+ break;
1295
+ }
1296
+ }
1297
+ }
1298
+ return flag;
1299
+ };
1300
+ /**
1301
+ *
1302
+ * @type {import("./../types/ajaxHooker.d.ts").UtilsAjaxHookResult["removeFilter"]}
1303
+ */
1304
+ const removeFilter = () => {
1305
+ if (Array.isArray(filter)) {
1306
+ filter.length = 0;
1307
+ }
1308
+ else {
1309
+ filter = void 0;
1310
+ }
1311
+ };
1235
1312
  return {
1236
- hook: (fn) => hookFns.push(fn),
1313
+ hook: (fn) => {
1314
+ hookFns.push(fn);
1315
+ return {
1316
+ remove: () => {
1317
+ return removeHook(fn, true);
1318
+ }
1319
+ };
1320
+ },
1237
1321
  filter: (arr) => {
1238
1322
  filter = Array.isArray(arr) && arr.map(toFilterObj);
1323
+ return {
1324
+ remove: removeFilter
1325
+ };
1239
1326
  },
1240
1327
  protect: () => {
1241
1328
  readonly(win, "XMLHttpRequest", fakeXhr);
@@ -1245,6 +1332,8 @@ const AjaxHooker1_2_4 = function () {
1245
1332
  writable(win, "XMLHttpRequest", realXhr);
1246
1333
  writable(win, "fetch", realFetch);
1247
1334
  },
1335
+ removeHook: removeHook,
1336
+ removeFilter: removeFilter
1248
1337
  };
1249
1338
  })();
1250
1339
  };
@@ -1829,13 +1918,25 @@ class UtilsDictionary {
1829
1918
  }
1830
1919
  /**
1831
1920
  * 迭代字典
1832
- * @param callbackfn 回调函数
1921
+ * @param cb 回调函数
1833
1922
  */
1834
- forEach(callbackfn) {
1923
+ forEach(cb) {
1835
1924
  this.items.forEach((value, key) => {
1836
- callbackfn(value, key, this);
1925
+ cb(value, key, this);
1837
1926
  });
1838
1927
  }
1928
+ /**
1929
+ * 找到字典中对应的键和值
1930
+ * @param cb 回调函数
1931
+ */
1932
+ find(cb) {
1933
+ for (const [key, value] of this.items.entries()) {
1934
+ const result = cb(value, key, this);
1935
+ if (result) {
1936
+ return result;
1937
+ }
1938
+ }
1939
+ }
1839
1940
  /**
1840
1941
  * 检查已有的键中是否以xx开头
1841
1942
  * @param key 需要匹配的键
@@ -2669,42 +2770,42 @@ class Httpx {
2669
2770
  const method = requestOption.method;
2670
2771
  if (method === "GET" || method === "HEAD") {
2671
2772
  // GET类型,data如果有,那么需要转为searchParams
2672
- const urlObj = new URL(requestOption.url);
2773
+ const urlInst = new URL(requestOption.url);
2673
2774
  let urlSearch = "";
2674
- let isHandler = false;
2775
+ let deleteData = false;
2675
2776
  if (typeof requestOption.data === "string") {
2676
- isHandler = true;
2777
+ deleteData = true;
2677
2778
  urlSearch = requestOption.data;
2678
2779
  }
2679
2780
  else if (typeof requestOption.data === "object") {
2680
- isHandler = true;
2781
+ deleteData = true;
2681
2782
  // URLSearchParams参数可以转普通的string:string,包括FormData
2682
2783
  const searchParams = new URLSearchParams(requestOption.data);
2683
2784
  urlSearch = searchParams.toString();
2684
2785
  }
2685
- if (isHandler) {
2786
+ if (deleteData) {
2686
2787
  // GET/HEAD请求不支持data参数
2687
2788
  // 对data进行处理了才可以删除
2688
2789
  Reflect.deleteProperty(requestOption, "data");
2689
2790
  }
2690
- if (urlSearch != "") {
2691
- if (urlObj.search === "") {
2791
+ if (urlSearch.trim() != "") {
2792
+ if (urlInst.search.trim() === "") {
2692
2793
  // url没有search参数,直接覆盖
2693
- urlObj.search = urlSearch;
2794
+ urlInst.search = urlSearch;
2694
2795
  }
2695
2796
  else {
2696
2797
  // 有search参数
2697
- if (urlObj.search.endsWith("&")) {
2798
+ if (urlInst.search.trim().endsWith("&")) {
2698
2799
  // xxx=xxx&
2699
- urlObj.search = urlObj.search + urlSearch;
2800
+ urlInst.search = urlInst.search + urlSearch;
2700
2801
  }
2701
2802
  else {
2702
2803
  // xxx=xxx&xxx=
2703
- urlObj.search = `${urlObj.search}&${urlSearch}`;
2804
+ urlInst.search = `${urlInst.search}&${urlSearch}`;
2704
2805
  }
2705
2806
  }
2706
2807
  }
2707
- requestOption.url = urlObj.toString();
2808
+ requestOption.url = urlInst.toString();
2708
2809
  }
2709
2810
  else if (method === "POST" && requestOption.headers != null) {
2710
2811
  // POST类型,data如果是FormData,那么需要转为string
@@ -2714,10 +2815,10 @@ class Httpx {
2714
2815
  typeof requestOption.headers[headerKey] === "string");
2715
2816
  });
2716
2817
  if (ContentTypeIndex !== -1) {
2717
- const ContentTypeKey = headersKeyList[ContentTypeIndex];
2718
- const ContentType = requestOption.headers[ContentTypeKey];
2818
+ const contentTypeKey = headersKeyList[ContentTypeIndex];
2819
+ const contentType = requestOption.headers[contentTypeKey].toLowerCase();
2719
2820
  // 设置了Content-Type
2720
- if (ContentType.includes("application/json")) {
2821
+ if (contentType.includes("application/json")) {
2721
2822
  // application/json
2722
2823
  if (requestOption.data instanceof FormData) {
2723
2824
  const entries = {};
@@ -2730,16 +2831,16 @@ class Httpx {
2730
2831
  requestOption.data = JSON.stringify(requestOption.data);
2731
2832
  }
2732
2833
  }
2733
- else if (ContentType.includes("application/x-www-form-urlencoded")) {
2834
+ else if (contentType.includes("application/x-www-form-urlencoded")) {
2734
2835
  // application/x-www-form-urlencoded
2735
2836
  if (typeof requestOption.data === "object") {
2736
2837
  requestOption.data = new URLSearchParams(requestOption.data).toString();
2737
2838
  }
2738
2839
  }
2739
- else if (ContentType.includes("multipart/form-data")) {
2840
+ else if (contentType.includes("multipart/form-data")) {
2740
2841
  // multipart/form-data
2741
2842
  if (requestOption.data instanceof FormData) {
2742
- Reflect.deleteProperty(requestOption.headers, ContentTypeKey);
2843
+ Reflect.deleteProperty(requestOption.headers, contentTypeKey);
2743
2844
  }
2744
2845
  }
2745
2846
  }
@@ -5697,20 +5798,25 @@ class Utils {
5697
5798
  }
5698
5799
  /**
5699
5800
  * ajax劫持库,支持xhr和fetch劫持。
5700
- * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5701
- * + 作者:cxxjackie
5702
- * + 版本:1.4.8
5703
- * + 旧版本:1.2.4
5704
- * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
5705
- * @param useOldVersion 是否使用旧版本,默认false
5706
- */
5707
- ajaxHooker = (useOldVersion = false) => {
5708
- if (useOldVersion) {
5709
- return AjaxHooker1_2_4();
5710
- }
5711
- else {
5712
- return ajaxHooker();
5713
- }
5801
+ * + 来源: https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5802
+ * + 作者: cxxjackie
5803
+ * + 实现方式: Proxy
5804
+ * + 版本: `1.4.8`
5805
+ * + 文档: https://scriptcat.org/zh-CN/script-show-page/637/
5806
+ */
5807
+ ajaxHooker = () => {
5808
+ return ajaxHooker();
5809
+ };
5810
+ /**
5811
+ * ajax劫持库,支持xhr和fetch劫持。
5812
+ * + 来源: https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5813
+ * + 作者: cxxjackie
5814
+ * + 实现方式: Object.defineProperty
5815
+ * + 版本: `1.2.4`
5816
+ * + 文档: https://scriptcat.org/zh-CN/script-show-page/637/
5817
+ */
5818
+ oldAjaxHooker = () => {
5819
+ return AjaxHooker1_2_4();
5714
5820
  };
5715
5821
  canvasClickByPosition(canvasElement, clientX = 0, clientY = 0, view = this.windowApi.window) {
5716
5822
  if (!(canvasElement instanceof HTMLCanvasElement)) {
@@ -7235,6 +7341,26 @@ class Utils {
7235
7341
  }
7236
7342
  return content;
7237
7343
  }
7344
+ /**
7345
+ * 监听页面元素改变并处理
7346
+ * @param target 需要监听的元素,如果不存在,可以等待它出现
7347
+ * @param observer_config MutationObserver的配置
7348
+ * @example
7349
+ Utils.mutationObserver(document.querySelector("div.xxxx"),{
7350
+ "callback":(mutations, observer)=>{},
7351
+ "config":{childList:true,attributes:true}
7352
+ });
7353
+ * @example
7354
+ Utils.mutationObserver(document.querySelectorAll("div.xxxx"),{
7355
+ "callback":(mutations, observer)=>{},
7356
+ "config":{childList:true,attributes:true}}
7357
+ );
7358
+ * @example
7359
+ Utils.mutationObserver($("div.xxxx"),{
7360
+ "callback":(mutations, observer)=>{},
7361
+ "config":{childList:true,attributes:true}}
7362
+ );
7363
+ **/
7238
7364
  mutationObserver(target, observer_config) {
7239
7365
  const that = this;
7240
7366
  const default_obverser_config = {
@@ -7277,16 +7403,24 @@ class Utils {
7277
7403
  characterDataOldValue: void 0,
7278
7404
  },
7279
7405
  immediate: false,
7406
+ once: false,
7280
7407
  };
7281
7408
  observer_config = that.assign(default_obverser_config, observer_config);
7282
7409
  const windowMutationObserver = this.windowApi.window.MutationObserver ||
7283
7410
  this.windowApi.window.webkitMutationObserver ||
7284
7411
  this.windowApi.window.MozMutationObserver;
7285
7412
  // 观察者对象
7286
- const mutationObserver = new windowMutationObserver(function (mutations, observer) {
7413
+ const handler = (mutations, observer) => {
7414
+ if (observer_config.once) {
7415
+ // 仅触发一次
7416
+ observer.disconnect();
7417
+ }
7287
7418
  if (typeof observer_config.callback === "function") {
7288
7419
  observer_config.callback(mutations, observer);
7289
7420
  }
7421
+ };
7422
+ const mutationObserver = new windowMutationObserver(function (mutations, observer) {
7423
+ handler(mutations, observer);
7290
7424
  });
7291
7425
  if (Array.isArray(target) || target instanceof NodeList) {
7292
7426
  // 传入的是数组或者元素数组
@@ -7295,7 +7429,7 @@ class Utils {
7295
7429
  });
7296
7430
  }
7297
7431
  else if (that.isJQuery(target)) {
7298
- /* 传入的参数是jQuery对象 */
7432
+ // 传入的参数是jQuery对象
7299
7433
  target.each((_, item) => {
7300
7434
  mutationObserver.observe(item, observer_config.config);
7301
7435
  });
@@ -7304,16 +7438,14 @@ class Utils {
7304
7438
  mutationObserver.observe(target, observer_config.config);
7305
7439
  }
7306
7440
  if (observer_config.immediate) {
7307
- /* 主动触发一次 */
7308
- if (typeof observer_config.callback === "function") {
7309
- observer_config.callback([], mutationObserver);
7310
- }
7441
+ // 主动触发一次
7442
+ handler([], mutationObserver);
7311
7443
  }
7312
7444
  return mutationObserver;
7313
7445
  }
7314
7446
  /**
7315
7447
  * 使用观察器观察元素出现在视图内,出现的话触发回调
7316
- * @param target 目标元素
7448
+ * @param $el 目标元素
7317
7449
  * @param callback 触发的回调
7318
7450
  * @param options 观察器配置
7319
7451
  * @example
@@ -7321,34 +7453,47 @@ class Utils {
7321
7453
  * console.log("该元素出现在视图内");
7322
7454
  * }))
7323
7455
  */
7324
- mutationVisible(target, callback, options) {
7456
+ mutationVisible($el, callback, options) {
7325
7457
  if (typeof IntersectionObserver === "undefined") {
7326
7458
  throw new TypeError("IntersectionObserver is not defined");
7327
7459
  }
7328
- if (target == null) {
7460
+ if ($el == null) {
7329
7461
  throw new TypeError("mutatuinVisible target is null");
7330
7462
  }
7463
+ options = options || {};
7331
7464
  let defaultOptions = {
7332
7465
  root: null,
7333
7466
  rootMargin: "0px 0px 0px 0px",
7334
7467
  threshold: [0.01, 0.99],
7335
7468
  };
7336
- defaultOptions = this.assign(defaultOptions, options || {});
7469
+ defaultOptions = this.assign(defaultOptions, options);
7470
+ const handler = (entries, observer) => {
7471
+ if (options.once) {
7472
+ // 仅触发一次
7473
+ observer.disconnect();
7474
+ }
7475
+ if (typeof callback === "function") {
7476
+ callback(entries, observer);
7477
+ }
7478
+ };
7337
7479
  const intersectionObserver = new IntersectionObserver((entries, observer) => {
7338
7480
  if (entries[0].isIntersecting) {
7339
- if (typeof callback === "function") {
7340
- callback(entries, observer);
7341
- }
7481
+ handler(entries, observer);
7342
7482
  }
7343
7483
  }, defaultOptions);
7344
- if (Array.isArray(target)) {
7345
- target.forEach((item) => {
7346
- intersectionObserver.observe(item);
7484
+ if (Array.isArray($el)) {
7485
+ $el.forEach(($elItem) => {
7486
+ intersectionObserver.observe($elItem);
7347
7487
  });
7348
7488
  }
7349
7489
  else {
7350
- intersectionObserver.observe(target);
7490
+ intersectionObserver.observe($el);
7351
7491
  }
7492
+ if (options.immediate) {
7493
+ // 立即触发
7494
+ handler([], intersectionObserver);
7495
+ }
7496
+ return intersectionObserver;
7352
7497
  }
7353
7498
  /**
7354
7499
  * 去除全局window下的Utils,返回控制权
@@ -8137,12 +8282,25 @@ class Utils {
8137
8282
  }
8138
8283
  /**
8139
8284
  * 将UrlSearchParams格式的字符串转为对象
8285
+ * @param searhParamsStr 字符串或对象
8286
+ * @example
8287
+ * Utils.searchParamStrToObj("xxx=xx&xx2=xx2")
8288
+ * @example
8289
+ * Utils.searchParamStrToObj(new URLSearchParams({
8290
+ * test1: 1,
8291
+ * test2: 2
8292
+ * }))
8140
8293
  */
8141
8294
  searchParamStrToObj(searhParamsStr) {
8142
- if (typeof searhParamsStr !== "string") {
8143
- return {};
8295
+ const params = {};
8296
+ if (searhParamsStr == null) {
8297
+ return params;
8144
8298
  }
8145
- return Object.fromEntries(new URLSearchParams(searhParamsStr));
8299
+ const urlSearchParams = searhParamsStr instanceof URLSearchParams ? searhParamsStr : new URLSearchParams(searhParamsStr);
8300
+ urlSearchParams.forEach((value, key) => {
8301
+ Reflect.set(params, key, value);
8302
+ });
8303
+ return params;
8146
8304
  }
8147
8305
  /**
8148
8306
  * 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。