@whitesev/utils 2.9.8 → 2.9.10

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.
@@ -1,4 +1,4 @@
1
- import type { HttpxHookErrorData, HttpxRequestOption, HttpxResponse, HttpxResponseData, HttpxPromise, HttpxInitOption } from "./types/Httpx";
1
+ import type { HttpxHookErrorData, HttpxInitOption, HttpxPromise, HttpxRequestOption, HttpxResponse, HttpxResponseData } from "./types/Httpx";
2
2
  export declare class Httpx {
3
3
  #private;
4
4
  private GM_Api;
@@ -24,8 +24,7 @@ export declare class Log {
24
24
  /**
25
25
  * 输出内容
26
26
  * @param msg 需要输出的内容
27
- * @param color 颜色
28
- * @param otherStyle 其它CSS
27
+ * @param tagColor tag颜色
29
28
  */
30
29
  private printContent;
31
30
  /**
@@ -1,20 +1,20 @@
1
1
  import { ColorConversion } from "./ColorConversion";
2
+ import { UtilsDictionary } from "./Dictionary";
2
3
  import { GBKEncoder } from "./GBKEncoder";
3
- import { UtilsGMCookie } from "./UtilsGMCookie";
4
- import { GMMenu } from "./UtilsGMMenu";
5
4
  import { Hooks } from "./Hooks";
6
5
  import { Httpx } from "./Httpx";
7
6
  import { indexedDB } from "./indexedDB";
8
7
  import { LockFunction } from "./LockFunction";
9
8
  import { Log } from "./Log";
9
+ import { ModuleRaid } from "./ModuleRaid";
10
10
  import { Progress } from "./Progress";
11
- import { UtilsDictionary } from "./Dictionary";
12
11
  import type { UtilsAjaxHookResult } from "./types/ajaxHooker";
13
- import { Vue } from "./Vue";
14
12
  import { type ArgsType, type JSTypeNames, type UtilsOwnObject } from "./types/global";
15
- import type { WindowApiOption } from "./types/WindowApi";
16
- import { ModuleRaid } from "./ModuleRaid";
17
13
  import type { ReactInstance } from "./types/React";
14
+ import type { WindowApiOption } from "./types/WindowApi";
15
+ import { UtilsGMCookie } from "./UtilsGMCookie";
16
+ import { GMMenu } from "./UtilsGMMenu";
17
+ import { Vue } from "./Vue";
18
18
  declare class Utils {
19
19
  private windowApi;
20
20
  constructor(option?: WindowApiOption);
@@ -616,43 +616,43 @@ declare class Utils {
616
616
  **/
617
617
  Hooks: typeof Hooks;
618
618
  /**
619
- * 为减少代码量和回调,把GM_xmlhttpRequest封装
620
- * 文档地址: https://www.tampermonkey.net/documentation.php?ext=iikm
621
- * 其中onloadstart、onprogress、onreadystatechange是回调形式,onabort、ontimeout、onerror可以设置全局回调函数
622
- * @param _GM_xmlHttpRequest_ 油猴中的GM_xmlhttpRequest
623
- * @example
624
- let httpx = new Utils.Httpx(GM_xmlhttpRequest);
625
- let postResp = await httpx.post({
626
- url:url,
627
- data:JSON.stringify({
628
- test:1
629
- }),
630
- timeout: 5000
631
- });
632
- console.log(postResp);
633
- > {
634
- status: true,
635
- data: {responseText: "...", response: xxx,...},
636
- msg: "请求完毕",
637
- type: "onload",
638
- }
639
-
640
- if(postResp === "onload" && postResp.status){
641
- // onload
642
- }else if(postResp === "ontimeout"){
643
- // ontimeout
644
- }
645
- * @example
646
- // 也可以先配置全局参数
647
- let httpx = new Utils.Httpx(GM_xmlhttpRequest);
648
- httpx.config({
649
- timeout: 5000,
650
- async: false,
651
- responseType: "html",
652
- redirect: "follow",
653
- })
654
- // 优先级为 默认details < 全局details < 单独的details
655
- */
619
+ * 为减少代码量和回调,把GM_xmlhttpRequest封装
620
+ * 文档地址: https://www.tampermonkey.net/documentation.php?ext=iikm
621
+ * 其中onloadstart、onprogress、onreadystatechange是回调形式,onabort、ontimeout、onerror可以设置全局回调函数
622
+ * @param _GM_xmlHttpRequest_ 油猴中的GM_xmlhttpRequest
623
+ * @example
624
+ let httpx = new Utils.Httpx(GM_xmlhttpRequest);
625
+ let postResp = await httpx.post({
626
+ url:url,
627
+ data:JSON.stringify({
628
+ test:1
629
+ }),
630
+ timeout: 5000
631
+ });
632
+ console.log(postResp);
633
+ > {
634
+ status: true,
635
+ data: {responseText: "...", response: xxx,...},
636
+ msg: "请求完毕",
637
+ type: "onload",
638
+ }
639
+
640
+ if(postResp === "onload" && postResp.status){
641
+ // onload
642
+ }else if(postResp === "ontimeout"){
643
+ // ontimeout
644
+ }
645
+ * @example
646
+ // 也可以先配置全局参数
647
+ let httpx = new Utils.Httpx(GM_xmlhttpRequest);
648
+ httpx.config({
649
+ timeout: 5000,
650
+ async: false,
651
+ responseType: "html",
652
+ redirect: "follow",
653
+ })
654
+ // 优先级为 默认details < 全局details < 单独的details
655
+ */
656
656
  Httpx: typeof Httpx;
657
657
  /**
658
658
  * 浏览器端的indexedDB操作封装
@@ -1295,7 +1295,7 @@ declare class Utils {
1295
1295
  run<A extends any[], R>(callback: ((...args: A) => R) | string | ((...args: any[]) => any), __context__?: any): import("./types/TryCatch").UtilsTryCatchType;
1296
1296
  };
1297
1297
  /**
1298
- * 数组去重,去除重复的值
1298
+ * 数组去重,去除重复的值,返回新的数组(不修改原数组)
1299
1299
  * @param uniqueArrayData 需要去重的数组
1300
1300
  * @param compareArrayData 用来比较的数组
1301
1301
  * @param compareFun 数组比较方法,如果值相同,去除该数据
@@ -89,7 +89,9 @@ export declare interface UtilsAjaxHookFilterOptions {
89
89
  /** Utils.ajaxHooker */
90
90
  export declare interface UtilsAjaxHookResult {
91
91
  /**
92
- * 劫持
92
+ * 劫持xhr和fetch
93
+ *
94
+ * 提示:如果多次调用.hook方法,将会多次处理同一个请求
93
95
  * @example
94
96
  ajaxHooker.hook(request => {
95
97
  if (request.url === 'https://www.example.com/') {
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.9.8",
4
+ "version": "2.9.10",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
@@ -36,27 +36,29 @@
36
36
  "author": "WhiteSev",
37
37
  "license": "MIT",
38
38
  "dependencies": {
39
- "worker-timers": "^8.0.25"
39
+ "worker-timers": "^8.0.26"
40
40
  },
41
41
  "devDependencies": {
42
- "@eslint/js": "^9.39.1",
42
+ "@eslint/js": "^9.39.2",
43
43
  "@rollup/plugin-commonjs": "^29.0.0",
44
44
  "@rollup/plugin-json": "^6.1.0",
45
45
  "@rollup/plugin-node-resolve": "^16.0.3",
46
46
  "@rollup/plugin-terser": "^0.4.4",
47
47
  "@rollup/plugin-typescript": "^12.3.0",
48
- "browserslist": "^4.28.0",
49
- "caniuse-lite": "^1.0.30001756",
50
- "eslint": "^9.39.1",
48
+ "@types/node": "^25.0.3",
49
+ "browserslist": "^4.28.1",
50
+ "caniuse-lite": "^1.0.30001760",
51
+ "eslint": "^9.39.2",
51
52
  "eslint-config-prettier": "^10.1.8",
52
53
  "eslint-plugin-compat": "^6.0.2",
53
54
  "eslint-plugin-prettier": "^5.5.4",
54
55
  "globals": "^16.5.0",
55
- "rollup": "^4.53.3",
56
+ "rolldown": "1.0.0-beta.55",
57
+ "rollup": "^4.53.5",
56
58
  "rollup-plugin-clear": "^2.0.7",
57
59
  "tslib": "^2.8.1",
58
60
  "typescript": "^5.9.3",
59
- "typescript-eslint": "^8.47.0"
61
+ "typescript-eslint": "^8.50.0"
60
62
  },
61
63
  "scripts": {
62
64
  "lint": "eslint .",
package/src/Dictionary.ts CHANGED
@@ -114,13 +114,23 @@ export class UtilsDictionary<K, V> {
114
114
  * 获取字典所有的键
115
115
  */
116
116
  keys(): K[] {
117
- return Array.from(this.items.keys());
117
+ const keys = this.items.keys();
118
+ if (typeof keys.toArray === "function") {
119
+ return keys.toArray();
120
+ } else {
121
+ return [...keys];
122
+ }
118
123
  }
119
124
  /**
120
125
  * 返回字典中的所有值
121
126
  */
122
127
  values(): V[] {
123
- return Array.from(this.items.values());
128
+ const values = this.items.values();
129
+ if (typeof values.toArray === "function") {
130
+ return values.toArray();
131
+ } else {
132
+ return [...values];
133
+ }
124
134
  }
125
135
  /**
126
136
  * 清空字典
package/src/Httpx.ts CHANGED
@@ -3,12 +3,12 @@ import { TryCatch } from "./TryCatch";
3
3
  import type {
4
4
  HttpxAllowInterceptConfig,
5
5
  HttpxHookErrorData,
6
+ HttpxInitOption,
6
7
  HttpxMethod,
8
+ HttpxPromise,
7
9
  HttpxRequestOption,
8
10
  HttpxResponse,
9
11
  HttpxResponseData,
10
- HttpxPromise,
11
- HttpxInitOption,
12
12
  } from "./types/Httpx";
13
13
  import { GenerateUUID } from "./UtilsCommon";
14
14
 
package/src/Log.ts CHANGED
@@ -12,23 +12,22 @@ export class Log {
12
12
  /* 配置 */
13
13
  #details: UtilsLogOptions = {
14
14
  tag: true,
15
- successColor: "#0000FF",
16
- errorColor: "#FF0000",
17
- infoColor: "0",
18
- warnColor: "0",
15
+ successColor: "background: #0eac0eff;",
16
+ errorColor: "background: #FF0000;",
17
+ infoColor: "background: #6495ed;",
18
+ warnColor: "background: #ff8c00;",
19
19
  debug: false,
20
20
  autoClearConsole: false,
21
21
  logMaxCount: 999,
22
22
  };
23
23
  /**
24
- * 颜色配置
24
+ * 通用样式配置
25
25
  */
26
- #msgColorDetails = [
27
- "font-weight: bold; color: cornflowerblue",
28
- "font-weight: bold; color: cornflowerblue",
29
- "font-weight: bold; color: darkorange",
30
- "font-weight: bold; color: cornflowerblue",
31
- ];
26
+ #commonStyleConfig = {
27
+ commonStyle: "color: #ffffff; padding: 3px; border-radius: 3px;line-height: 1;margin-right: 6px;",
28
+ tagStyle: "background: #6495ed;",
29
+ callerNameStyle: "background: #78909C;",
30
+ };
32
31
  /**
33
32
  * @param __GM_info 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串,用作tag名
34
33
  * @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
@@ -112,61 +111,36 @@ export class Log {
112
111
  /**
113
112
  * 输出内容
114
113
  * @param msg 需要输出的内容
115
- * @param color 颜色
116
- * @param otherStyle 其它CSS
114
+ * @param tagColor tag颜色
117
115
  */
118
- private printContent(msg: any[], color: string, otherStyle?: string) {
116
+ private printContent(msg: any[], tagColor?: string) {
119
117
  this.checkClearConsole();
120
- otherStyle = otherStyle || "";
121
118
  const stackSplit = new Error()!.stack!.split("\n");
122
119
  stackSplit.splice(0, 2);
123
120
  const { name: callerName, position: callerPosition } = this.parseErrorStack(stackSplit);
124
121
  const tagName = this.tag;
125
122
  const that = this;
126
123
 
124
+ const msgColorDetails: string[] = [];
125
+ // 控制台输出的tag的html格式
126
+ let consoleHTML = `%c${tagName}`;
127
127
  /** tag的html输出格式 */
128
- const tagNameHTML = `%c[${tagName}%c`;
129
- /** 调用的函数名的html输出格式 */
130
- let callerNameHTML = `%c${callerName}%c]%c`;
131
- if (callerName.trim() === "") {
132
- callerNameHTML = `-${callerNameHTML}`;
128
+ if (typeof tagColor === "string" && tagColor.trim() !== "") {
129
+ msgColorDetails.push(that.#commonStyleConfig.commonStyle + tagColor);
130
+ } else {
131
+ msgColorDetails.push(that.#commonStyleConfig.commonStyle + that.#commonStyleConfig.tagStyle);
133
132
  }
134
- /**
135
- * 输出消息到控制台
136
- * @param message
137
- */
138
- function consoleMsg(message: any) {
139
- if (typeof message === "string") {
140
- that.#console.log(
141
- `${tagNameHTML}${callerNameHTML} %s`,
142
- ...that.#msgColorDetails,
143
- `color: ${color};${otherStyle}`,
144
- message
145
- );
146
- } else if (typeof message === "number") {
147
- that.#console.log(
148
- `${tagNameHTML}${callerNameHTML} %d`,
149
- ...that.#msgColorDetails,
150
- `color: ${color};${otherStyle}`,
151
- message
152
- );
153
- } else if (typeof message === "object") {
154
- that.#console.log(
155
- `${tagNameHTML}${callerNameHTML} %o`,
156
- ...that.#msgColorDetails,
157
- `color: ${color};${otherStyle}`,
158
- message
159
- );
160
- } else {
161
- that.#console.log(message);
162
- }
133
+ // 调用的函数名的html输出格式
134
+ if (callerName.trim() == "") {
135
+ // 无调用函数名
136
+ } else {
137
+ consoleHTML = `${consoleHTML}%c${callerName}`;
138
+ msgColorDetails.push(that.#commonStyleConfig.commonStyle + that.#commonStyleConfig.callerNameStyle);
163
139
  }
164
140
  if (Array.isArray(msg)) {
165
- for (let index = 0; index < msg.length; index++) {
166
- consoleMsg(msg[index]);
167
- }
141
+ that.#console.log(`${consoleHTML}`, ...msgColorDetails, ...msg);
168
142
  } else {
169
- consoleMsg(msg);
143
+ that.#console.log(`${consoleHTML}`, ...msgColorDetails, msg);
170
144
  }
171
145
  if (this.#details.debug) {
172
146
  /* 如果开启调试模式,输出堆栈位置 */
@@ -191,7 +165,8 @@ export class Log {
191
165
  */
192
166
  warn(...args: any[]) {
193
167
  if (this.#disable) return;
194
- this.printContent(args, this.#details.warnColor, "background: #FEF6D5;padding: 4px 6px 4px 0px;");
168
+ // , "background: #FEF6D5;padding: 4px 6px 4px 0px;"
169
+ this.printContent(args, this.#details.warnColor);
195
170
  }
196
171
  /**
197
172
  * 控制台-错误输出
@@ -230,11 +205,11 @@ export class Log {
230
205
  /** 堆栈位置 */
231
206
  const stackFunctionNamePosition = errorStackParse.position;
232
207
  const callerName = stackFunctionName;
233
- this.#console.log(
234
- `%c[${this.tag}%c-%c${callerName}%c]%c`,
235
- ...this.#msgColorDetails,
236
- `color: ${this.#details.infoColor};`
237
- );
208
+ const msgColorDetails: string[] = [
209
+ `${this.#commonStyleConfig.commonStyle + this.#details.infoColor}`,
210
+ `${this.#commonStyleConfig.commonStyle + this.#commonStyleConfig.callerNameStyle}`,
211
+ ];
212
+ this.#console.log(`%c${this.tag}%c${callerName}%c`, msgColorDetails);
238
213
  this.#console.table(msg);
239
214
  if (this.#details.debug) {
240
215
  this.#console.log(stackFunctionNamePosition);
package/src/Utils.ts CHANGED
@@ -1,34 +1,34 @@
1
- import { ColorConversion } from "./ColorConversion";
2
- import { GBKEncoder } from "./GBKEncoder";
3
- import { UtilsGMCookie } from "./UtilsGMCookie";
1
+ import {
2
+ clearInterval as WorkerClearInterval,
3
+ clearTimeout as WorkerClearTimeout,
4
+ setInterval as WorkerSetInterval,
5
+ setTimeout as WorkerSetTimeout,
6
+ } from "worker-timers";
7
+ import { version } from "./../package.json";
4
8
  import { ajaxHooker } from "./ajaxHooker/ajaxHooker.js";
5
9
  import { AjaxHooker1_2_4 } from "./ajaxHooker/ajaxHooker1.2.4";
6
- import { GMMenu } from "./UtilsGMMenu";
10
+ import { ColorConversion } from "./ColorConversion";
11
+ import { CommonUtil } from "./CommonUtil";
12
+ import { UtilsDictionary } from "./Dictionary";
13
+ import { domUtils } from "./DOMUtils";
14
+ import { GBKEncoder } from "./GBKEncoder";
7
15
  import { Hooks } from "./Hooks";
8
16
  import { Httpx } from "./Httpx";
9
17
  import { indexedDB } from "./indexedDB";
10
18
  import { LockFunction } from "./LockFunction";
11
19
  import { Log } from "./Log";
20
+ import { ModuleRaid } from "./ModuleRaid";
12
21
  import { Progress } from "./Progress";
13
22
  import { TryCatch } from "./TryCatch";
14
- import { UtilsDictionary } from "./Dictionary";
15
23
  import type { UtilsAjaxHookResult } from "./types/ajaxHooker";
16
- import { GenerateUUID } from "./UtilsCommon";
17
- import { WindowApi } from "./WindowApi";
18
- import { Vue } from "./Vue";
19
24
  import { type ArgsType, type JSTypeNames, type UtilsOwnObject } from "./types/global";
20
- import type { WindowApiOption } from "./types/WindowApi";
21
- import {
22
- clearInterval as WorkerClearInterval,
23
- clearTimeout as WorkerClearTimeout,
24
- setInterval as WorkerSetInterval,
25
- setTimeout as WorkerSetTimeout,
26
- } from "worker-timers";
27
- import { ModuleRaid } from "./ModuleRaid";
28
- import { domUtils } from "./DOMUtils";
29
- import { CommonUtil } from "./CommonUtil";
30
25
  import type { ReactInstance } from "./types/React";
31
- import { version } from "./../package.json";
26
+ import type { WindowApiOption } from "./types/WindowApi";
27
+ import { GenerateUUID } from "./UtilsCommon";
28
+ import { UtilsGMCookie } from "./UtilsGMCookie";
29
+ import { GMMenu } from "./UtilsGMMenu";
30
+ import { Vue } from "./Vue";
31
+ import { WindowApi } from "./WindowApi";
32
32
 
33
33
  class Utils {
34
34
  private windowApi: typeof WindowApi.prototype;
@@ -1293,43 +1293,43 @@ class Utils {
1293
1293
  Hooks = Hooks;
1294
1294
 
1295
1295
  /**
1296
- * 为减少代码量和回调,把GM_xmlhttpRequest封装
1297
- * 文档地址: https://www.tampermonkey.net/documentation.php?ext=iikm
1298
- * 其中onloadstart、onprogress、onreadystatechange是回调形式,onabort、ontimeout、onerror可以设置全局回调函数
1299
- * @param _GM_xmlHttpRequest_ 油猴中的GM_xmlhttpRequest
1300
- * @example
1301
- let httpx = new Utils.Httpx(GM_xmlhttpRequest);
1302
- let postResp = await httpx.post({
1303
- url:url,
1304
- data:JSON.stringify({
1305
- test:1
1306
- }),
1307
- timeout: 5000
1308
- });
1309
- console.log(postResp);
1310
- > {
1311
- status: true,
1312
- data: {responseText: "...", response: xxx,...},
1313
- msg: "请求完毕",
1314
- type: "onload",
1315
- }
1316
-
1317
- if(postResp === "onload" && postResp.status){
1318
- // onload
1319
- }else if(postResp === "ontimeout"){
1320
- // ontimeout
1321
- }
1322
- * @example
1323
- // 也可以先配置全局参数
1324
- let httpx = new Utils.Httpx(GM_xmlhttpRequest);
1325
- httpx.config({
1326
- timeout: 5000,
1327
- async: false,
1328
- responseType: "html",
1329
- redirect: "follow",
1330
- })
1331
- // 优先级为 默认details < 全局details < 单独的details
1332
- */
1296
+ * 为减少代码量和回调,把GM_xmlhttpRequest封装
1297
+ * 文档地址: https://www.tampermonkey.net/documentation.php?ext=iikm
1298
+ * 其中onloadstart、onprogress、onreadystatechange是回调形式,onabort、ontimeout、onerror可以设置全局回调函数
1299
+ * @param _GM_xmlHttpRequest_ 油猴中的GM_xmlhttpRequest
1300
+ * @example
1301
+ let httpx = new Utils.Httpx(GM_xmlhttpRequest);
1302
+ let postResp = await httpx.post({
1303
+ url:url,
1304
+ data:JSON.stringify({
1305
+ test:1
1306
+ }),
1307
+ timeout: 5000
1308
+ });
1309
+ console.log(postResp);
1310
+ > {
1311
+ status: true,
1312
+ data: {responseText: "...", response: xxx,...},
1313
+ msg: "请求完毕",
1314
+ type: "onload",
1315
+ }
1316
+
1317
+ if(postResp === "onload" && postResp.status){
1318
+ // onload
1319
+ }else if(postResp === "ontimeout"){
1320
+ // ontimeout
1321
+ }
1322
+ * @example
1323
+ // 也可以先配置全局参数
1324
+ let httpx = new Utils.Httpx(GM_xmlhttpRequest);
1325
+ httpx.config({
1326
+ timeout: 5000,
1327
+ async: false,
1328
+ responseType: "html",
1329
+ redirect: "follow",
1330
+ })
1331
+ // 优先级为 默认details < 全局details < 单独的details
1332
+ */
1333
1333
  Httpx = Httpx;
1334
1334
  /**
1335
1335
  * 浏览器端的indexedDB操作封装
@@ -3209,7 +3209,7 @@ class Utils {
3209
3209
  */
3210
3210
  tryCatch = TryCatch;
3211
3211
  /**
3212
- * 数组去重,去除重复的值
3212
+ * 数组去重,去除重复的值,返回新的数组(不修改原数组)
3213
3213
  * @param uniqueArrayData 需要去重的数组
3214
3214
  * @param compareArrayData 用来比较的数组
3215
3215
  * @param compareFun 数组比较方法,如果值相同,去除该数据
@@ -89,7 +89,9 @@ export declare interface UtilsAjaxHookFilterOptions {
89
89
  /** Utils.ajaxHooker */
90
90
  export declare interface UtilsAjaxHookResult {
91
91
  /**
92
- * 劫持
92
+ * 劫持xhr和fetch
93
+ *
94
+ * 提示:如果多次调用.hook方法,将会多次处理同一个请求
93
95
  * @example
94
96
  ajaxHooker.hook(request => {
95
97
  if (request.url === 'https://www.example.com/') {