@whitesev/utils 1.0.0

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.
Files changed (55) hide show
  1. package/README.md +172 -0
  2. package/dist/index.cjs.js +6017 -0
  3. package/dist/index.cjs.js.map +1 -0
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.esm.js +6015 -0
  6. package/dist/index.esm.js.map +1 -0
  7. package/dist/index.umd.js +6023 -0
  8. package/dist/index.umd.js.map +1 -0
  9. package/dist/src/ColorConversion.d.ts +45 -0
  10. package/dist/src/Dictionary.d.ts +87 -0
  11. package/dist/src/GBKEncoder.d.ts +17 -0
  12. package/dist/src/Hooks.d.ts +5 -0
  13. package/dist/src/Httpx.d.ts +50 -0
  14. package/dist/src/LockFunction.d.ts +16 -0
  15. package/dist/src/Log.d.ts +66 -0
  16. package/dist/src/Progress.d.ts +6 -0
  17. package/dist/src/Utils.d.ts +1560 -0
  18. package/dist/src/UtilsCore.d.ts +9 -0
  19. package/dist/src/UtilsGMCookie.d.ts +36 -0
  20. package/dist/src/UtilsGMMenu.d.ts +119 -0
  21. package/dist/src/ajaxHooker.d.ts +6 -0
  22. package/dist/src/indexedDB.d.ts +165 -0
  23. package/dist/src/tryCatch.d.ts +31 -0
  24. package/index.ts +3 -0
  25. package/package.json +34 -0
  26. package/rollup.config.js +28 -0
  27. package/src/ColorConversion.ts +124 -0
  28. package/src/Dictionary.ts +158 -0
  29. package/src/GBKEncoder.js +111 -0
  30. package/src/GBKEncoder.ts +116 -0
  31. package/src/Hooks.js +73 -0
  32. package/src/Httpx.js +747 -0
  33. package/src/LockFunction.js +35 -0
  34. package/src/Log.js +256 -0
  35. package/src/Progress.js +98 -0
  36. package/src/Utils.ts +4495 -0
  37. package/src/UtilsCore.ts +39 -0
  38. package/src/UtilsGMCookie.ts +167 -0
  39. package/src/UtilsGMMenu.js +464 -0
  40. package/src/ajaxHooker.js +560 -0
  41. package/src/indexedDB.js +355 -0
  42. package/src/tryCatch.js +100 -0
  43. package/src/types/AjaxHooker.d.ts +153 -0
  44. package/src/types/DOMUtils.d.ts +188 -0
  45. package/src/types/Hook.d.ts +16 -0
  46. package/src/types/Httpx.d.ts +1308 -0
  47. package/src/types/Indexdb.d.ts +128 -0
  48. package/src/types/LockFunction.d.ts +47 -0
  49. package/src/types/Log.d.ts +91 -0
  50. package/src/types/Progress.d.ts +30 -0
  51. package/src/types/TryCatch.d.ts +6 -0
  52. package/src/types/UtilsCore.d.ts +7 -0
  53. package/src/types/UtilsGMMenu.d.ts +224 -0
  54. package/src/types/global.d.ts +58 -0
  55. package/tsconfig.json +32 -0
@@ -0,0 +1,35 @@
1
+ const LockFunction = function (callback, context, delayTime = 0) {
2
+ let flag = false;
3
+ let that = this;
4
+ context = context || this;
5
+ /**
6
+ * 锁
7
+ */
8
+ this.lock = function () {
9
+ flag = true;
10
+ };
11
+ /**
12
+ * 解锁
13
+ */
14
+ this.unlock = function () {
15
+ setTimeout(() => {
16
+ flag = false;
17
+ }, delayTime);
18
+ };
19
+ /**
20
+ * 执行
21
+ */
22
+ this.run = async function (...args) {
23
+ if (flag) {
24
+ return;
25
+ }
26
+ that.lock();
27
+ await callback.apply(context, args);
28
+ that.unlock();
29
+ };
30
+ };
31
+
32
+
33
+ export {
34
+ LockFunction
35
+ }
package/src/Log.js ADDED
@@ -0,0 +1,256 @@
1
+ const Log = function (
2
+ _GM_info_ = {
3
+ script: {
4
+ name: "Utils.Log",
5
+ },
6
+ },
7
+ console = globalThis.console
8
+ ) {
9
+ let msgColorDetails = [
10
+ "font-weight: bold; color: cornflowerblue",
11
+ "font-weight: bold; color: cornflowerblue",
12
+ "font-weight: bold; color: darkorange",
13
+ "font-weight: bold; color: cornflowerblue",
14
+ ];
15
+ /**
16
+ * @type {UtilsLogOptions}
17
+ */
18
+ let details = {
19
+ tag: true,
20
+ successColor: "#0000FF",
21
+ errorColor: "#FF0000",
22
+ infoColor: "0",
23
+ warnColor: "0",
24
+ debug: false,
25
+ autoClearConsole: false,
26
+ logMaxCount: 999,
27
+ };
28
+ let logCount = 0;
29
+ /**
30
+ * 解析Error的堆栈获取实际调用者的函数名及函数所在的位置
31
+ * @param {string[]} stack
32
+ * @returns {{
33
+ * name: string,
34
+ * position: string,
35
+ * }}
36
+ */
37
+ let parseErrorStack = function (stack) {
38
+ let result = {
39
+ name: "",
40
+ position: "",
41
+ };
42
+ for (let stackString of stack) {
43
+ stackString = stackString.trim();
44
+ let stackFunctionName = stackString.match(/^at[\s]+(.+?)[\s]+/i);
45
+ let stackFunctionNamePosition = stackString.match(
46
+ /^at[\s]+.+[\s]+\((.+?)\)/i
47
+ );
48
+ if (stackFunctionName == null) {
49
+ continue;
50
+ }
51
+ stackFunctionName = stackFunctionName[stackFunctionName.length - 1];
52
+ stackFunctionNamePosition =
53
+ stackFunctionNamePosition[stackFunctionNamePosition.length - 1];
54
+ if (
55
+ stackFunctionName === "" ||
56
+ stackFunctionName.match(
57
+ new RegExp(
58
+ "(^Utils.Log.|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each)",
59
+ "g"
60
+ )
61
+ )
62
+ ) {
63
+ continue;
64
+ } else {
65
+ result.name = stackFunctionName;
66
+ result.position = stackFunctionNamePosition;
67
+ break;
68
+ }
69
+ }
70
+ if (result.position === "") {
71
+ let lastStackString = stack[stack.length - 1].trim();
72
+ if (lastStackString.startsWith("at chrome-extension://")) {
73
+ let lastStackMatch = lastStackString.match(/^at[\s]+(.+)/);
74
+ if (lastStackMatch) {
75
+ result.position = lastStackMatch[lastStackMatch.length - 1];
76
+ }
77
+ }
78
+ }
79
+ if (result.position === "") {
80
+ result.position = stack[stack.length - 1].trim().replace(/^at[\s]*/g, "");
81
+ }
82
+ return result;
83
+ };
84
+ /**
85
+ * 待恢复的函数或对象
86
+ */
87
+ let recoveryList = [];
88
+ /**
89
+ * 检测清理控制台
90
+ * @this {Utils.Log}
91
+ */
92
+ let checkClearConsole = function () {
93
+ logCount++;
94
+ if (details.autoClearConsole && logCount > details.logMaxCount) {
95
+ console.clear();
96
+ logCount = 0;
97
+ }
98
+ };
99
+ /**
100
+ * 输出内容
101
+ * @param {any} msg 需要输出的内容
102
+ * @param {string} color 颜色
103
+ * @param {string|undefined} otherStyle 其它CSS
104
+ * @this {Utils.Log}
105
+ */
106
+ let printContent = function (msg, color, otherStyle) {
107
+ checkClearConsole.apply(this);
108
+ otherStyle = otherStyle || "";
109
+ let stackSplit = new Error().stack.split("\n");
110
+ stackSplit.splice(0, 2);
111
+ let { name: callerName, position: callerPosition } =
112
+ parseErrorStack(stackSplit);
113
+ let tagName = this.tag;
114
+ function consoleMsg(_msg_) {
115
+ if (typeof _msg_ === "string") {
116
+ console.log(
117
+ `%c[${tagName}%c-%c${callerName}%c]%c %s`,
118
+ ...msgColorDetails,
119
+ `color: ${color};${otherStyle}`,
120
+ _msg_
121
+ );
122
+ } else if (typeof _msg_ === "number") {
123
+ console.log(
124
+ `%c[${tagName}%c-%c${callerName}%c]%c %d`,
125
+ ...msgColorDetails,
126
+ `color: ${color};${otherStyle}`,
127
+ _msg_
128
+ );
129
+ } else if (typeof _msg_ === "object") {
130
+ console.log(
131
+ `%c[${tagName}%c-%c${callerName}%c]%c %o`,
132
+ ...msgColorDetails,
133
+ `color: ${color};${otherStyle}`,
134
+ _msg_
135
+ );
136
+ } else {
137
+ console.log(_msg_);
138
+ }
139
+ }
140
+ if (Array.isArray(msg)) {
141
+ msg.forEach((item) => {
142
+ consoleMsg(item);
143
+ });
144
+ } else {
145
+ consoleMsg(msg);
146
+ }
147
+ if (details.debug) {
148
+ /* 如果开启调试模式,输出堆栈位置 */
149
+ console.log(callerPosition);
150
+ }
151
+ };
152
+ /**
153
+ * 前面的TAG标志
154
+ */
155
+ this.tag = _GM_info_?.script?.name || "Utils.Log";
156
+ /**
157
+ * 控制台-普通输出
158
+ * @param {any} msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
159
+ * @param {string|undefined} color 输出的颜色
160
+ * @param {string|undefined} otherStyle 其它CSS
161
+ */
162
+ this.info = function (msg, color = details.infoColor, otherStyle) {
163
+ printContent.call(this, msg, color, otherStyle);
164
+ };
165
+ /**
166
+ * 控制台-警告输出
167
+ * @param {any} msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
168
+ * @param {string|undefined} color 输出的颜色
169
+ * @param {string|undefined} otherStyle 其它CSS
170
+ */
171
+ this.warn = function (
172
+ msg,
173
+ color = details.warnColor,
174
+ otherStyle = "background: #FEF6D5;padding: 4px 6px 4px 0px;"
175
+ ) {
176
+ printContent.call(this, msg, color, otherStyle);
177
+ };
178
+ /**
179
+ * 控制台-错误输出
180
+ * @param {any} msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
181
+ * @param {string|undefined} color 输出的颜色
182
+ * @param {string|undefined} otherStyle 其它CSS
183
+ */
184
+ this.error = function (msg, color = details.errorColor, otherStyle) {
185
+ printContent.call(this, msg, color, otherStyle);
186
+ };
187
+ /**
188
+ * 控制台-成功输出
189
+ * @param {any} msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
190
+ * @param {string|undefined} color 输出的颜色
191
+ * @param {string|undefined} otherStyle 其它CSS
192
+ */
193
+ this.success = function (msg, color = details.successColor, otherStyle) {
194
+ printContent.call(this, msg, color, otherStyle);
195
+ };
196
+ /**
197
+ * 控制台-输出表格
198
+ * @param {object[]} msg
199
+ * @param {string|undefined} color 输出的颜色
200
+ * @param {string|undefined} otherStyle 其它CSS
201
+ * @example
202
+ * log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
203
+ */
204
+ this.table = function (msg, color = details.infoColor, otherStyle = "") {
205
+ checkClearConsole.apply(this);
206
+ let stack = new Error().stack.split("\n");
207
+ stack.splice(0, 1);
208
+ let errorStackParse = parseErrorStack(stack);
209
+ let stackFunctionName = errorStackParse.name;
210
+ let stackFunctionNamePosition = errorStackParse.position;
211
+ let callerName = stackFunctionName;
212
+ console.log(
213
+ `%c[${this.tag}%c-%c${callerName}%c]%c`,
214
+ ...msgColorDetails,
215
+ `color: ${color};${otherStyle}`
216
+ );
217
+ console.table(msg);
218
+ if (details.debug) {
219
+ console.log(stackFunctionNamePosition);
220
+ }
221
+ };
222
+ /**
223
+ * 配置Log对象的颜色
224
+ * @param {UtilsLogOptions} paramDetails 配置信息
225
+ */
226
+ this.config = function (paramDetails) {
227
+ details = Object.assign(details, paramDetails);
228
+ };
229
+ /**
230
+ * 禁用输出
231
+ */
232
+ this.disable = function () {
233
+ let that = this;
234
+ Object.keys(this)
235
+ .filter((keyName) => Boolean(keyName.match(/info|error|success|table/)))
236
+ .forEach((keyName) => {
237
+ let value = {};
238
+ value[keyName] = that[keyName];
239
+ recoveryList = [...recoveryList, value];
240
+ that[keyName] = () => {};
241
+ });
242
+ };
243
+ /**
244
+ * 恢复输出
245
+ */
246
+ this.recovery = function () {
247
+ let that = this;
248
+ recoveryList.forEach((item) => {
249
+ let keyName = Object.keys(item);
250
+ that[keyName] = item[keyName];
251
+ });
252
+ recoveryList = [];
253
+ };
254
+ };
255
+
256
+ export { Log };
@@ -0,0 +1,98 @@
1
+ const Progress = function (paramConfig) {
2
+ this.config = {
3
+ /**
4
+ * canvas元素节点
5
+ * @type {HTMLCanvasElement}
6
+ */
7
+ canvasNode: null,
8
+ /**
9
+ * 绘制角度
10
+ */
11
+ deg: 95,
12
+ /**
13
+ * 进度
14
+ */
15
+ progress: 0,
16
+ /**
17
+ * 绘制的线宽度
18
+ */
19
+ lineWidth: 10,
20
+ /**
21
+ * 绘制的背景颜色
22
+ */
23
+ lineBgColor: "#1e637c",
24
+ /**
25
+ * 绘制的线的颜色
26
+ */
27
+ lineColor: "#25deff",
28
+ /**
29
+ * 绘制的字体颜色
30
+ */
31
+ textColor: "#000000",
32
+ /**
33
+ * 绘制的字体大小(px)
34
+ */
35
+ fontSize: 22,
36
+ /**
37
+ * 绘制的圆的半径
38
+ */
39
+ circleRadius: 50,
40
+ /**
41
+ * 控制绘制的函数
42
+ */
43
+ draw: () => {},
44
+ };
45
+ this.config = Utils.assign(this.config, paramConfig);
46
+ if (!(this.config.canvasNode instanceof HTMLCanvasElement)) {
47
+ throw new Error("Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement");
48
+ }
49
+ /* 获取画笔 */
50
+ let ctx = this.config.canvasNode.getContext("2d");
51
+ /* 元素宽度 */
52
+ let width = this.config.canvasNode.width;
53
+ /* 元素高度 */
54
+ let height = this.config.canvasNode.height;
55
+
56
+ /* 清除锯齿 */
57
+ if (window.devicePixelRatio) {
58
+ this.config.canvasNode.style.width = width + "px";
59
+ this.config.canvasNode.style.height = height + "px";
60
+ this.config.canvasNode.height = height * window.devicePixelRatio;
61
+ this.config.canvasNode.width = width * window.devicePixelRatio;
62
+ ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
63
+ }
64
+ /* 设置线宽 */
65
+ ctx.lineWidth = this.config.lineWidth;
66
+ /* 绘制 */
67
+ this.draw = function () {
68
+ let degActive = (this.config.progress * 360) / 100;
69
+ /* 清除画布 */
70
+ ctx.clearRect(0, 0, width, height);
71
+ /* 开始绘制底圆 */
72
+ ctx.beginPath();
73
+ ctx.arc(width / 2, height / 2, this.config.circleRadius, 1, 8);
74
+ ctx.strokeStyle = this.config.lineBgColor;
75
+ ctx.stroke();
76
+ /* 开始绘制动态圆 */
77
+ ctx.beginPath();
78
+ ctx.arc(
79
+ width / 2,
80
+ height / 2,
81
+ this.config.circleRadius,
82
+ -Math.PI / 2,
83
+ (degActive * Math.PI) / 180 - Math.PI / 2
84
+ );
85
+ ctx.strokeStyle = this.config.lineColor;
86
+ ctx.stroke();
87
+ /* 获取百分比 */
88
+ let txt = parseInt(this.config.progress) + "%";
89
+ ctx.font = this.config.fontSize + "px SimHei";
90
+ /* 获取文本宽度 */
91
+ let w = ctx.measureText(txt).width;
92
+ let h = this.config.fontSize / 2;
93
+ ctx.fillStyle = this.config.textColor;
94
+ ctx.fillText(txt, width / 2 - w / 2, height / 2 + h / 2);
95
+ }.bind(this);
96
+ };
97
+
98
+ export { Progress };