@whitesev/utils 2.8.2 → 2.9.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 +176 -176
- package/dist/index.amd.js +283 -776
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +2 -0
- package/dist/index.amd.min.js.map +1 -0
- package/dist/index.cjs.js +283 -776
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +2 -0
- package/dist/index.cjs.min.js.map +1 -0
- package/dist/index.esm.js +283 -776
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +2 -0
- package/dist/index.esm.min.js.map +1 -0
- package/dist/index.iife.js +283 -776
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +2 -0
- package/dist/index.iife.min.js.map +1 -0
- package/dist/index.system.js +283 -776
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +2 -0
- package/dist/index.system.min.js.map +1 -0
- package/dist/index.umd.js +283 -776
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +2 -0
- package/dist/index.umd.min.js.map +1 -0
- package/dist/types/src/Utils.d.ts +132 -448
- package/dist/types/src/UtilsGMCookie.d.ts +4 -0
- package/dist/types/src/UtilsGMMenu.d.ts +3 -6
- package/dist/types/src/types/Httpx.d.ts +1344 -1344
- package/dist/types/src/types/Log.d.ts +19 -19
- package/dist/types/src/types/Progress.d.ts +20 -20
- package/dist/types/src/types/React.d.ts +119 -119
- package/dist/types/src/types/TryCatch.d.ts +9 -9
- package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
- package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
- package/dist/types/src/types/Vue2.d.ts +166 -166
- package/dist/types/src/types/WindowApi.d.ts +14 -14
- package/dist/types/src/types/ajaxHooker.d.ts +153 -151
- package/dist/types/src/types/env.d.ts +7 -7
- package/dist/types/src/types/global.d.ts +31 -31
- package/package.json +11 -10
- package/src/ColorConversion.ts +105 -105
- package/src/CommonUtil.ts +280 -280
- package/src/DOMUtils.ts +251 -251
- package/src/Dictionary.ts +153 -153
- package/src/GBKEncoder.ts +108 -108
- package/src/Hooks.ts +73 -73
- package/src/Httpx.ts +1457 -1457
- package/src/LockFunction.ts +62 -62
- package/src/Log.ts +258 -258
- package/src/Progress.ts +108 -108
- package/src/TryCatch.ts +86 -86
- package/src/Utils.ts +3827 -4773
- package/src/UtilsCommon.ts +14 -14
- package/src/UtilsGMCookie.ts +272 -254
- package/src/UtilsGMMenu.ts +441 -445
- package/src/Vue.ts +233 -233
- package/src/WindowApi.ts +59 -59
- package/src/indexedDB.ts +497 -497
- package/src/types/Httpx.d.ts +1344 -1344
- package/src/types/Log.d.ts +19 -19
- package/src/types/Progress.d.ts +20 -20
- package/src/types/React.d.ts +119 -119
- package/src/types/TryCatch.d.ts +9 -9
- package/src/types/UtilsGMCookie.d.ts +93 -93
- package/src/types/UtilsGMMenu.d.ts +77 -77
- package/src/types/Vue2.d.ts +166 -166
- package/src/types/WindowApi.d.ts +14 -14
- package/src/types/ajaxHooker.d.ts +153 -151
- package/src/types/env.d.ts +7 -7
- package/src/types/global.d.ts +31 -31
- package/dist/types/src/types/Event.d.ts +0 -188
- package/src/types/Event.d.ts +0 -188
package/src/LockFunction.ts
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
export class LockFunction<K extends (...args: any[]) => any | Promise<any> | void> {
|
|
2
|
-
#flag: boolean = false;
|
|
3
|
-
#delayTime: number = 0;
|
|
4
|
-
#callback: K;
|
|
5
|
-
#timeId: number | undefined = void 0;
|
|
6
|
-
lock: () => void;
|
|
7
|
-
unlock: () => void;
|
|
8
|
-
run: (...args: any[]) => Promise<void>;
|
|
9
|
-
isLock: () => boolean;
|
|
10
|
-
/**
|
|
11
|
-
* @param callback 需要执行的函数
|
|
12
|
-
* @param delayTime (可选)延迟xx毫秒后解锁,默认:0
|
|
13
|
-
*/
|
|
14
|
-
constructor(callback: K, delayTime?: number);
|
|
15
|
-
/**
|
|
16
|
-
* @param callback 需要执行的函数
|
|
17
|
-
* @param context (可选)函数作用域,默认:this(Utils)
|
|
18
|
-
* @param delayTime (可选)延迟xx毫秒后解锁,默认:0
|
|
19
|
-
*/
|
|
20
|
-
constructor(callback: K, context?: any, delayTime?: number);
|
|
21
|
-
constructor(callback: K, context?: any, delayTime?: number) {
|
|
22
|
-
const that = this;
|
|
23
|
-
this.#callback = callback;
|
|
24
|
-
if (typeof context === "number") {
|
|
25
|
-
this.#delayTime = context;
|
|
26
|
-
} else {
|
|
27
|
-
this.#delayTime = delayTime as number;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* 锁
|
|
31
|
-
*/
|
|
32
|
-
this.lock = function () {
|
|
33
|
-
that.#flag = true;
|
|
34
|
-
clearTimeout(that.#timeId);
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* 解锁
|
|
38
|
-
*/
|
|
39
|
-
this.unlock = function () {
|
|
40
|
-
that.#timeId = setTimeout(() => {
|
|
41
|
-
that.#flag = false;
|
|
42
|
-
}, that.#delayTime);
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* 判断是否被锁
|
|
46
|
-
*/
|
|
47
|
-
this.isLock = function () {
|
|
48
|
-
return that.#flag;
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* 执行
|
|
52
|
-
*/
|
|
53
|
-
this.run = async function (...args: any[]) {
|
|
54
|
-
if (that.isLock()) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
that.lock();
|
|
58
|
-
await that.#callback.apply(this, args);
|
|
59
|
-
that.unlock();
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
1
|
+
export class LockFunction<K extends (...args: any[]) => any | Promise<any> | void> {
|
|
2
|
+
#flag: boolean = false;
|
|
3
|
+
#delayTime: number = 0;
|
|
4
|
+
#callback: K;
|
|
5
|
+
#timeId: number | undefined = void 0;
|
|
6
|
+
lock: () => void;
|
|
7
|
+
unlock: () => void;
|
|
8
|
+
run: (...args: any[]) => Promise<void>;
|
|
9
|
+
isLock: () => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* @param callback 需要执行的函数
|
|
12
|
+
* @param delayTime (可选)延迟xx毫秒后解锁,默认:0
|
|
13
|
+
*/
|
|
14
|
+
constructor(callback: K, delayTime?: number);
|
|
15
|
+
/**
|
|
16
|
+
* @param callback 需要执行的函数
|
|
17
|
+
* @param context (可选)函数作用域,默认:this(Utils)
|
|
18
|
+
* @param delayTime (可选)延迟xx毫秒后解锁,默认:0
|
|
19
|
+
*/
|
|
20
|
+
constructor(callback: K, context?: any, delayTime?: number);
|
|
21
|
+
constructor(callback: K, context?: any, delayTime?: number) {
|
|
22
|
+
const that = this;
|
|
23
|
+
this.#callback = callback;
|
|
24
|
+
if (typeof context === "number") {
|
|
25
|
+
this.#delayTime = context;
|
|
26
|
+
} else {
|
|
27
|
+
this.#delayTime = delayTime as number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 锁
|
|
31
|
+
*/
|
|
32
|
+
this.lock = function () {
|
|
33
|
+
that.#flag = true;
|
|
34
|
+
clearTimeout(that.#timeId);
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* 解锁
|
|
38
|
+
*/
|
|
39
|
+
this.unlock = function () {
|
|
40
|
+
that.#timeId = setTimeout(() => {
|
|
41
|
+
that.#flag = false;
|
|
42
|
+
}, that.#delayTime);
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* 判断是否被锁
|
|
46
|
+
*/
|
|
47
|
+
this.isLock = function () {
|
|
48
|
+
return that.#flag;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* 执行
|
|
52
|
+
*/
|
|
53
|
+
this.run = async function (...args: any[]) {
|
|
54
|
+
if (that.isLock()) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
that.lock();
|
|
58
|
+
await that.#callback.apply(this, args);
|
|
59
|
+
that.unlock();
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/Log.ts
CHANGED
|
@@ -1,258 +1,258 @@
|
|
|
1
|
-
import type { UtilsLogOptions } from "./types/Log";
|
|
2
|
-
|
|
3
|
-
export class Log {
|
|
4
|
-
/** 是否禁用输出的flag */
|
|
5
|
-
#disable: boolean = false;
|
|
6
|
-
/** 前面的TAG标志 */
|
|
7
|
-
tag: string = "Utils.Log";
|
|
8
|
-
/* 使用的console函数 */
|
|
9
|
-
#console: Console = null as any;
|
|
10
|
-
/* 当前输出的数量 */
|
|
11
|
-
#logCount = 0;
|
|
12
|
-
/* 配置 */
|
|
13
|
-
#details: UtilsLogOptions = {
|
|
14
|
-
tag: true,
|
|
15
|
-
successColor: "#0000FF",
|
|
16
|
-
errorColor: "#FF0000",
|
|
17
|
-
infoColor: "0",
|
|
18
|
-
warnColor: "0",
|
|
19
|
-
debug: false,
|
|
20
|
-
autoClearConsole: false,
|
|
21
|
-
logMaxCount: 999,
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* 颜色配置
|
|
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
|
-
];
|
|
32
|
-
/**
|
|
33
|
-
* @param __GM_info 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串,用作tag名
|
|
34
|
-
* @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
|
|
35
|
-
*/
|
|
36
|
-
constructor(
|
|
37
|
-
__GM_info?:
|
|
38
|
-
| {
|
|
39
|
-
script: {
|
|
40
|
-
name: string;
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
| string,
|
|
44
|
-
console: Console = window.console
|
|
45
|
-
) {
|
|
46
|
-
if (typeof __GM_info === "string") {
|
|
47
|
-
this.tag = __GM_info;
|
|
48
|
-
} else if (typeof __GM_info === "object" && typeof __GM_info?.script?.name === "string") {
|
|
49
|
-
this.tag = __GM_info.script.name;
|
|
50
|
-
}
|
|
51
|
-
this.#console = console;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 解析Error的堆栈获取实际调用者的函数名及函数所在的位置
|
|
56
|
-
* @param stack
|
|
57
|
-
*/
|
|
58
|
-
private parseErrorStack(stack: string[]) {
|
|
59
|
-
const result = {
|
|
60
|
-
name: "",
|
|
61
|
-
position: "",
|
|
62
|
-
};
|
|
63
|
-
for (let stackString of stack) {
|
|
64
|
-
stackString = stackString.trim();
|
|
65
|
-
const stackFunctionNameMatch = stackString.match(/^at[\s]+(.+?)[\s]+/i);
|
|
66
|
-
const stackFunctionNamePositionMatch = stackString.match(/^at[\s]+.+[\s]+\((.+?)\)/i);
|
|
67
|
-
if (stackFunctionNameMatch == null) {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
if (stackFunctionNamePositionMatch == null) {
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
/* 获取最后一个,因为第一个是包含了at */
|
|
74
|
-
const stackFunctionName = stackFunctionNameMatch[stackFunctionNameMatch.length - 1];
|
|
75
|
-
const stackFunctionNamePosition = stackFunctionNamePositionMatch[stackFunctionNamePositionMatch.length - 1];
|
|
76
|
-
if (
|
|
77
|
-
stackFunctionName === "" ||
|
|
78
|
-
stackFunctionName.match(/^(Utils\.|)Log(\.|)|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each/g)
|
|
79
|
-
) {
|
|
80
|
-
continue;
|
|
81
|
-
} else {
|
|
82
|
-
result.name = stackFunctionName;
|
|
83
|
-
result.position = stackFunctionNamePosition;
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (result.position === "") {
|
|
88
|
-
const lastStackString = stack[stack.length - 1].trim();
|
|
89
|
-
if (lastStackString.startsWith("at chrome-extension://")) {
|
|
90
|
-
const lastStackMatch = lastStackString.match(/^at[\s]+(.+)/);
|
|
91
|
-
if (lastStackMatch) {
|
|
92
|
-
result.position = lastStackMatch[lastStackMatch.length - 1];
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (result.position === "") {
|
|
97
|
-
result.position = stack[stack.length - 1].trim().replace(/^at[\s]*/g, "");
|
|
98
|
-
}
|
|
99
|
-
return result;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* 检测清理控制台
|
|
103
|
-
*/
|
|
104
|
-
private checkClearConsole() {
|
|
105
|
-
this.#logCount++;
|
|
106
|
-
if (this.#details.autoClearConsole && this.#logCount > this.#details.logMaxCount) {
|
|
107
|
-
this.#console.clear();
|
|
108
|
-
this.#logCount = 0;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 输出内容
|
|
114
|
-
* @param msg 需要输出的内容
|
|
115
|
-
* @param color 颜色
|
|
116
|
-
* @param otherStyle 其它CSS
|
|
117
|
-
*/
|
|
118
|
-
private printContent(msg: any[], color: string, otherStyle?: string) {
|
|
119
|
-
this.checkClearConsole();
|
|
120
|
-
otherStyle = otherStyle || "";
|
|
121
|
-
const stackSplit = new Error()!.stack!.split("\n");
|
|
122
|
-
stackSplit.splice(0, 2);
|
|
123
|
-
const { name: callerName, position: callerPosition } = this.parseErrorStack(stackSplit);
|
|
124
|
-
const tagName = this.tag;
|
|
125
|
-
const that = this;
|
|
126
|
-
|
|
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}`;
|
|
133
|
-
}
|
|
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
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if (Array.isArray(msg)) {
|
|
165
|
-
for (let index = 0; index < msg.length; index++) {
|
|
166
|
-
consoleMsg(msg[index]);
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
consoleMsg(msg);
|
|
170
|
-
}
|
|
171
|
-
if (this.#details.debug) {
|
|
172
|
-
/* 如果开启调试模式,输出堆栈位置 */
|
|
173
|
-
this.#console.log(callerPosition);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* 控制台-普通输出
|
|
178
|
-
* @param args 需要输出的内容
|
|
179
|
-
* @example
|
|
180
|
-
* log.info("输出信息","输出信息2","输出信息3","输出")
|
|
181
|
-
*/
|
|
182
|
-
info(...args: any[]) {
|
|
183
|
-
if (this.#disable) return;
|
|
184
|
-
this.printContent(args, this.#details.infoColor);
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* 控制台-警告输出
|
|
188
|
-
* @param args 需要输出的内容
|
|
189
|
-
* @example
|
|
190
|
-
* log.warn("输出警告","输出警告2","输出警告3","输出警告4")
|
|
191
|
-
*/
|
|
192
|
-
warn(...args: any[]) {
|
|
193
|
-
if (this.#disable) return;
|
|
194
|
-
this.printContent(args, this.#details.warnColor, "background: #FEF6D5;padding: 4px 6px 4px 0px;");
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* 控制台-错误输出
|
|
198
|
-
* @param args 需要输出的内容
|
|
199
|
-
* @example
|
|
200
|
-
* log.error("输出错误","输出错误2","输出错误3","输出错误4")
|
|
201
|
-
*/
|
|
202
|
-
error(...args: any[]) {
|
|
203
|
-
if (this.#disable) return;
|
|
204
|
-
this.printContent(args, this.#details.errorColor);
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* 控制台-成功输出
|
|
208
|
-
* @param args 需要输出的内容
|
|
209
|
-
* @example
|
|
210
|
-
* log.success("输出成功")
|
|
211
|
-
*/
|
|
212
|
-
success(...args: any[]) {
|
|
213
|
-
if (this.#disable) return;
|
|
214
|
-
this.printContent(args, this.#details.successColor);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* 控制台-输出表格
|
|
218
|
-
* @param msg 需要输出的内容
|
|
219
|
-
* @example
|
|
220
|
-
* log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
|
|
221
|
-
*/
|
|
222
|
-
table(msg: any[]) {
|
|
223
|
-
if (this.#disable) return;
|
|
224
|
-
this.checkClearConsole();
|
|
225
|
-
const stack = new Error()!.stack!.split("\n");
|
|
226
|
-
stack.splice(0, 1);
|
|
227
|
-
const errorStackParse = this.parseErrorStack(stack);
|
|
228
|
-
/** 堆栈函数名 */
|
|
229
|
-
const stackFunctionName = errorStackParse.name;
|
|
230
|
-
/** 堆栈位置 */
|
|
231
|
-
const stackFunctionNamePosition = errorStackParse.position;
|
|
232
|
-
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
|
-
);
|
|
238
|
-
this.#console.table(msg);
|
|
239
|
-
if (this.#details.debug) {
|
|
240
|
-
this.#console.log(stackFunctionNamePosition);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* 配置Log对象的颜色
|
|
245
|
-
* @param paramDetails 配置信息
|
|
246
|
-
*/
|
|
247
|
-
config(paramDetails: Partial<UtilsLogOptions>) {
|
|
248
|
-
this.#details = Object.assign(this.#details, paramDetails);
|
|
249
|
-
}
|
|
250
|
-
/** 禁用输出 */
|
|
251
|
-
disable() {
|
|
252
|
-
this.#disable = true;
|
|
253
|
-
}
|
|
254
|
-
/** 恢复输出 */
|
|
255
|
-
recovery() {
|
|
256
|
-
this.#disable = false;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
1
|
+
import type { UtilsLogOptions } from "./types/Log";
|
|
2
|
+
|
|
3
|
+
export class Log {
|
|
4
|
+
/** 是否禁用输出的flag */
|
|
5
|
+
#disable: boolean = false;
|
|
6
|
+
/** 前面的TAG标志 */
|
|
7
|
+
tag: string = "Utils.Log";
|
|
8
|
+
/* 使用的console函数 */
|
|
9
|
+
#console: Console = null as any;
|
|
10
|
+
/* 当前输出的数量 */
|
|
11
|
+
#logCount = 0;
|
|
12
|
+
/* 配置 */
|
|
13
|
+
#details: UtilsLogOptions = {
|
|
14
|
+
tag: true,
|
|
15
|
+
successColor: "#0000FF",
|
|
16
|
+
errorColor: "#FF0000",
|
|
17
|
+
infoColor: "0",
|
|
18
|
+
warnColor: "0",
|
|
19
|
+
debug: false,
|
|
20
|
+
autoClearConsole: false,
|
|
21
|
+
logMaxCount: 999,
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* 颜色配置
|
|
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
|
+
];
|
|
32
|
+
/**
|
|
33
|
+
* @param __GM_info 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串,用作tag名
|
|
34
|
+
* @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
|
|
35
|
+
*/
|
|
36
|
+
constructor(
|
|
37
|
+
__GM_info?:
|
|
38
|
+
| {
|
|
39
|
+
script: {
|
|
40
|
+
name: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
| string,
|
|
44
|
+
console: Console = window.console
|
|
45
|
+
) {
|
|
46
|
+
if (typeof __GM_info === "string") {
|
|
47
|
+
this.tag = __GM_info;
|
|
48
|
+
} else if (typeof __GM_info === "object" && typeof __GM_info?.script?.name === "string") {
|
|
49
|
+
this.tag = __GM_info.script.name;
|
|
50
|
+
}
|
|
51
|
+
this.#console = console;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 解析Error的堆栈获取实际调用者的函数名及函数所在的位置
|
|
56
|
+
* @param stack
|
|
57
|
+
*/
|
|
58
|
+
private parseErrorStack(stack: string[]) {
|
|
59
|
+
const result = {
|
|
60
|
+
name: "",
|
|
61
|
+
position: "",
|
|
62
|
+
};
|
|
63
|
+
for (let stackString of stack) {
|
|
64
|
+
stackString = stackString.trim();
|
|
65
|
+
const stackFunctionNameMatch = stackString.match(/^at[\s]+(.+?)[\s]+/i);
|
|
66
|
+
const stackFunctionNamePositionMatch = stackString.match(/^at[\s]+.+[\s]+\((.+?)\)/i);
|
|
67
|
+
if (stackFunctionNameMatch == null) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (stackFunctionNamePositionMatch == null) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
/* 获取最后一个,因为第一个是包含了at */
|
|
74
|
+
const stackFunctionName = stackFunctionNameMatch[stackFunctionNameMatch.length - 1];
|
|
75
|
+
const stackFunctionNamePosition = stackFunctionNamePositionMatch[stackFunctionNamePositionMatch.length - 1];
|
|
76
|
+
if (
|
|
77
|
+
stackFunctionName === "" ||
|
|
78
|
+
stackFunctionName.match(/^(Utils\.|)Log(\.|)|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each/g)
|
|
79
|
+
) {
|
|
80
|
+
continue;
|
|
81
|
+
} else {
|
|
82
|
+
result.name = stackFunctionName;
|
|
83
|
+
result.position = stackFunctionNamePosition;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (result.position === "") {
|
|
88
|
+
const lastStackString = stack[stack.length - 1].trim();
|
|
89
|
+
if (lastStackString.startsWith("at chrome-extension://")) {
|
|
90
|
+
const lastStackMatch = lastStackString.match(/^at[\s]+(.+)/);
|
|
91
|
+
if (lastStackMatch) {
|
|
92
|
+
result.position = lastStackMatch[lastStackMatch.length - 1];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (result.position === "") {
|
|
97
|
+
result.position = stack[stack.length - 1].trim().replace(/^at[\s]*/g, "");
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 检测清理控制台
|
|
103
|
+
*/
|
|
104
|
+
private checkClearConsole() {
|
|
105
|
+
this.#logCount++;
|
|
106
|
+
if (this.#details.autoClearConsole && this.#logCount > this.#details.logMaxCount) {
|
|
107
|
+
this.#console.clear();
|
|
108
|
+
this.#logCount = 0;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 输出内容
|
|
114
|
+
* @param msg 需要输出的内容
|
|
115
|
+
* @param color 颜色
|
|
116
|
+
* @param otherStyle 其它CSS
|
|
117
|
+
*/
|
|
118
|
+
private printContent(msg: any[], color: string, otherStyle?: string) {
|
|
119
|
+
this.checkClearConsole();
|
|
120
|
+
otherStyle = otherStyle || "";
|
|
121
|
+
const stackSplit = new Error()!.stack!.split("\n");
|
|
122
|
+
stackSplit.splice(0, 2);
|
|
123
|
+
const { name: callerName, position: callerPosition } = this.parseErrorStack(stackSplit);
|
|
124
|
+
const tagName = this.tag;
|
|
125
|
+
const that = this;
|
|
126
|
+
|
|
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}`;
|
|
133
|
+
}
|
|
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
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (Array.isArray(msg)) {
|
|
165
|
+
for (let index = 0; index < msg.length; index++) {
|
|
166
|
+
consoleMsg(msg[index]);
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
consoleMsg(msg);
|
|
170
|
+
}
|
|
171
|
+
if (this.#details.debug) {
|
|
172
|
+
/* 如果开启调试模式,输出堆栈位置 */
|
|
173
|
+
this.#console.log(callerPosition);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 控制台-普通输出
|
|
178
|
+
* @param args 需要输出的内容
|
|
179
|
+
* @example
|
|
180
|
+
* log.info("输出信息","输出信息2","输出信息3","输出")
|
|
181
|
+
*/
|
|
182
|
+
info(...args: any[]) {
|
|
183
|
+
if (this.#disable) return;
|
|
184
|
+
this.printContent(args, this.#details.infoColor);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* 控制台-警告输出
|
|
188
|
+
* @param args 需要输出的内容
|
|
189
|
+
* @example
|
|
190
|
+
* log.warn("输出警告","输出警告2","输出警告3","输出警告4")
|
|
191
|
+
*/
|
|
192
|
+
warn(...args: any[]) {
|
|
193
|
+
if (this.#disable) return;
|
|
194
|
+
this.printContent(args, this.#details.warnColor, "background: #FEF6D5;padding: 4px 6px 4px 0px;");
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* 控制台-错误输出
|
|
198
|
+
* @param args 需要输出的内容
|
|
199
|
+
* @example
|
|
200
|
+
* log.error("输出错误","输出错误2","输出错误3","输出错误4")
|
|
201
|
+
*/
|
|
202
|
+
error(...args: any[]) {
|
|
203
|
+
if (this.#disable) return;
|
|
204
|
+
this.printContent(args, this.#details.errorColor);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* 控制台-成功输出
|
|
208
|
+
* @param args 需要输出的内容
|
|
209
|
+
* @example
|
|
210
|
+
* log.success("输出成功")
|
|
211
|
+
*/
|
|
212
|
+
success(...args: any[]) {
|
|
213
|
+
if (this.#disable) return;
|
|
214
|
+
this.printContent(args, this.#details.successColor);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* 控制台-输出表格
|
|
218
|
+
* @param msg 需要输出的内容
|
|
219
|
+
* @example
|
|
220
|
+
* log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
|
|
221
|
+
*/
|
|
222
|
+
table(msg: any[]) {
|
|
223
|
+
if (this.#disable) return;
|
|
224
|
+
this.checkClearConsole();
|
|
225
|
+
const stack = new Error()!.stack!.split("\n");
|
|
226
|
+
stack.splice(0, 1);
|
|
227
|
+
const errorStackParse = this.parseErrorStack(stack);
|
|
228
|
+
/** 堆栈函数名 */
|
|
229
|
+
const stackFunctionName = errorStackParse.name;
|
|
230
|
+
/** 堆栈位置 */
|
|
231
|
+
const stackFunctionNamePosition = errorStackParse.position;
|
|
232
|
+
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
|
+
);
|
|
238
|
+
this.#console.table(msg);
|
|
239
|
+
if (this.#details.debug) {
|
|
240
|
+
this.#console.log(stackFunctionNamePosition);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* 配置Log对象的颜色
|
|
245
|
+
* @param paramDetails 配置信息
|
|
246
|
+
*/
|
|
247
|
+
config(paramDetails: Partial<UtilsLogOptions>) {
|
|
248
|
+
this.#details = Object.assign(this.#details, paramDetails);
|
|
249
|
+
}
|
|
250
|
+
/** 禁用输出 */
|
|
251
|
+
disable() {
|
|
252
|
+
this.#disable = true;
|
|
253
|
+
}
|
|
254
|
+
/** 恢复输出 */
|
|
255
|
+
recovery() {
|
|
256
|
+
this.#disable = false;
|
|
257
|
+
}
|
|
258
|
+
}
|