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