@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/dist/index.umd.js CHANGED
@@ -752,13 +752,13 @@
752
752
  // ==UserScript==
753
753
  // @name ajaxHooker
754
754
  // @author cxxjackie
755
- // @version 1.4.6
755
+ // @version 1.4.7
756
756
  // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
757
757
  // @license GNU LGPL-3.0
758
758
  // ==/UserScript==
759
759
 
760
760
  const ajaxHooker = function () {
761
- const version = "1.4.6";
761
+ const version = "1.4.7";
762
762
  const hookInst = {
763
763
  hookFns: [],
764
764
  filters: [],
@@ -768,20 +768,18 @@
768
768
  const resProto = win.Response.prototype;
769
769
  const xhrResponses = ["response", "responseText", "responseXML"];
770
770
  const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
771
- const fetchInitProps = [
772
- "method",
773
- "headers",
774
- "body",
775
- "mode",
776
- "credentials",
771
+ const xhrExtraProps = ["responseType", "timeout", "withCredentials"];
772
+ const fetchExtraProps = [
777
773
  "cache",
774
+ "credentials",
775
+ "integrity",
776
+ "keepalive",
777
+ "mode",
778
+ "priority",
778
779
  "redirect",
779
780
  "referrer",
780
781
  "referrerPolicy",
781
- "integrity",
782
- "keepalive",
783
782
  "signal",
784
- "priority",
785
783
  ];
786
784
  const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
787
785
  const getType = {}.toString.call.bind({}.toString);
@@ -859,6 +857,10 @@
859
857
  this.request = request;
860
858
  this.requestClone = { ...this.request };
861
859
  }
860
+ _recoverRequestKey(key) {
861
+ if (key in this.requestClone) this.request[key] = this.requestClone[key];
862
+ else delete this.request[key];
863
+ }
862
864
  shouldFilter(filters) {
863
865
  const { type, url, method, async } = this.request;
864
866
  return (
@@ -879,7 +881,6 @@
879
881
  );
880
882
  }
881
883
  waitForRequestKeys() {
882
- const requestKeys = ["url", "method", "abort", "headers", "data"];
883
884
  if (!this.request.async) {
884
885
  win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
885
886
  if (this.shouldFilter(filters)) return;
@@ -887,27 +888,31 @@
887
888
  if (getType(fn) === "[object Function]")
888
889
  catchError(fn, this.request);
889
890
  });
890
- requestKeys.forEach((key) => {
891
- if (isThenable(this.request[key]))
892
- this.request[key] = this.requestClone[key];
893
- });
891
+ for (const key in this.request) {
892
+ if (isThenable(this.request[key])) this._recoverRequestKey(key);
893
+ }
894
894
  });
895
895
  return new SyncThenable();
896
896
  }
897
897
  const promises = [];
898
+ const ignoreKeys = new Set(["type", "async", "response"]);
898
899
  win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
899
900
  if (this.shouldFilter(filters)) return;
900
901
  promises.push(
901
902
  Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
902
- () =>
903
- Promise.all(
903
+ () => {
904
+ const requestKeys = [];
905
+ for (const key in this.request)
906
+ !ignoreKeys.has(key) && requestKeys.push(key);
907
+ return Promise.all(
904
908
  requestKeys.map((key) =>
905
909
  Promise.resolve(this.request[key]).then(
906
910
  (val) => (this.request[key] = val),
907
- () => (this.request[key] = this.requestClone[key])
911
+ () => this._recoverRequestKey(key)
908
912
  )
909
913
  )
910
- )
914
+ );
915
+ }
911
916
  )
912
917
  );
913
918
  });
@@ -1088,6 +1093,7 @@
1088
1093
  e.stopImmediatePropagation = stopImmediatePropagation;
1089
1094
  defineProp(e, "target", () => this.proxyXhr);
1090
1095
  defineProp(e, "currentTarget", () => this.proxyXhr);
1096
+ defineProp(e, "srcElement", () => this.proxyXhr);
1091
1097
  this.proxyEvents[e.type] &&
1092
1098
  this.proxyEvents[e.type].forEach((fn) => {
1093
1099
  this.resThenable.then(
@@ -1165,6 +1171,9 @@
1165
1171
  for (const header in request.headers) {
1166
1172
  xhr.setRequestHeader(header, request.headers[header]);
1167
1173
  }
1174
+ for (const prop of xhrExtraProps) {
1175
+ if (prop in request) xhr[prop] = request[prop];
1176
+ }
1168
1177
  xhr.send(request.data);
1169
1178
  }
1170
1179
  });
@@ -1186,8 +1195,10 @@
1186
1195
  return new Promise(async (resolve, reject) => {
1187
1196
  const init = {};
1188
1197
  if (getType(url) === "[object Request]") {
1189
- for (const prop of fetchInitProps) init[prop] = url[prop];
1198
+ init.method = url.method;
1199
+ init.headers = url.headers;
1190
1200
  if (url.body) init.body = await url.arrayBuffer();
1201
+ for (const prop of fetchExtraProps) init[prop] = url[prop];
1191
1202
  url = url.url;
1192
1203
  }
1193
1204
  url = url.toString();
@@ -1234,6 +1245,9 @@
1234
1245
  init.method = request.method;
1235
1246
  init.headers = request.headers;
1236
1247
  init.body = request.data;
1248
+ for (const prop of fetchExtraProps) {
1249
+ if (prop in request) init[prop] = request[prop];
1250
+ }
1237
1251
  winAh.realFetch.call(win, request.url, init).then((res) => {
1238
1252
  if (typeof request.response === "function") {
1239
1253
  const response = {
@@ -5596,7 +5610,7 @@
5596
5610
  this.windowApi = new WindowApi(option);
5597
5611
  }
5598
5612
  /** 版本号 */
5599
- version = "2025.6.26";
5613
+ version = "2025.7.29";
5600
5614
  addStyle(cssText) {
5601
5615
  if (typeof cssText !== "string") {
5602
5616
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -5680,7 +5694,7 @@
5680
5694
  * ajax劫持库,支持xhr和fetch劫持。
5681
5695
  * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5682
5696
  * + 作者:cxxjackie
5683
- * + 版本:1.4.6
5697
+ * + 版本:1.4.7
5684
5698
  * + 旧版本:1.2.4
5685
5699
  * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
5686
5700
  * @param useOldVersion 是否使用旧版本,默认false