@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.esm.js CHANGED
@@ -746,13 +746,13 @@ class UtilsGMCookie {
746
746
  // ==UserScript==
747
747
  // @name ajaxHooker
748
748
  // @author cxxjackie
749
- // @version 1.4.6
749
+ // @version 1.4.7
750
750
  // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
751
751
  // @license GNU LGPL-3.0
752
752
  // ==/UserScript==
753
753
 
754
754
  const ajaxHooker = function () {
755
- const version = "1.4.6";
755
+ const version = "1.4.7";
756
756
  const hookInst = {
757
757
  hookFns: [],
758
758
  filters: [],
@@ -762,20 +762,18 @@ const ajaxHooker = function () {
762
762
  const resProto = win.Response.prototype;
763
763
  const xhrResponses = ["response", "responseText", "responseXML"];
764
764
  const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
765
- const fetchInitProps = [
766
- "method",
767
- "headers",
768
- "body",
769
- "mode",
770
- "credentials",
765
+ const xhrExtraProps = ["responseType", "timeout", "withCredentials"];
766
+ const fetchExtraProps = [
771
767
  "cache",
768
+ "credentials",
769
+ "integrity",
770
+ "keepalive",
771
+ "mode",
772
+ "priority",
772
773
  "redirect",
773
774
  "referrer",
774
775
  "referrerPolicy",
775
- "integrity",
776
- "keepalive",
777
776
  "signal",
778
- "priority",
779
777
  ];
780
778
  const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
781
779
  const getType = {}.toString.call.bind({}.toString);
@@ -853,6 +851,10 @@ const ajaxHooker = function () {
853
851
  this.request = request;
854
852
  this.requestClone = { ...this.request };
855
853
  }
854
+ _recoverRequestKey(key) {
855
+ if (key in this.requestClone) this.request[key] = this.requestClone[key];
856
+ else delete this.request[key];
857
+ }
856
858
  shouldFilter(filters) {
857
859
  const { type, url, method, async } = this.request;
858
860
  return (
@@ -873,7 +875,6 @@ const ajaxHooker = function () {
873
875
  );
874
876
  }
875
877
  waitForRequestKeys() {
876
- const requestKeys = ["url", "method", "abort", "headers", "data"];
877
878
  if (!this.request.async) {
878
879
  win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
879
880
  if (this.shouldFilter(filters)) return;
@@ -881,27 +882,31 @@ const ajaxHooker = function () {
881
882
  if (getType(fn) === "[object Function]")
882
883
  catchError(fn, this.request);
883
884
  });
884
- requestKeys.forEach((key) => {
885
- if (isThenable(this.request[key]))
886
- this.request[key] = this.requestClone[key];
887
- });
885
+ for (const key in this.request) {
886
+ if (isThenable(this.request[key])) this._recoverRequestKey(key);
887
+ }
888
888
  });
889
889
  return new SyncThenable();
890
890
  }
891
891
  const promises = [];
892
+ const ignoreKeys = new Set(["type", "async", "response"]);
892
893
  win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
893
894
  if (this.shouldFilter(filters)) return;
894
895
  promises.push(
895
896
  Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
896
- () =>
897
- Promise.all(
897
+ () => {
898
+ const requestKeys = [];
899
+ for (const key in this.request)
900
+ !ignoreKeys.has(key) && requestKeys.push(key);
901
+ return Promise.all(
898
902
  requestKeys.map((key) =>
899
903
  Promise.resolve(this.request[key]).then(
900
904
  (val) => (this.request[key] = val),
901
- () => (this.request[key] = this.requestClone[key])
905
+ () => this._recoverRequestKey(key)
902
906
  )
903
907
  )
904
- )
908
+ );
909
+ }
905
910
  )
906
911
  );
907
912
  });
@@ -1082,6 +1087,7 @@ const ajaxHooker = function () {
1082
1087
  e.stopImmediatePropagation = stopImmediatePropagation;
1083
1088
  defineProp(e, "target", () => this.proxyXhr);
1084
1089
  defineProp(e, "currentTarget", () => this.proxyXhr);
1090
+ defineProp(e, "srcElement", () => this.proxyXhr);
1085
1091
  this.proxyEvents[e.type] &&
1086
1092
  this.proxyEvents[e.type].forEach((fn) => {
1087
1093
  this.resThenable.then(
@@ -1159,6 +1165,9 @@ const ajaxHooker = function () {
1159
1165
  for (const header in request.headers) {
1160
1166
  xhr.setRequestHeader(header, request.headers[header]);
1161
1167
  }
1168
+ for (const prop of xhrExtraProps) {
1169
+ if (prop in request) xhr[prop] = request[prop];
1170
+ }
1162
1171
  xhr.send(request.data);
1163
1172
  }
1164
1173
  });
@@ -1180,8 +1189,10 @@ const ajaxHooker = function () {
1180
1189
  return new Promise(async (resolve, reject) => {
1181
1190
  const init = {};
1182
1191
  if (getType(url) === "[object Request]") {
1183
- for (const prop of fetchInitProps) init[prop] = url[prop];
1192
+ init.method = url.method;
1193
+ init.headers = url.headers;
1184
1194
  if (url.body) init.body = await url.arrayBuffer();
1195
+ for (const prop of fetchExtraProps) init[prop] = url[prop];
1185
1196
  url = url.url;
1186
1197
  }
1187
1198
  url = url.toString();
@@ -1228,6 +1239,9 @@ const ajaxHooker = function () {
1228
1239
  init.method = request.method;
1229
1240
  init.headers = request.headers;
1230
1241
  init.body = request.data;
1242
+ for (const prop of fetchExtraProps) {
1243
+ if (prop in request) init[prop] = request[prop];
1244
+ }
1231
1245
  winAh.realFetch.call(win, request.url, init).then((res) => {
1232
1246
  if (typeof request.response === "function") {
1233
1247
  const response = {
@@ -5590,7 +5604,7 @@ class Utils {
5590
5604
  this.windowApi = new WindowApi(option);
5591
5605
  }
5592
5606
  /** 版本号 */
5593
- version = "2025.6.26";
5607
+ version = "2025.7.29";
5594
5608
  addStyle(cssText) {
5595
5609
  if (typeof cssText !== "string") {
5596
5610
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -5674,7 +5688,7 @@ class Utils {
5674
5688
  * ajax劫持库,支持xhr和fetch劫持。
5675
5689
  * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5676
5690
  * + 作者:cxxjackie
5677
- * + 版本:1.4.6
5691
+ * + 版本:1.4.7
5678
5692
  * + 旧版本:1.2.4
5679
5693
  * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
5680
5694
  * @param useOldVersion 是否使用旧版本,默认false