@whitesev/utils 1.0.9 → 1.1.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.
@@ -1,10 +1,14 @@
1
- import { Utils } from ".";
1
+ import { Utils } from "./Utils";
2
2
 
3
3
  class LockFunction<K extends (...args: any[]) => any | Promise<any> | void> {
4
4
  #flag: boolean = false;
5
5
  #delayTime: number = 0;
6
6
  #callback: K;
7
7
  #context: typeof Utils;
8
+ lock: () => void;
9
+ unlock: () => void;
10
+ run: (...args: any[]) => Promise<void>;
11
+ isLock: () => boolean;
8
12
  /**
9
13
  * @param callback 需要执行的函数
10
14
  * @param delayTime (可选)延迟xx毫秒后解锁,默认:0
@@ -25,37 +29,38 @@ class LockFunction<K extends (...args: any[]) => any | Promise<any> | void> {
25
29
  this.#delayTime = delayTime as number;
26
30
  this.#context = context;
27
31
  }
28
- }
29
- /**
30
- * 判断是否被锁
31
- */
32
- isLock() {
33
- return this.#flag;
34
- }
35
- /**
36
- *
37
- */
38
- lock() {
39
- this.#flag = true;
40
- }
41
- /**
42
- * 解锁
43
- */
44
- unlock() {
45
- setTimeout(() => {
46
- this.#flag = false;
47
- }, this.#delayTime);
48
- }
49
- /**
50
- * 执行
51
- */
52
- async run(...args: any[]) {
53
- if (this.isLock()) {
54
- return;
55
- }
56
- this.lock();
57
- await this.#callback.apply(this.#context, args);
58
- this.unlock();
32
+
33
+ /**
34
+ *
35
+ */
36
+ this.lock = function () {
37
+ this.#flag = true;
38
+ };
39
+ /**
40
+ * 解锁
41
+ */
42
+ this.unlock = function () {
43
+ setTimeout(() => {
44
+ this.#flag = false;
45
+ }, this.#delayTime);
46
+ };
47
+ /**
48
+ * 判断是否被锁
49
+ */
50
+ this.isLock = function () {
51
+ return this.#flag;
52
+ };
53
+ /**
54
+ * 执行
55
+ */
56
+ this.run = async function (...args: any[]) {
57
+ if (this.isLock()) {
58
+ return;
59
+ }
60
+ this.lock();
61
+ await this.#callback.apply(this.#context, args);
62
+ this.unlock();
63
+ };
59
64
  }
60
65
  }
61
66
 
package/src/Log.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AnyObject } from ".";
1
+ import type { AnyObject } from "./Utils";
2
2
 
3
3
  /** Utils.Log的初始化配置 */
4
4
  declare interface UtilsLogOptions {
@@ -21,11 +21,15 @@ declare interface UtilsLogOptions {
21
21
  }
22
22
 
23
23
  class Log {
24
- /** 前面的TAG标志 */
24
+ /** 是否禁用输出的flag */
25
25
  #disable: boolean = false;
26
- tag: string = "";
26
+ /** 前面的TAG标志 */
27
+ tag: string = "Utils.Log";
28
+ /* 使用的console函数 */
27
29
  #console: Console = null as any;
30
+ /* 当前输出的数量 */
28
31
  #logCount = 0;
32
+ /* 配置 */
29
33
  #details: UtilsLogOptions = {
30
34
  tag: true,
31
35
  successColor: "#0000FF",
@@ -36,10 +40,6 @@ class Log {
36
40
  autoClearConsole: false,
37
41
  logMaxCount: 999,
38
42
  };
39
- /**
40
- * 待恢复的函数或对象
41
- */
42
- #recoveryList = [];
43
43
  #msgColorDetails = [
44
44
  "font-weight: bold; color: cornflowerblue",
45
45
  "font-weight: bold; color: cornflowerblue",
@@ -47,22 +47,27 @@ class Log {
47
47
  "font-weight: bold; color: cornflowerblue",
48
48
  ];
49
49
  /**
50
- * @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}}
50
+ * @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串
51
51
  * @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
52
52
  */
53
53
  constructor(
54
- _GM_info_: {
55
- script: {
56
- name: string;
57
- };
58
- } = {
59
- script: {
60
- name: "Utils.Log",
61
- },
62
- },
63
- console: Console = global.console
54
+ _GM_info_?:
55
+ | {
56
+ script: {
57
+ name: string;
58
+ };
59
+ }
60
+ | string,
61
+ console: Console = globalThis.console
64
62
  ) {
65
- this.tag = _GM_info_.script.name;
63
+ if (typeof _GM_info_ === "string") {
64
+ this.tag = _GM_info_;
65
+ } else if (
66
+ typeof _GM_info_ === "object" &&
67
+ typeof _GM_info_?.script?.name === "string"
68
+ ) {
69
+ this.tag = _GM_info_.script.name;
70
+ }
66
71
  this.#console = console;
67
72
  }
68
73
 
@@ -87,6 +92,7 @@ class Log {
87
92
  if (stackFunctionNamePositionMatch == null) {
88
93
  continue;
89
94
  }
95
+ /* 获取最后一个,因为第一个是包含了at */
90
96
  let stackFunctionName =
91
97
  stackFunctionNameMatch[stackFunctionNameMatch.length - 1];
92
98
  let stackFunctionNamePosition =
@@ -96,10 +102,7 @@ class Log {
96
102
  if (
97
103
  stackFunctionName === "" ||
98
104
  stackFunctionName.match(
99
- new RegExp(
100
- "(^Utils.Log.|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each)",
101
- "g"
102
- )
105
+ /^(Utils\.|)Log(\.|)|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each/g
103
106
  )
104
107
  ) {
105
108
  continue;
@@ -144,7 +147,7 @@ class Log {
144
147
  * @param otherStyle 其它CSS
145
148
  */
146
149
  private printContent(msg: any, color: string, otherStyle?: string) {
147
- this.checkClearConsole.apply(this);
150
+ this.checkClearConsole();
148
151
  otherStyle = otherStyle || "";
149
152
  let stackSplit = new Error()!.stack!.split("\n");
150
153
  stackSplit.splice(0, 2);
@@ -244,7 +247,7 @@ class Log {
244
247
  */
245
248
  table(msg: AnyObject[], color = this.#details.infoColor, otherStyle = "") {
246
249
  if (this.#disable) return;
247
- this.checkClearConsole.apply(this);
250
+ this.checkClearConsole();
248
251
  let stack = new Error()!.stack!.split("\n");
249
252
  stack.splice(0, 1);
250
253
  let errorStackParse = this.parseErrorStack(stack);
package/src/Progress.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Utils } from ".";
1
+ import { Utils } from "./Utils";
2
2
 
3
3
  declare interface ProgressParamConfig {
4
4
  /** canvas元素节点 */
@@ -1,4 +1,4 @@
1
- import { Utils } from ".";
1
+ import { Utils } from "./Utils";
2
2
 
3
3
  export declare interface UtilsTryCatchConfig {
4
4
  log: boolean;
@@ -11,8 +11,9 @@ import { indexedDB } from "./indexedDB";
11
11
  import { LockFunction } from "./LockFunction";
12
12
  import { Log } from "./Log";
13
13
  import { Progress } from "./Progress";
14
- import { TryCatch } from "./tryCatch";
14
+ import { TryCatch } from "./TryCatch";
15
15
  import { UtilsDictionary } from "./Dictionary";
16
+ import type { DOMUtils_EventType } from "./Event";
16
17
 
17
18
  export declare var unsafeWindow: Window & typeof globalThis;
18
19
 
@@ -33,8 +34,8 @@ export type ArgsType<T extends JSTypeNames[]> = {
33
34
  [I in keyof T]: JSTypeMap[T[I]];
34
35
  };
35
36
 
36
- export declare interface UtilsNestedObjectWithToString<V extends any> {
37
- [key: string]: V | UtilsNestedObjectWithToString<V>;
37
+ export declare interface UtilsOwnObject<V extends any> {
38
+ [key: string]: V | UtilsOwnObject<V>;
38
39
  }
39
40
  export declare interface AnyObject {
40
41
  [key: string]: any | AnyObject;
@@ -64,199 +65,9 @@ export declare interface Vue2Context extends AnyObject {
64
65
  $el: Element;
65
66
  }
66
67
 
67
- /**
68
- * 鼠标事件
69
- * + https://blog.csdn.net/weixin_68658847/article/details/126939879
70
- */
71
-
72
- declare interface DOMUtils_MouseEvent {
73
- click: MouseEvent | PointerEvent;
74
- contextmenu: MouseEvent | PointerEvent;
75
- dblclick: MouseEvent | PointerEvent;
76
- mousedown: MouseEvent | PointerEvent;
77
- mouseenter: MouseEvent | PointerEvent;
78
- mouseleave: MouseEvent | PointerEvent;
79
- mousemove: MouseEvent | PointerEvent;
80
- mouseover: MouseEvent | PointerEvent;
81
- mouseout: MouseEvent | PointerEvent;
82
- mouseup: MouseEvent | PointerEvent;
83
- }
84
- declare type DOMUtils_MouseEventType = keyof DOMUtils_MouseEvent;
85
- /**
86
- * 鼠标事件
87
- */
88
- declare interface DOMUtils_KeyboardEvent {
89
- keydown: KeyboardEvent;
90
- keypress: KeyboardEvent;
91
- keyup: KeyboardEvent;
92
- }
93
- declare type DOMUtils_KeyboardEventType = keyof DOMUtils_KeyboardEvent;
94
- /**
95
- * 框架/对象事件
96
- */
97
- declare interface DOMUtils_Frame_Object_Event {
98
- abort: Event;
99
- beforeunload: Event;
100
- error: Event;
101
- hashchange: Event;
102
- load: Event;
103
- pageshow: Event;
104
- pagehide: Event;
105
- resize: Event;
106
- scroll: Event;
107
- unload: Event;
108
- }
109
- declare type DOMUtils_Frame_Object_EventType =
110
- keyof DOMUtils_Frame_Object_Event;
111
- /**
112
- * 表单事件
113
- */
114
- declare interface DOMUtils_FormEvent {
115
- blur: Event;
116
- change: Event;
117
- focus: Event;
118
- focusin: Event;
119
- focusout: Event;
120
- input: Event;
121
- reset: Event;
122
- search: Event;
123
- }
124
- declare type DOMUtils_FormEventType = keyof DOMUtils_FormEvent;
125
-
126
- /**
127
- * 剪贴板事件
128
- */
129
- declare interface DOMUtils_ClipboardEvent {
130
- copy: ClipboardEvent;
131
- cut: ClipboardEvent;
132
- paste: ClipboardEvent;
133
- }
134
- declare type DOMUtils_ClipboardEventType = keyof DOMUtils_ClipboardEvent;
135
-
136
- /**
137
- * 打印事件
138
- */
139
- declare interface DOMUtils_PrintEvent {
140
- afterprint: Event;
141
- beforeprint: Event;
142
- }
143
- declare type DOMUtils_PrintEventType = keyof DOMUtils_PrintEvent;
144
-
145
- /**
146
- * 拖动事件
147
- */
148
- declare interface DOMUtils_DragEvent {
149
- drag: DragEvent;
150
- dragend: DragEvent;
151
- dragenter: DragEvent;
152
- dragleave: DragEvent;
153
- dragover: DragEvent;
154
- dragstart: DragEvent;
155
- drop: DragEvent;
156
- }
157
- declare type DOMUtils_DragEventType = keyof DOMUtils_DragEvent;
158
-
159
- /**
160
- * 多媒体(Media)事件
161
- */
162
- declare interface DOMUtils_MediaEvent {
163
- abort: Event;
164
- canplay: Event;
165
- canplaythrough: Event;
166
- durationchange: Event;
167
- emptied: Event;
168
- ended: Event;
169
- error: Event;
170
- loadeddata: Event;
171
- loadedmetadata: Event;
172
- loadstart: Event;
173
- pause: Event;
174
- play: Event;
175
- playing: Event;
176
- progress: Event;
177
- ratechange: Event;
178
- seeked: Event;
179
- seeking: Event;
180
- stalled: Event;
181
- suspend: Event;
182
- timeupdate: Event;
183
- volumechange: Event;
184
- waiting: Event;
185
- }
186
- declare type DOMUtils_MediaEventType = keyof DOMUtils_MediaEvent;
187
-
188
- /**
189
- * 动画事件
190
- */
191
- declare interface DOMUtils_AnimationEvent {
192
- animationend: AnimationEvent;
193
- animationiteration: AnimationEvent;
194
- animationstart: AnimationEvent;
195
- }
196
- declare type DOMUtils_AnimationEventType = keyof DOMUtils_AnimationEvent;
197
-
198
- /**
199
- * 过渡事件
200
- */
201
- declare interface DOMUtils_TransitionEvent {
202
- transitionend: TransitionEvent;
203
- }
204
- declare type DOMUtils_TransitionEventType = keyof DOMUtils_TransitionEvent;
205
-
206
- /**
207
- * 触摸事件
208
- */
209
- declare interface DOMUtils_TouchEvent {
210
- touchstart: TouchEvent;
211
- touchmove: TouchEvent;
212
- touchend: TouchEvent;
213
- touchcancel: TouchEvent;
214
- touchenter: TouchEvent;
215
- touchleave: TouchEvent;
216
- }
217
- declare type DOMUtils_TouchEventType = keyof DOMUtils_TouchEvent;
218
- /**
219
- * 其它事件
220
- */
221
- declare interface DOMUtils_OtherEvent {
222
- message: Event;
223
- online: Event;
224
- offline: Event;
225
- popstate: Event;
226
- show: Event;
227
- storage: Event;
228
- toggle: Event;
229
- wheel: Event;
230
- propertychange: Event;
231
- fullscreenchange: Event;
232
- DOMContentLoaded: Event;
233
- }
234
- declare type DOMUtils_OtherEventType = keyof DOMUtils_OtherEvent;
235
-
236
- /**
237
- * 全部事件
238
- */
239
- declare type DOMUtils_Event = DOMUtils_MouseEvent &
240
- DOMUtils_KeyboardEvent &
241
- DOMUtils_Frame_Object_Event &
242
- DOMUtils_FormEvent &
243
- DOMUtils_ClipboardEvent &
244
- DOMUtils_PrintEvent &
245
- DOMUtils_DragEvent &
246
- DOMUtils_MediaEvent &
247
- DOMUtils_AnimationEvent &
248
- DOMUtils_TransitionEvent &
249
- DOMUtils_TouchEvent &
250
- DOMUtils_OtherEvent;
251
-
252
- /**
253
- * 事件类型
254
- */
255
- declare type DOMUtils_EventType = keyof DOMUtils_Event;
256
-
257
68
  class Utils {
258
69
  /** 版本号 */
259
- version = "2024.5.25";
70
+ version = "2024.5.28";
260
71
 
261
72
  /**
262
73
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
@@ -697,7 +508,7 @@ class Utils {
697
508
  dispatchEvent(
698
509
  element: HTMLElement | Document,
699
510
  eventName: DOMUtils_EventType | DOMUtils_EventType[],
700
- details?: UtilsNestedObjectWithToString<any>
511
+ details?: UtilsOwnObject<any>
701
512
  ): void;
702
513
  /**
703
514
  * 主动触发事件
@@ -712,12 +523,12 @@ class Utils {
712
523
  dispatchEvent(
713
524
  element: HTMLElement | Document,
714
525
  eventName: string,
715
- details?: UtilsNestedObjectWithToString<any>
526
+ details?: UtilsOwnObject<any>
716
527
  ): void;
717
528
  dispatchEvent(
718
529
  element: HTMLElement | Document,
719
530
  eventName: DOMUtils_EventType | DOMUtils_EventType[] | string,
720
- details?: UtilsNestedObjectWithToString<any>
531
+ details?: UtilsOwnObject<any>
721
532
  ) {
722
533
  let eventNameList: string[] = [];
723
534
  if (typeof eventName === "string") {
@@ -926,7 +737,7 @@ class Utils {
926
737
  }
927
738
  let result = 0;
928
739
  let resultType = "KB";
929
- let sizeData: UtilsNestedObjectWithToString<number> = {};
740
+ let sizeData: UtilsOwnObject<number> = {};
930
741
  sizeData.B = 1;
931
742
  sizeData.KB = 1024;
932
743
  sizeData.MB = sizeData.KB * sizeData.KB;
@@ -1341,7 +1152,7 @@ class Utils {
1341
1152
  * > 456
1342
1153
  */
1343
1154
  getMaxValue(
1344
- val: UtilsNestedObjectWithToString<number>,
1155
+ val: UtilsOwnObject<number>,
1345
1156
  handler: (key: any, value: any) => number
1346
1157
  ): number;
1347
1158
  /**
@@ -1431,7 +1242,7 @@ class Utils {
1431
1242
  * > 123
1432
1243
  */
1433
1244
  getMinValue(
1434
- val: UtilsNestedObjectWithToString<number>,
1245
+ val: UtilsOwnObject<number>,
1435
1246
  handler: (key: any, value: any) => number
1436
1247
  ): number;
1437
1248
  /**
@@ -1441,7 +1252,7 @@ class Utils {
1441
1252
  * > 0
1442
1253
  */
1443
1254
  getMinValue(
1444
- val: UtilsNestedObjectWithToString<number>[],
1255
+ val: UtilsOwnObject<number>[],
1445
1256
  handler: (index: number, value: any) => number
1446
1257
  ): number;
1447
1258
  getMinValue(...args: any[]): number {
@@ -1528,7 +1339,7 @@ class Utils {
1528
1339
  * Utils.getRandomValue({1:"结果1",2:"结果2",3:"结果3"}})
1529
1340
  * > 结果2
1530
1341
  */
1531
- getRandomValue<T extends any>(val: T[] | UtilsNestedObjectWithToString<T>): T;
1342
+ getRandomValue<T extends any>(val: T[] | UtilsOwnObject<T>): T;
1532
1343
  /**
1533
1344
  * 获取两个数之间随机值
1534
1345
  * @example
@@ -1543,8 +1354,8 @@ class Utils {
1543
1354
  * > {1: 1}
1544
1355
  */
1545
1356
  getRandomValue<T extends any>(
1546
- val_1: UtilsNestedObjectWithToString<T>,
1547
- val_2: UtilsNestedObjectWithToString<T>
1357
+ val_1: UtilsOwnObject<T>,
1358
+ val_2: UtilsOwnObject<T>
1548
1359
  ): T;
1549
1360
  getRandomValue(...args: any[]): any {
1550
1361
  let result = [...args];
@@ -2174,7 +1985,7 @@ class Utils {
2174
1985
  return false;
2175
1986
  }
2176
1987
  targetStr = targetStr.toLowerCase();
2177
- const targetCharMap: UtilsNestedObjectWithToString<string> = {};
1988
+ const targetCharMap: UtilsOwnObject<string> = {};
2178
1989
  let targetStrLength = 0;
2179
1990
  for (const char of targetStr) {
2180
1991
  if (Reflect.has(targetCharMap, char)) {
@@ -2691,60 +2502,52 @@ class Utils {
2691
2502
  let default_obverser_config = {
2692
2503
  /* 监听到元素有反馈,需执行的函数 */
2693
2504
  callback: () => {},
2694
- config: {
2505
+ config: <MutationObserverInit>{
2695
2506
  /**
2696
- * @type {boolean|undefined}
2697
2507
  * + true 监听以 target 为根节点的整个子树。包括子树中所有节点的属性,而不仅仅是针对 target
2698
2508
  * + false (默认) 不生效
2699
2509
  */
2700
- subtree: void 0,
2510
+ subtree: void 0 as any as boolean,
2701
2511
  /**
2702
- * @type {boolean|undefined}
2703
2512
  * + true 监听 target 节点中发生的节点的新增与删除(同时,如果 subtree 为 true,会针对整个子树生效)
2704
2513
  * + false (默认) 不生效
2705
2514
  */
2706
- childList: void 0,
2515
+ childList: void 0 as any as boolean,
2707
2516
  /**
2708
- * @type {boolean|undefined}
2709
2517
  * + true 观察所有监听的节点属性值的变化。默认值为 true,当声明了 attributeFilter 或 attributeOldValue
2710
2518
  * + false (默认) 不生效
2711
2519
  */
2712
- attributes: void 0,
2520
+ attributes: void 0 as any as boolean,
2713
2521
  /**
2714
2522
  * 一个用于声明哪些属性名会被监听的数组。如果不声明该属性,所有属性的变化都将触发通知
2715
- * @type {[...string]|undefined}
2716
2523
  */
2717
- attributeFilter: void 0,
2524
+ attributeFilter: void 0 as any as string[],
2718
2525
  /**
2719
- * @type {boolean|undefined}
2720
2526
  * + true 记录上一次被监听的节点的属性变化;可查阅 MutationObserver 中的 Monitoring attribute values 了解关于观察属性变化和属性值记录的详情
2721
2527
  * + false (默认) 不生效
2722
2528
  */
2723
- attributeOldValue: void 0,
2529
+ attributeOldValue: void 0 as any as boolean,
2724
2530
  /**
2725
- * @type {boolean|undefined}
2726
2531
  * + true 监听声明的 target 节点上所有字符的变化。默认值为 true,如果声明了 characterDataOldValue
2727
2532
  * + false (默认) 不生效
2728
2533
  */
2729
- characterData: void 0,
2534
+ characterData: void 0 as any as boolean,
2730
2535
  /**
2731
- * @type {boolean|undefined}
2732
2536
  * + true 记录前一个被监听的节点中发生的文本变化
2733
2537
  * + false (默认) 不生效
2734
2538
  */
2735
- characterDataOldValue: void 0,
2539
+ characterDataOldValue: void 0 as any as boolean,
2736
2540
  },
2737
2541
  };
2738
2542
  observer_config = UtilsContext.assign(
2739
2543
  default_obverser_config,
2740
2544
  observer_config
2741
2545
  );
2742
- let MutationObserver =
2743
- (UtilsCore.window as any).MutationObserver ||
2546
+ let windowMutationObserver =
2547
+ window.MutationObserver ||
2744
2548
  (UtilsCore.window as any).webkitMutationObserver ||
2745
2549
  (UtilsCore.window as any).MozMutationObserver;
2746
- /** @type {MutationObserver} */
2747
- let mutationObserver = new MutationObserver(function (
2550
+ let mutationObserver = new windowMutationObserver(function (
2748
2551
  mutations: MutationRecord[],
2749
2552
  observer: MutationObserver
2750
2553
  ) {
@@ -1,4 +1,4 @@
1
- import { Utils } from ".";
1
+ import { Utils } from "./Utils";
2
2
 
3
3
  declare interface UtilsGMCookieListResult {
4
4
  /** 为 window.location.hostname */
@@ -1,4 +1,4 @@
1
- import { Utils } from ".";
1
+ import { Utils } from "./Utils";
2
2
 
3
3
  declare interface UtilsGMMenuClickCallBackData {
4
4
  /** 菜单键名 */
package/src/indexedDB.ts CHANGED
@@ -1,5 +1,3 @@
1
- import type { AnyObject } from ".";
2
-
3
1
  declare interface UtilsIDBOpenErrorResult {
4
2
  code: number;
5
3
  msg: string;
@@ -104,14 +102,14 @@ class indexedDB {
104
102
  };
105
103
  request.onsuccess = function (event: Event) {
106
104
  if (!that.#db[dbName]) {
107
- let target = event.target as IDBRequest;
105
+ let target = event.target as IDBRequest;
108
106
  that.#db[dbName] = target.result;
109
107
  }
110
108
  let store = that.createStore(dbName);
111
109
  callback(store, true);
112
110
  };
113
111
  request.onupgradeneeded = function (event: Event) {
114
- let target = event.target as IDBRequest;
112
+ let target = event.target as IDBRequest;
115
113
  that.#db[dbName] = target.result;
116
114
  let store = that.#db[dbName].createObjectStore(that.#storeName, {
117
115
  keyPath: "key",
File without changes