@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.
@@ -56,7 +56,7 @@ declare class Utils {
56
56
  * ajax劫持库,支持xhr和fetch劫持。
57
57
  * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
58
58
  * + 作者:cxxjackie
59
- * + 版本:1.4.6
59
+ * + 版本:1.4.7
60
60
  * + 旧版本:1.2.4
61
61
  * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
62
62
  * @param useOldVersion 是否使用旧版本,默认false
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.7.0",
4
+ "version": "2.7.1",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
package/src/Utils.ts CHANGED
@@ -39,7 +39,7 @@ class Utils {
39
39
  this.windowApi = new WindowApi(option);
40
40
  }
41
41
  /** 版本号 */
42
- version = "2025.6.26";
42
+ version = "2025.7.29";
43
43
  /**
44
44
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
45
45
  * @param cssText css字符串
@@ -148,7 +148,7 @@ class Utils {
148
148
  * ajax劫持库,支持xhr和fetch劫持。
149
149
  * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
150
150
  * + 作者:cxxjackie
151
- * + 版本:1.4.6
151
+ * + 版本:1.4.7
152
152
  * + 旧版本:1.2.4
153
153
  * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
154
154
  * @param useOldVersion 是否使用旧版本,默认false
@@ -1,14 +1,14 @@
1
1
  // ==UserScript==
2
2
  // @name ajaxHooker
3
3
  // @author cxxjackie
4
- // @version 1.4.6
4
+ // @version 1.4.7
5
5
  // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
6
6
  // @license GNU LGPL-3.0
7
7
  // ==/UserScript==
8
8
 
9
9
  export const ajaxHooker = function () {
10
10
  "use strict";
11
- const version = "1.4.6";
11
+ const version = "1.4.7";
12
12
  const hookInst = {
13
13
  hookFns: [],
14
14
  filters: [],
@@ -18,20 +18,18 @@ export const ajaxHooker = function () {
18
18
  const resProto = win.Response.prototype;
19
19
  const xhrResponses = ["response", "responseText", "responseXML"];
20
20
  const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
21
- const fetchInitProps = [
22
- "method",
23
- "headers",
24
- "body",
25
- "mode",
26
- "credentials",
21
+ const xhrExtraProps = ["responseType", "timeout", "withCredentials"];
22
+ const fetchExtraProps = [
27
23
  "cache",
24
+ "credentials",
25
+ "integrity",
26
+ "keepalive",
27
+ "mode",
28
+ "priority",
28
29
  "redirect",
29
30
  "referrer",
30
31
  "referrerPolicy",
31
- "integrity",
32
- "keepalive",
33
32
  "signal",
34
- "priority",
35
33
  ];
36
34
  const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
37
35
  const getType = {}.toString.call.bind({}.toString);
@@ -109,6 +107,10 @@ export const ajaxHooker = function () {
109
107
  this.request = request;
110
108
  this.requestClone = { ...this.request };
111
109
  }
110
+ _recoverRequestKey(key) {
111
+ if (key in this.requestClone) this.request[key] = this.requestClone[key];
112
+ else delete this.request[key];
113
+ }
112
114
  shouldFilter(filters) {
113
115
  const { type, url, method, async } = this.request;
114
116
  return (
@@ -129,7 +131,6 @@ export const ajaxHooker = function () {
129
131
  );
130
132
  }
131
133
  waitForRequestKeys() {
132
- const requestKeys = ["url", "method", "abort", "headers", "data"];
133
134
  if (!this.request.async) {
134
135
  win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
135
136
  if (this.shouldFilter(filters)) return;
@@ -137,27 +138,31 @@ export const ajaxHooker = function () {
137
138
  if (getType(fn) === "[object Function]")
138
139
  catchError(fn, this.request);
139
140
  });
140
- requestKeys.forEach((key) => {
141
- if (isThenable(this.request[key]))
142
- this.request[key] = this.requestClone[key];
143
- });
141
+ for (const key in this.request) {
142
+ if (isThenable(this.request[key])) this._recoverRequestKey(key);
143
+ }
144
144
  });
145
145
  return new SyncThenable();
146
146
  }
147
147
  const promises = [];
148
+ const ignoreKeys = new Set(["type", "async", "response"]);
148
149
  win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
149
150
  if (this.shouldFilter(filters)) return;
150
151
  promises.push(
151
152
  Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
152
- () =>
153
- Promise.all(
153
+ () => {
154
+ const requestKeys = [];
155
+ for (const key in this.request)
156
+ !ignoreKeys.has(key) && requestKeys.push(key);
157
+ return Promise.all(
154
158
  requestKeys.map((key) =>
155
159
  Promise.resolve(this.request[key]).then(
156
160
  (val) => (this.request[key] = val),
157
- () => (this.request[key] = this.requestClone[key])
161
+ () => this._recoverRequestKey(key)
158
162
  )
159
163
  )
160
- )
164
+ );
165
+ }
161
166
  )
162
167
  );
163
168
  });
@@ -338,6 +343,7 @@ export const ajaxHooker = function () {
338
343
  e.stopImmediatePropagation = stopImmediatePropagation;
339
344
  defineProp(e, "target", () => this.proxyXhr);
340
345
  defineProp(e, "currentTarget", () => this.proxyXhr);
346
+ defineProp(e, "srcElement", () => this.proxyXhr);
341
347
  this.proxyEvents[e.type] &&
342
348
  this.proxyEvents[e.type].forEach((fn) => {
343
349
  this.resThenable.then(
@@ -415,6 +421,9 @@ export const ajaxHooker = function () {
415
421
  for (const header in request.headers) {
416
422
  xhr.setRequestHeader(header, request.headers[header]);
417
423
  }
424
+ for (const prop of xhrExtraProps) {
425
+ if (prop in request) xhr[prop] = request[prop];
426
+ }
418
427
  xhr.send(request.data);
419
428
  }
420
429
  });
@@ -436,8 +445,10 @@ export const ajaxHooker = function () {
436
445
  return new Promise(async (resolve, reject) => {
437
446
  const init = {};
438
447
  if (getType(url) === "[object Request]") {
439
- for (const prop of fetchInitProps) init[prop] = url[prop];
448
+ init.method = url.method;
449
+ init.headers = url.headers;
440
450
  if (url.body) init.body = await url.arrayBuffer();
451
+ for (const prop of fetchExtraProps) init[prop] = url[prop];
441
452
  url = url.url;
442
453
  }
443
454
  url = url.toString();
@@ -484,6 +495,9 @@ export const ajaxHooker = function () {
484
495
  init.method = request.method;
485
496
  init.headers = request.headers;
486
497
  init.body = request.data;
498
+ for (const prop of fetchExtraProps) {
499
+ if (prop in request) init[prop] = request[prop];
500
+ }
487
501
  winAh.realFetch.call(win, request.url, init).then((res) => {
488
502
  if (typeof request.response === "function") {
489
503
  const response = {