@whitesev/utils 2.7.0 → 2.7.1

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # 安装
2
2
 
3
- + 最新版本:[![npm version](https://img.shields.io/npm/v/@whitesev/utils)](https://www.npmjs.com/package/@whitesev/utils)
3
+ - 最新版本:[![npm version](https://img.shields.io/npm/v/@whitesev/utils)](https://www.npmjs.com/package/@whitesev/utils)
4
4
 
5
5
  ```node
6
6
  npm install @whitesev/utils
@@ -37,8 +37,8 @@ Utils.assign({1:1},{1:2,2:3})
37
37
 
38
38
  ```javascript
39
39
  /* 等待a元素出现,返回Promise对象,在then中或使用await获取结果,统一返回数组格式的元素,如[...a] */
40
- Utils.waitNode("a").then((element => {
41
- console.log(element );
40
+ Utils.waitNode("a").then((element) => {
41
+ console.log(element);
42
42
  });
43
43
 
44
44
  /* 同时满足多个选择器的结果,都满足了才会触发回调 */
@@ -61,24 +61,24 @@ Utils.toJSON("{123:123}");
61
61
  let httpx = new Utils.Httpx();
62
62
  /* 修改配置 */
63
63
  httpx.config({
64
- timeout: 5000,
65
- onabort: function () {
66
- console.log("请求取消");
67
- },
68
- ontimeout: function () {
69
- console.log("请求超时");
70
- },
71
- onerror: function (response) {
72
- console.log("httpx-onerror 请求异常");
73
- console.log(response);
74
- },
64
+ timeout: 5000,
65
+ onabort: function () {
66
+ console.log("请求取消");
67
+ },
68
+ ontimeout: function () {
69
+ console.log("请求超时");
70
+ },
71
+ onerror: function (response) {
72
+ console.log("httpx-onerror 请求异常");
73
+ console.log(response);
74
+ },
75
75
  });
76
76
  /* 发送post请求 */
77
77
  let postResp = await httpx.post({
78
- url: url,
79
- data: JSON.stringify({
80
- test: 1,
81
- }),
78
+ url: url,
79
+ data: JSON.stringify({
80
+ test: 1,
81
+ }),
82
82
  });
83
83
  ```
84
84
 
@@ -173,4 +173,4 @@ menu_key:{
173
173
  GM_Menu.delete("menu_key");
174
174
  ```
175
175
 
176
- 等...API请看代码
176
+ 等...API 请看文档
package/dist/index.amd.js CHANGED
@@ -748,13 +748,13 @@ define((function () { 'use strict';
748
748
  // ==UserScript==
749
749
  // @name ajaxHooker
750
750
  // @author cxxjackie
751
- // @version 1.4.6
751
+ // @version 1.4.7
752
752
  // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
753
753
  // @license GNU LGPL-3.0
754
754
  // ==/UserScript==
755
755
 
756
756
  const ajaxHooker = function () {
757
- const version = "1.4.6";
757
+ const version = "1.4.7";
758
758
  const hookInst = {
759
759
  hookFns: [],
760
760
  filters: [],
@@ -764,20 +764,18 @@ define((function () { 'use strict';
764
764
  const resProto = win.Response.prototype;
765
765
  const xhrResponses = ["response", "responseText", "responseXML"];
766
766
  const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
767
- const fetchInitProps = [
768
- "method",
769
- "headers",
770
- "body",
771
- "mode",
772
- "credentials",
767
+ const xhrExtraProps = ["responseType", "timeout", "withCredentials"];
768
+ const fetchExtraProps = [
773
769
  "cache",
770
+ "credentials",
771
+ "integrity",
772
+ "keepalive",
773
+ "mode",
774
+ "priority",
774
775
  "redirect",
775
776
  "referrer",
776
777
  "referrerPolicy",
777
- "integrity",
778
- "keepalive",
779
778
  "signal",
780
- "priority",
781
779
  ];
782
780
  const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
783
781
  const getType = {}.toString.call.bind({}.toString);
@@ -855,6 +853,10 @@ define((function () { 'use strict';
855
853
  this.request = request;
856
854
  this.requestClone = { ...this.request };
857
855
  }
856
+ _recoverRequestKey(key) {
857
+ if (key in this.requestClone) this.request[key] = this.requestClone[key];
858
+ else delete this.request[key];
859
+ }
858
860
  shouldFilter(filters) {
859
861
  const { type, url, method, async } = this.request;
860
862
  return (
@@ -875,7 +877,6 @@ define((function () { 'use strict';
875
877
  );
876
878
  }
877
879
  waitForRequestKeys() {
878
- const requestKeys = ["url", "method", "abort", "headers", "data"];
879
880
  if (!this.request.async) {
880
881
  win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
881
882
  if (this.shouldFilter(filters)) return;
@@ -883,27 +884,31 @@ define((function () { 'use strict';
883
884
  if (getType(fn) === "[object Function]")
884
885
  catchError(fn, this.request);
885
886
  });
886
- requestKeys.forEach((key) => {
887
- if (isThenable(this.request[key]))
888
- this.request[key] = this.requestClone[key];
889
- });
887
+ for (const key in this.request) {
888
+ if (isThenable(this.request[key])) this._recoverRequestKey(key);
889
+ }
890
890
  });
891
891
  return new SyncThenable();
892
892
  }
893
893
  const promises = [];
894
+ const ignoreKeys = new Set(["type", "async", "response"]);
894
895
  win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
895
896
  if (this.shouldFilter(filters)) return;
896
897
  promises.push(
897
898
  Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
898
- () =>
899
- Promise.all(
899
+ () => {
900
+ const requestKeys = [];
901
+ for (const key in this.request)
902
+ !ignoreKeys.has(key) && requestKeys.push(key);
903
+ return Promise.all(
900
904
  requestKeys.map((key) =>
901
905
  Promise.resolve(this.request[key]).then(
902
906
  (val) => (this.request[key] = val),
903
- () => (this.request[key] = this.requestClone[key])
907
+ () => this._recoverRequestKey(key)
904
908
  )
905
909
  )
906
- )
910
+ );
911
+ }
907
912
  )
908
913
  );
909
914
  });
@@ -1084,6 +1089,7 @@ define((function () { 'use strict';
1084
1089
  e.stopImmediatePropagation = stopImmediatePropagation;
1085
1090
  defineProp(e, "target", () => this.proxyXhr);
1086
1091
  defineProp(e, "currentTarget", () => this.proxyXhr);
1092
+ defineProp(e, "srcElement", () => this.proxyXhr);
1087
1093
  this.proxyEvents[e.type] &&
1088
1094
  this.proxyEvents[e.type].forEach((fn) => {
1089
1095
  this.resThenable.then(
@@ -1161,6 +1167,9 @@ define((function () { 'use strict';
1161
1167
  for (const header in request.headers) {
1162
1168
  xhr.setRequestHeader(header, request.headers[header]);
1163
1169
  }
1170
+ for (const prop of xhrExtraProps) {
1171
+ if (prop in request) xhr[prop] = request[prop];
1172
+ }
1164
1173
  xhr.send(request.data);
1165
1174
  }
1166
1175
  });
@@ -1182,8 +1191,10 @@ define((function () { 'use strict';
1182
1191
  return new Promise(async (resolve, reject) => {
1183
1192
  const init = {};
1184
1193
  if (getType(url) === "[object Request]") {
1185
- for (const prop of fetchInitProps) init[prop] = url[prop];
1194
+ init.method = url.method;
1195
+ init.headers = url.headers;
1186
1196
  if (url.body) init.body = await url.arrayBuffer();
1197
+ for (const prop of fetchExtraProps) init[prop] = url[prop];
1187
1198
  url = url.url;
1188
1199
  }
1189
1200
  url = url.toString();
@@ -1230,6 +1241,9 @@ define((function () { 'use strict';
1230
1241
  init.method = request.method;
1231
1242
  init.headers = request.headers;
1232
1243
  init.body = request.data;
1244
+ for (const prop of fetchExtraProps) {
1245
+ if (prop in request) init[prop] = request[prop];
1246
+ }
1233
1247
  winAh.realFetch.call(win, request.url, init).then((res) => {
1234
1248
  if (typeof request.response === "function") {
1235
1249
  const response = {
@@ -5592,7 +5606,7 @@ define((function () { 'use strict';
5592
5606
  this.windowApi = new WindowApi(option);
5593
5607
  }
5594
5608
  /** 版本号 */
5595
- version = "2025.6.26";
5609
+ version = "2025.7.29";
5596
5610
  addStyle(cssText) {
5597
5611
  if (typeof cssText !== "string") {
5598
5612
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -5676,7 +5690,7 @@ define((function () { 'use strict';
5676
5690
  * ajax劫持库,支持xhr和fetch劫持。
5677
5691
  * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5678
5692
  * + 作者:cxxjackie
5679
- * + 版本:1.4.6
5693
+ * + 版本:1.4.7
5680
5694
  * + 旧版本:1.2.4
5681
5695
  * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
5682
5696
  * @param useOldVersion 是否使用旧版本,默认false