@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,1560 @@
1
+ import { ColorConversion } from "./ColorConversion";
2
+ import { UtilsDictionary } from "./Dictionary";
3
+ import { GBKEncoder } from "./GBKEncoder";
4
+ import { UtilsGMCookie } from "./UtilsGMCookie";
5
+ declare class Utils {
6
+ /** 版本号 */
7
+ version: string;
8
+ /**
9
+ * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
10
+ * @param cssText css字符串
11
+ * @returns 返回添加的CSS标签
12
+ * @example
13
+ * Utils.GM_addStyle("html{}");
14
+ * > <style type="text/css">html{}</style>
15
+ */
16
+ addStyle(cssText: string): HTMLStyleElement;
17
+ /**
18
+ * JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
19
+ * @param target 目标数据
20
+ * @param source 源数据
21
+ * @param isAdd 是否可以追加键,默认false
22
+ * @example
23
+ * Utils.assign({"1":1,"2":{"3":3}}, {"2":{"3":4}});
24
+ * >
25
+ * {
26
+ "1": 1,
27
+ "2": {
28
+ "3": 4
29
+ }
30
+ }
31
+ */
32
+ assign<T1, T2 extends object, T3 extends boolean>(target: T1, source: T2, isAdd?: T3): T3 extends true ? T1 & T2 : T1;
33
+ /**
34
+ * 异步替换字符串
35
+ * @param string 需要被替换的目标字符串
36
+ * @param pattern 正则匹配模型
37
+ * @param asyncFn 异步获取的函数
38
+ */
39
+ asyncReplaceAll(string: string, pattern: RegExp | string, asyncFn: (item: string) => Promise<string>): Promise<string>;
40
+ /**
41
+ * ajax劫持库,支持xhr和fetch劫持。
42
+ * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
43
+ * + 作者:cxxjackie
44
+ * + 版本:1.4.1
45
+ * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
46
+ */
47
+ ajaxHooker: UtilsAjaxHookResult;
48
+ /**
49
+ * 根据坐标点击canvas元素的内部位置
50
+ * @param canvasElement 画布元素
51
+ * @param clientX X坐标,默认值0
52
+ * @param clientY Y坐标,默认值0
53
+ * @param view 触发的事件目标
54
+ */
55
+ canvasClickByPosition(canvasElement: HTMLCanvasElement, clientX?: number | string, clientY?: number | string, view?: Window & typeof globalThis): void;
56
+ /**
57
+ * 【手机】检测点击的地方是否在该元素区域内
58
+ * @param element 需要检测的元素
59
+ * @returns
60
+ * + true 点击在元素上
61
+ * + false 未点击在元素上
62
+ * @example
63
+ * Utils.checkUserClickInNode(document.querySelector(".xxx"));
64
+ * > false
65
+ **/
66
+ checkUserClickInNode(element: Element | Node | HTMLElement): boolean;
67
+ /**
68
+ * 复制formData数据
69
+ * @param formData 需要clone的数据
70
+ */
71
+ cloneFormData<T extends FormData>(formData: T): T;
72
+ /**
73
+ * 函数重载实现
74
+ * @example
75
+ * let getUsers = Utils.createOverload();
76
+ * getUsers.addImpl("",()=>{
77
+ * console.log("无参数");
78
+ * });
79
+ *
80
+ * getUsers.addImpl("boolean",()=>{
81
+ * console.log("boolean");
82
+ * });
83
+ *
84
+ * getUsers.addImpl("string",()=>{
85
+ * console.log("string");
86
+ * });
87
+ *
88
+ * getUsers.addImpl("number","string",()=>{
89
+ * console.log("number string");
90
+ * });
91
+ */
92
+ createOverload(): {
93
+ /**
94
+ * 前面的参数都是字符串,最后一个参数是函数
95
+ */
96
+ addImpl<T extends JSTypeNames[]>(...args: [...T, (...args: ArgsType<T>) => any]): void;
97
+ };
98
+ /**
99
+ * 颜色转换
100
+ * @returns
101
+ */
102
+ ColorConversion(): ColorConversion;
103
+ /**
104
+ * 深拷贝
105
+ * @param obj 对象
106
+ */
107
+ deepClone<T extends object | undefined | null>(obj?: T): T;
108
+ /**
109
+ * 防抖函数
110
+ * @param fn 需要触发的回调
111
+ * @param delay 防抖判定时间(毫秒),默认是0ms
112
+ */
113
+ debounce<A extends any[], R>(fn: (...args: A) => R, delay?: number): (...args: A) => void;
114
+ /**
115
+ * 删除某个父元素,父元素可能在上层或上上层或上上上层...
116
+ * @param element 当前元素
117
+ * @param targetSelector 判断是否满足父元素,参数为当前处理的父元素,满足返回true,否则false
118
+ * @returns
119
+ * + true 已删除
120
+ * + false 未删除
121
+ * @example
122
+ * Utils.deleteParentNode(document.querySelector("a"),".xxx");
123
+ * > true
124
+ **/
125
+ deleteParentNode(element: Node | HTMLElement | Element | null, targetSelector: string): boolean;
126
+ /**
127
+ * 字典
128
+ * @example
129
+ * let dictionary = new Utils.Dictionary();
130
+ * let dictionary2 = new Utils.Dictionary();
131
+ * dictionary.set("test","111");
132
+ * dictionary.get("test");
133
+ * > '111'
134
+ * dictionary.has("test");
135
+ * > true
136
+ * dictionary.concat(dictionary2);
137
+ **/
138
+ Dictionary: typeof UtilsDictionary;
139
+ /**
140
+ * 主动触发事件
141
+ * @param element 元素
142
+ * @param eventName 事件名称,可以是字符串,也可是字符串格式的列表
143
+ * @param details (可选)赋予触发的Event的额外属性
144
+ * + true 使用Proxy代理Event并设置获取isTrusted永远为True
145
+ * + false (默认) 不对Event进行Proxy代理
146
+ * @example
147
+ * Utils.dispatchEvent(document.querySelector("input","input"))
148
+ */
149
+ dispatchEvent(element: HTMLElement | Document, eventName: DOMUtils_EventType | DOMUtils_EventType[], details?: UtilsNestedObjectWithToString<any>): void;
150
+ /**
151
+ * 主动触发事件
152
+ * @param element 元素
153
+ * @param eventName 事件名称,可以是字符串,也可是字符串格式的列表
154
+ * @param details (可选)赋予触发的Event的额外属性
155
+ * + true 使用Proxy代理Event并设置获取isTrusted永远为True
156
+ * + false (默认) 不对Event进行Proxy代理
157
+ * @example
158
+ * Utils.dispatchEvent(document.querySelector("input","input"))
159
+ */
160
+ dispatchEvent(element: HTMLElement | Document, eventName: string, details?: UtilsNestedObjectWithToString<any>): void;
161
+ /**
162
+ * 下载base64格式的数据
163
+ * @param base64Data 需要转换的base64数据
164
+ * @param fileName 需要保存的文件名
165
+ * @param isIFrame (可选)是否使用iframe进行下载
166
+ * @example
167
+ * Utils.downloadBase64("data:image/jpeg:base64/,xxxxxx");
168
+ **/
169
+ downloadBase64(base64Data: string, fileName: string, isIFrame?: boolean): void;
170
+ /**
171
+ * 选中页面中的文字,类似Ctrl+F的选中
172
+ * @param str (可选)需要寻找的字符串,默认为空
173
+ * @param caseSensitive(可选)默认false
174
+ * + true 区分大小写
175
+ * + false (默认) 不区分大小写
176
+ * @returns
177
+ * + true 找到
178
+ * + false 未找到
179
+ * + undefined 不可使用该Api
180
+ * @example
181
+ * Utils.findWebPageVisibleText("xxxxx");
182
+ * > true
183
+ **/
184
+ findWebPageVisibleText(str?: string, caseSensitive?: boolean): boolean | void;
185
+ /**
186
+ * 定位元素上的字符串,返回一个迭代器
187
+ * @param element 目标元素
188
+ * @param text 需要定位的字符串
189
+ * @param filter (可选)过滤器函数,返回值为true是排除该元素
190
+ * @example
191
+ * let textIterator = Utils.findElementsWithText(document.documentElement,"xxxx");
192
+ * textIterator.next();
193
+ * > {value: ?HTMLElement, done: boolean, next: Function}
194
+ */
195
+ findElementsWithText<T extends HTMLElement | Element | Node>(element: T, text: string, filter?: (element: T) => boolean): Generator<HTMLElement | ChildNode, void, any>;
196
+ /**
197
+ * 判断该元素是否可见,如果不可见,向上找它的父元素直至找到可见的元素
198
+ * @param element
199
+ * @example
200
+ * let visibleElement = Utils.findVisibleElement(document.querySelector("a.xx"));
201
+ * > <HTMLElement>
202
+ */
203
+ findVisibleElement(element: HTMLElement | Element | Node): HTMLElement | null;
204
+ /**
205
+ * 格式化byte为KB、MB、GB、TB、PB、EB、ZB、YB、BB、NB、DB
206
+ * @param byteSize 字节
207
+ * @param addType (可选)是否添加单位
208
+ * + true (默认) 添加单位
209
+ * + false 不添加单位
210
+ * @returns
211
+ * + {string} 当addType为true时,且保留小数点末尾2位
212
+ * + {number} 当addType为false时,且保留小数点末尾2位
213
+ * @example
214
+ * Utils.formatByteToSize("812304");
215
+ * > '793.27KB'
216
+ * @example
217
+ * Utils.formatByteToSize("812304",false);
218
+ * > 793.27
219
+ **/
220
+ formatByteToSize<T extends boolean>(byteSize: number | string, addType?: T): T extends true ? string : number;
221
+ /**
222
+ * 应用场景: 当你想要获取数组形式的元素时,它可能是其它的选择器,那么需要按照先后顺序填入参数
223
+ * 第一个是优先级最高的,依次下降,如果都没有,返回空列表
224
+ * 支持document.querySelectorAll、$("")、()=>{return document.querySelectorAll("")}
225
+ * @param NodeList
226
+ * @example
227
+ * Utils.getNodeListValue(
228
+ * document.querySelectorAll("div.xxx"),
229
+ * document.querySelectorAll("a.xxx")
230
+ * );
231
+ * > [...div,div,div]
232
+ * @example
233
+ * Utils.getNodeListValue(divGetFunction,aGetFunction);
234
+ * > [...div,div,div]
235
+ */
236
+ getNodeListValue(...args: (NodeList | (() => HTMLElement))[]): HTMLElement[];
237
+ /**
238
+ * 自动判断N个参数,获取非空的值,如果都是空,返回最后一个值
239
+ */
240
+ getNonNullValue(...args: any[]): any;
241
+ /**
242
+ * 获取格式化后的时间
243
+ * @param text (可选)需要格式化的字符串或者时间戳,默认:new Date()
244
+ * @param formatType (可选)格式化成的显示类型,默认:yyyy-MM-dd HH:mm:ss
245
+ * + yyyy 年
246
+ * + MM 月
247
+ * + dd 天
248
+ * + HH 时 (24小时制)
249
+ * + hh 时 (12小时制)
250
+ * + mm 分
251
+ * + ss 秒
252
+ * @returns {string} 返回格式化后的时间
253
+ * @example
254
+ * Utils.formatTime("2022-08-21 23:59:00","HH:mm:ss");
255
+ * > '23:59:00'
256
+ * @example
257
+ * Utils.formatTime(1899187424988,"HH:mm:ss");
258
+ * > '15:10:13'
259
+ * @example
260
+ * Utils.formatTime()
261
+ * > '2023-1-1 00:00:00'
262
+ **/
263
+ formatTime(text?: string | number | Date, formatType?: "yyyy-MM-dd HH:mm:ss" | "yyyy/MM/dd HH:mm:ss" | "yyyy年MM月dd日 HH时mm分ss秒" | "yyyyMMdd" | "yyyy_MM_dd_HH_mm_ss" | "yyyy年MM月dd日 hh:mm:ss" | "yyyy-MM-dd" | "HH:mm:ss"): string;
264
+ /**
265
+ * 字符串格式的时间转时间戳
266
+ * @param text 字符串格式的时间,例如:
267
+ * + 2022-11-21 00:00:00
268
+ * + 00:00:00
269
+ * @returns 返回时间戳
270
+ * @example
271
+ * Utils.formatToTimeStamp("2022-11-21 00:00:00");
272
+ * > 1668960000000
273
+ **/
274
+ formatToTimeStamp(text: string): number;
275
+ /**
276
+ * gbk格式的url编码,来自https://greasyfork.org/zh-CN/scripts/427726-gbk-url-js
277
+ * @example
278
+ * let gbkEncoder = new Utils.GBKEncoder();
279
+ * gbkEncoder.encode("测试");
280
+ * > '%B2%E2%CA%D4'
281
+ * gbkEncoder.decode("%B2%E2%CA%D4");
282
+ * > 测试
283
+ */
284
+ GBKEncoder: typeof GBKEncoder;
285
+ /**
286
+ * 获取 transitionend 的在各个浏览器的兼容名
287
+ */
288
+ getTransitionEndNameList(): string[];
289
+ /**
290
+ * 获取 animationend 的在各个浏览器的兼容名
291
+ */
292
+ getAnimationEndNameList(): string[];
293
+ /**
294
+ * 获取NodeList或Array对象中的最后一个的值
295
+ * @param targetObj
296
+ * @returns
297
+ * @example
298
+ * Utils.getArrayLastValue(document.querySelectorAll("div"));
299
+ * > div
300
+ * @example
301
+ * Utils.getArrayLastValue([1,2,3,4,5]);
302
+ * > 5
303
+ */
304
+ getArrayLastValue<R extends any>(targetObj: NodeList | any[]): R;
305
+ /**
306
+ * 应用场景: 当想获取的元素可能是不同的选择器的时候,按顺序优先级获取
307
+ * 参数类型可以是Element或者是Function
308
+ * @returns 如果都没有的话,返回null
309
+ * @example
310
+ * // 如果a.aaa不存在的话,取a.bbb,这里假设a.aaa不存在
311
+ * Utils.getArrayRealValue(document.querySelector("a.aaa"),document.querySelector("a.bbb"));
312
+ * > a.bbb
313
+ * @example
314
+ * Utils.getArrayRealValue(()=>{return document.querySelector("a.aaa").href},()=>{document.querySelector("a.bbb").getAttribute("data-href")});
315
+ * > javascript:;
316
+ */
317
+ getArrayRealValue(...args: (NodeList | (() => HTMLElement))[]): any;
318
+ /**
319
+ * 获取天数差异,如何获取某个时间与另一个时间相差的天数
320
+ * @param timestamp1 (可选)时间戳(毫秒|秒),不区分哪个更大,默认为:Date.now()
321
+ * @param timestamp2 (可选)时间戳(毫秒|秒),不区分哪个更大,默认为:Date.now()
322
+ * @param type (可选)返回的数字的表达的类型,比如:年、月、天、时、分、秒、auto,默认天
323
+ * @example
324
+ * Utils.getDaysDifference(new Date().getTime());
325
+ * > 0
326
+ * @example
327
+ * Utils.getDaysDifference(new Date().getTime(),undefined,"秒");
328
+ * > 0
329
+ */
330
+ getDaysDifference(timestamp1?: number, timestamp2?: number, type?: "auto"): string;
331
+ /**
332
+ * 获取天数差异,如何获取某个时间与另一个时间相差的天数
333
+ * @param timestamp1 (可选)时间戳(毫秒|秒),不区分哪个更大,默认为:Date.now()
334
+ * @param timestamp2 (可选)时间戳(毫秒|秒),不区分哪个更大,默认为:Date.now()
335
+ * @param type (可选)返回的数字的表达的类型,比如:年、月、天、时、分、秒、auto,默认天
336
+ * @example
337
+ * Utils.getDaysDifference(new Date().getTime());
338
+ * > 0
339
+ * @example
340
+ * Utils.getDaysDifference(new Date().getTime(),undefined,"秒");
341
+ * > 0
342
+ */
343
+ getDaysDifference(timestamp1?: number, timestamp2?: number, type?: "年" | "月" | "天" | "时" | "分" | "秒"): number;
344
+ /**
345
+ * 获取元素的选择器字符串
346
+ * @param element
347
+ * @example
348
+ * Utils.getElementSelector(document.querySelector("a"))
349
+ * > '.....'
350
+ */
351
+ getElementSelector(element: HTMLElement): string;
352
+ /**
353
+ * 获取最大值
354
+ * @example
355
+ * Utils.getMaxValue(1,3,5,7,9)
356
+ * > 9
357
+ */
358
+ getMaxValue(...args: number[]): number;
359
+ /**
360
+ * 获取最大值
361
+ * @example
362
+ * Utils.getMaxValue([1,3,5])
363
+ * > 5
364
+ */
365
+ getMaxValue(val: number[]): number;
366
+ /**
367
+ * 获取最大值
368
+ * @example
369
+ * Utils.getMaxValue({1:123,2:345,3:456},(key,value)=>{return parseInt(value)})
370
+ * > 456
371
+ */
372
+ getMaxValue(val: UtilsNestedObjectWithToString<number>, handler: (key: any, value: any) => number): number;
373
+ /**
374
+ * 获取页面中最大的z-index
375
+ * @param deviation 获取最大的z-index值的偏移,默认是+1
376
+ * @example
377
+ * Utils.getMaxZIndex();
378
+ * > 1001
379
+ **/
380
+ getMaxZIndex(deviation?: number): number;
381
+ /**
382
+ * 获取最小值
383
+ * @example
384
+ * Utils.getMinValue(1,3,5,7,9)
385
+ * > 1
386
+ */
387
+ getMinValue(...args: number[]): number;
388
+ /**
389
+ * 获取最小值
390
+ * @example
391
+ * Utils.getMinValue([1,3,5])
392
+ * > 1
393
+ */
394
+ getMinValue(val: number[]): number;
395
+ /**
396
+ * 获取最小值
397
+ * @example
398
+ * Utils.getMinValue({1:123,2:345,3:456},(key,value)=>{return parseInt(value)})
399
+ * > 123
400
+ */
401
+ getMinValue(val: UtilsNestedObjectWithToString<number>, handler: (key: any, value: any) => number): number;
402
+ /**
403
+ * 获取最小值
404
+ * @example
405
+ * Utils.getMinValue([{1:123},{2:345},{3:456}],(index,value)=>{return parseInt(index)})
406
+ * > 0
407
+ */
408
+ getMinValue(val: UtilsNestedObjectWithToString<number>[], handler: (index: number, value: any) => number): number;
409
+ /**
410
+ * 获取随机的安卓手机User-Agent
411
+ * @example
412
+ * Utils.getRandomAndroidUA();
413
+ * > 'Mozilla/5.0 (Linux; Android 10; MI 13 Build/OPR1.170623.027; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.3490.40 Mobile Safari/537.36'
414
+ **/
415
+ getRandomAndroidUA(): string;
416
+ /**
417
+ * 获取随机值
418
+ * @example
419
+ * Utils.getRandomValue(1,9,6,99)
420
+ * > 6
421
+ */
422
+ getRandomValue<T extends any>(...args: T[]): T;
423
+ /**
424
+ * 获取随机值
425
+ * @example
426
+ * Utils.getRandomValue([1,2,3])
427
+ * > 3
428
+ * @example
429
+ * Utils.getRandomValue({1:"结果1",2:"结果2",3:"结果3"}})
430
+ * > 结果2
431
+ */
432
+ getRandomValue<T extends any>(val: T[] | UtilsNestedObjectWithToString<T>): T;
433
+ /**
434
+ * 获取两个数之间随机值
435
+ * @example
436
+ * Utils.getRandomValue(1,9)
437
+ * > 6
438
+ */
439
+ getRandomValue(val_1: number, val_2: number): number;
440
+ /**
441
+ * 获取随机值
442
+ * @example
443
+ * Utils.getRandomValue({1:1},{2:2})
444
+ * > {1: 1}
445
+ */
446
+ getRandomValue<T extends any>(val_1: UtilsNestedObjectWithToString<T>, val_2: UtilsNestedObjectWithToString<T>): T;
447
+ /**
448
+ * 获取随机的电脑端User-Agent
449
+ * + Mozilla/5.0:以前用于Netscape浏览器,目前大多数浏览器UA都会带有
450
+ * + Windows NT 13:代表Window11系统
451
+ * + Windows NT 10.0:代表Window10系统
452
+ * + Windows NT 6.1:代表windows7系统
453
+ * + WOW64:Windows-on-Windows 64-bit,32位的应用程序运行于此64位处理器上
454
+ * + Win64:64位
455
+ * + AppleWebKit/537.36:浏览器内核
456
+ * + KHTML:HTML排版引擎
457
+ * + like Gecko:这不是Geckeo 浏览器,但是运行起来像Geckeo浏览器
458
+ * + Chrome/106.0.5068.19:Chrome版本号
459
+ * + Safari/537.36:宣称自己是Safari?
460
+ * @returns 返回随机字符串
461
+ * @example
462
+ * Utils.getRandomPCUA();
463
+ * > 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.5068.19 Safari/537.36'
464
+ **/
465
+ getRandomPCUA(): string;
466
+ /**
467
+ * 获取元素上的使用React框架的实例属性,目前包括reactFiber、reactProps、reactEvents、reactEventHandlers、reactInternalInstance
468
+ * @param element 需要获取的目标元素
469
+ * @returns
470
+ * @example
471
+ * Utils.getReactObj(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
472
+ */
473
+ getReactObj(element: HTMLElement | Element): {
474
+ reactFiber?: AnyObject;
475
+ reactProps?: AnyObject;
476
+ reactEvents?: AnyObject;
477
+ reactEventHandlers?: AnyObject;
478
+ reactInternalInstance?: AnyObject;
479
+ reactContainer?: AnyObject;
480
+ };
481
+ /**
482
+ * 获取对象上的Symbol属性,如果没设置keyName,那么返回一个对象,对象是所有遍历到的Symbol对象
483
+ * @param target 目标对象
484
+ * @param keyName (可选)Symbol名或者Symbol对象
485
+ */
486
+ getSymbol(target: any, keyName?: string | symbol): any;
487
+ /**
488
+ * 获取文本的字符长度
489
+ * @param text
490
+ * @example
491
+ * Utils.getTextLength("测试文本")
492
+ * > 12
493
+ */
494
+ getTextLength(text: string): number;
495
+ /**
496
+ * 获取文本占据的空间大小,返回自动的单位,如12 Kb,14 K,20 MB,1 GB
497
+ * @param text 目标字符串
498
+ * @param addType (可选)是否添加单位
499
+ * + true (默认) 自动添加单位
500
+ * + false 不添加单位
501
+ * @example
502
+ * Utils.getTextStorageSize("测试文本");
503
+ * > '12.00B'
504
+ */
505
+ getTextStorageSize<T extends boolean>(text: string, addType?: T): T extends true ? string : number;
506
+ /**
507
+ * 获取迅雷协议的Url
508
+ * @param url Url链接或者其它信息
509
+ */
510
+ getThunderUrl(url: string): string;
511
+ /**
512
+ * 对于GM_cookie的兼容写法,当无法使用GM_cookie时可以使用这个,但是并不完全兼容,有些写不出来且限制了httponly是无法访问的
513
+ * @example
514
+ let GM_cookie = new Utils.GM_Cookie();
515
+ GM_cookie.list({name:"xxx_cookie_xxx"},function(cookies,error){
516
+ if (!error) {
517
+ console.log(cookies);
518
+ console.log(cookies.value);
519
+ } else {
520
+ console.error(error);
521
+ }
522
+ });
523
+ GM_cookie.set({name:"xxx_cookie_test_xxx",value:"这是Cookie测试值"},function(error){
524
+ if (error) {
525
+ console.error(error);
526
+ } else {
527
+ console.log('Cookie set successfully.');
528
+ }
529
+ })
530
+ GM_cookie.delete({name:"xxx_cookie_test_xxx"},function(error){
531
+ if (error) {
532
+ console.error(error);
533
+ } else {
534
+ console.log('Cookie set successfully.');
535
+ }
536
+ })
537
+ **/
538
+ GM_Cookie: typeof UtilsGMCookie;
539
+ /**
540
+ * 注册油猴菜单,要求本地存储的键名不能存在其它键名`GM_Menu_Local_Map`会冲突/覆盖
541
+ * @example
542
+ let GM_Menu = new Utils.GM_Menu({
543
+ data: [
544
+ {
545
+ menu_key: "menu_key",
546
+ text: "测试按钮",
547
+ enable: true,
548
+ accessKey: "a",
549
+ autoClose: false,
550
+ showText(text, enable) {
551
+ return "[" + (enable ? "√" : "×") + "]" + text;
552
+ },
553
+ callback(data) {
554
+ console.log("点击菜单,值修改为", data.enable);
555
+ },
556
+ },
557
+ ],
558
+ autoReload: false,
559
+ GM_getValue,
560
+ GM_setValue,
561
+ GM_registerMenuCommand,
562
+ GM_unregisterMenuCommand,
563
+ });
564
+
565
+
566
+ // 获取某个菜单项的值
567
+ GM_Menu.get("menu_key");
568
+ > true
569
+
570
+ // 获取某个菜单项的开启/关闭后显示的文本
571
+ GM_Menu.getShowTextValue("menu_key");
572
+ > √测试按钮
573
+
574
+ // 添加键为menu_key2的菜单项
575
+ GM_Menu.add({
576
+ key:"menu_key2",
577
+ text: "测试按钮2",
578
+ enable: false,
579
+ showText(text,enable){
580
+ return "[" + (enable ? "√" : "×") + "]" + text;
581
+ },
582
+ callback(data){
583
+ console.log("点击菜单,值修改为",data.enable);
584
+ }
585
+ });
586
+ // 使用数组的方式添加多个菜单,如menu_key3、menu_key4
587
+ GM_Menu.add([
588
+ {
589
+ key:"menu_key3",
590
+ text: "测试按钮3",
591
+ enable: false,
592
+ showText(text,enable){
593
+ return "[" + (enable ? "√" : "×") + "]" + text;
594
+ },
595
+ callback(data){
596
+ console.log("点击菜单,值修改为",data.enable);
597
+ }
598
+ },
599
+ {
600
+ key:"menu_key4",
601
+ text: "测试按钮4",
602
+ enable: false,
603
+ showText(text,enable){
604
+ return "[" + (enable ? "√" : "×") + "]" + text;
605
+ },
606
+ callback(data){
607
+ console.log("点击菜单,值修改为",data.enable);
608
+ }
609
+ }
610
+ ]);
611
+
612
+ // 更新键为menu_key的显示文字和点击回调
613
+ GM_Menu.update({
614
+ menu_key:{
615
+ text: "更新后的测试按钮",
616
+ enable: true,
617
+ showText(text,enable){
618
+ return "[" + (enable ? "√" : "×") + "]" + text;
619
+ },
620
+ callback(data){
621
+ console.log("点击菜单更新后的测试按钮,新值修改为",data.enable);
622
+ }
623
+ }
624
+ });
625
+
626
+ // 删除键为menu_key的菜单
627
+ GM_Menu.delete("menu_key");
628
+ **/
629
+ GM_Menu: UtilsGMMenu;
630
+ /**
631
+ * 基于Function prototype,能够勾住和释放任何函数
632
+ *
633
+ * .hook
634
+ * + realFunc {string} 用于保存原始函数的函数名称,用于unHook
635
+ * + hookFunc {string} 替换的hook函数
636
+ * + context {object} 目标函数所在对象,用于hook非window对象下的函数,如String.protype.slice,carInstance1
637
+ * + methodName {string} 匿名函数需显式传入目标函数名eg:this.Begin = function(){....};}
638
+ *
639
+ * .unhook
640
+ * + realFunc {string} 用于保存原始函数的函数名称,用于unHook
641
+ * + funcName {string} 被Hook的函数名称
642
+ * + context {object} 目标函数所在对象,用于hook非window对象下的函数,如String.protype.slice,carInstance1
643
+ * @example
644
+ let hook = new Utils.Hooks();
645
+ hook.initEnv();
646
+ function myFunction(){
647
+ console.log("我自己需要执行的函数");
648
+ }
649
+ function testFunction(){
650
+ console.log("正常执行的函数");
651
+ }
652
+ testFunction.hook(testFunction,myFunction,window);
653
+ **/
654
+ Hooks: UtilsHooks;
655
+ /**
656
+ * 为减少代码量和回调,把GM_xmlhttpRequest封装
657
+ * 文档地址: https://www.tampermonkey.net/documentation.php?ext=iikm
658
+ * 其中onloadstart、onprogress、onreadystatechange是回调形式,onabort、ontimeout、onerror可以设置全局回调函数
659
+ * @param _GM_xmlHttpRequest_ 油猴中的GM_xmlhttpRequest
660
+ * @example
661
+ let httpx = new Utils.Httpx(GM_xmlhttpRequest);
662
+ let postResp = await httpx.post({
663
+ url:url,
664
+ data:JSON.stringify({
665
+ test:1
666
+ }),
667
+ timeout: 5000
668
+ });
669
+ console.log(postResp);
670
+ > {
671
+ status: true,
672
+ data: {responseText: "...", response: xxx,...},
673
+ msg: "请求完毕",
674
+ type: "onload",
675
+ }
676
+
677
+ if(postResp === "onload" && postResp.status){
678
+ // onload
679
+ }else if(postResp === "ontimeout"){
680
+ // ontimeout
681
+ }
682
+ * @example
683
+ // 也可以先配置全局参数
684
+ let httpx = new Utils.Httpx(GM_xmlhttpRequest);
685
+ httpx.config({
686
+ timeout: 5000,
687
+ async: false,
688
+ responseType: "html",
689
+ redirect: "follow",
690
+ })
691
+ // 优先级为 默认details < 全局details < 单独的details
692
+ */
693
+ Httpx: UtilsHttpx;
694
+ /**
695
+ * 浏览器端的indexedDB操作封装
696
+ * @example
697
+ let db = new Utils.indexedDB('web_DB', 'nav_text')
698
+ let data = {name:'管理员', roleId: 1, type: 1};
699
+ db.save('list',data).then((resolve)=>{
700
+ console.log(resolve,'存储成功')
701
+ })
702
+
703
+ db.get('list').then((resolve)=>{
704
+ console.log(resolve,'查询成功')
705
+ })
706
+
707
+ db.getPaging('list',20,10).then((resolve)=>{
708
+ console.log(resolve,'查询分页偏移第20,一共10行成功');
709
+ })
710
+
711
+ db.delete('list').then(resolve=>{
712
+ console.log(resolve,'删除成功---->>>>>>name')
713
+ })
714
+
715
+ db.deleteAll().then(resolve=>{
716
+ console.log(resolve,'清除数据库---->>>>>>name')
717
+ })
718
+ **/
719
+ indexedDB: UtilsIndexedDB;
720
+ /**
721
+ * 判断目标函数是否是Native Code
722
+ * @param target
723
+ * @returns
724
+ * + true 是Native
725
+ * + false 不是Native
726
+ * @example
727
+ * Utils.isNativeFunc(window.location.assign)
728
+ * > true
729
+ */
730
+ isNativeFunc(target: Function): boolean;
731
+ /**
732
+ * 判断当前的位置是否位于页面底部附近
733
+ * @param nearValue (可选)判断在页面底部的误差值,默认:50
734
+ * @returns
735
+ * + true 在底部附近
736
+ * + false 不在底部附近
737
+ */
738
+ isNearBottom(nearValue?: number): boolean;
739
+ /**
740
+ * 判断对象是否是元素
741
+ * @param target
742
+ * @returns
743
+ * + true 是元素
744
+ * + false 不是元素
745
+ * @example
746
+ * Utils.isDOM(document.querySelector("a"))
747
+ * > true
748
+ */
749
+ isDOM(target: any): boolean;
750
+ /**
751
+ * 判断浏览器是否支持全屏
752
+ */
753
+ isFullscreenEnabled(): boolean;
754
+ /**
755
+ * 判断对象是否是jQuery对象
756
+ * @param target
757
+ * @returns
758
+ * + true 是jQuery对象
759
+ * + false 不是jQuery对象
760
+ * @example
761
+ * Utils.isJQuery($("a"))
762
+ * > true
763
+ */
764
+ isJQuery(target: any): boolean;
765
+ /**
766
+ * 判断当前设备是否是移动端
767
+ * @param userAgent (可选)UA字符串,默认使用当前的navigator.userAgent
768
+ * @returns
769
+ * + true 是移动端
770
+ * + false 不是移动端
771
+ * @example
772
+ * Utils.isPhone();
773
+ * > true
774
+ **/
775
+ isPhone(userAgent?: string): boolean;
776
+ /**
777
+ * 判断传递的字符串是否是由相同的字符组成
778
+ * @param targetStr 需要判断的字符串,长度(.length)需要≥2
779
+ * @param coefficient 系数(默认:1),某个字符重复的系数大于它那么就是返回true,默认全部
780
+ */
781
+ isSameChars(targetStr: string, coefficient?: number): boolean;
782
+ /**
783
+ * 判断对象是否不为空
784
+ * @returns {boolean}
785
+ * + true 不为空
786
+ * + false 为空
787
+ * @example
788
+ * Utils.isNotNull("123");
789
+ * > true
790
+ */
791
+ isNotNull(...args: any[]): boolean;
792
+ /**
793
+ * 判断对象或数据是否为空
794
+ * + `String`判空的值,如 ""、"null"、"undefined"、" "
795
+ * + `Number`判空的值,如 0
796
+ * + `Object`判空的值,如 {}、null、undefined
797
+ * + `Array`(存在属性Symbol.iterator)判空的值,如 []
798
+ * + `Boolean`判空的值,如false
799
+ * + `Function`判空的值,如()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){}
800
+ * @returns
801
+ * + true 为空
802
+ * + false 不为空
803
+ * @example
804
+ Utils.isNull({});
805
+ > true
806
+ * @example
807
+ Utils.isNull([]);
808
+ > true
809
+ * @example
810
+ Utils.isNull(" ");
811
+ > true
812
+ * @example
813
+ Utils.isNull(function(){});
814
+ > true
815
+ * @example
816
+ Utils.isNull(()=>{}));
817
+ > true
818
+ * @example
819
+ Utils.isNull("undefined");
820
+ > true
821
+ * @example
822
+ Utils.isNull("null");
823
+ > true
824
+ * @example
825
+ Utils.isNull(" ", false);
826
+ > true
827
+ * @example
828
+ Utils.isNull([1],[]);
829
+ > false
830
+ * @example
831
+ Utils.isNull([],[1]);
832
+ > false
833
+ * @example
834
+ Utils.isNull(false,[123]);
835
+ > false
836
+ **/
837
+ isNull(...args: any[]): boolean;
838
+ /**
839
+ * 判断浏览器主题是否是暗黑|深色模式
840
+ */
841
+ isThemeDark(): boolean;
842
+ /**
843
+ * 判断元素是否在页面中可见
844
+ * @param element 需要检查的元素,可以是普通元素|数组形式的元素|通过querySelectorAll获取的元素数组
845
+ * @param inView
846
+ * + true 在窗口可视区域
847
+ * + false 不在窗口可视区域
848
+ * @returns
849
+ * + true 可见
850
+ * + false 不可见
851
+ * @example
852
+ * Utils.isVisible(document.documentElement)
853
+ * > true
854
+ */
855
+ isVisible(element: HTMLElement[] | NodeList, inView?: boolean): boolean;
856
+ /**
857
+ * 判断是否是Via浏览器环境
858
+ * @returns
859
+ * + true 是Via
860
+ * + false 不是Via
861
+ * @example
862
+ * Utils.isWebView_Via()
863
+ * > false
864
+ */
865
+ isWebView_Via(): boolean;
866
+ /**
867
+ * 判断是否是X浏览器环境
868
+ * @returns
869
+ * + true 是X浏览器
870
+ * + false 不是X浏览器
871
+ * @example
872
+ * Utils.isWebView_X()
873
+ * > false
874
+ */
875
+ isWebView_X(): boolean;
876
+ /**
877
+ * 把对象内的value值全部取出成数组
878
+ * @param target 目标对象
879
+ * @returns 返回数组
880
+ * @example
881
+ * Utils.parseObjectToArray({"工具类":"jsonToArray","return","Array"});
882
+ * > ['jsonToArray', 'Array']
883
+ **/
884
+ parseObjectToArray(target: AnyObject): any;
885
+ /**
886
+ * 监听某个元素键盘按键事件或window全局按键事件
887
+ * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
888
+ * @param target 需要监听的对象,可以是全局Window或者某个元素
889
+ * @param eventName 事件名,默认keypress
890
+ * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
891
+ * @example
892
+ Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
893
+ if(keyName === "Enter"){
894
+ console.log("回车按键的值是:"+keyValue)
895
+ }
896
+ if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
897
+ console.log("Ctrl和回车键");
898
+ }
899
+ })
900
+ * @example
901
+ 字母和数字键的键码值(keyCode)
902
+ 按键 键码 按键 键码 按键 键码 按键 键码
903
+ A 65 J 74 S 83 1 49
904
+ B 66 K 75 T 84 2 50
905
+ C 67 L 76 U 85 3 51
906
+ D 68 M 77 V 86 4 52
907
+ E 69 N 78 W 87 5 53
908
+ F 70 O 79 X 88 6 54
909
+ G 71 P 80 Y 89 7 55
910
+ H 72 Q 81 Z 90 8 56
911
+ I 73 R 82 0 48 9 57
912
+
913
+ 数字键盘上的键的键码值(keyCode)
914
+ 功能键键码值(keyCode)
915
+ 按键 键码 按键 键码 按键 键码 按键 键码
916
+ 0 96 8 104 F1 112 F7 118
917
+ 1 97 9 105 F2 113 F8 119
918
+ 2 98 * 106 F3 114 F9 120
919
+ 3 99 + 107 F4 115 F10 121
920
+ 4 100 Enter 108 F5 116 F11 122
921
+ 5 101 - 109 F6 117 F12 123
922
+ 6 102 . 110
923
+ 7 103 / 111
924
+
925
+ 控制键键码值(keyCode)
926
+ 按键 键码 按键 键码 按键 键码 按键 键码
927
+ BackSpace 8 Esc 27 → 39 -_ 189
928
+ Tab 9 Spacebar 32 ↓ 40 .> 190
929
+ Clear 12 Page Up 33 Insert 45 /? 191
930
+ Enter 13 Page Down 34 Delete 46 `~ 192
931
+ Shift 16 End 35 Num Lock 144 [{ 219
932
+ Control 17 Home 36 ;: 186 \| 220
933
+ Alt 18 ← 37 =+ 187 ]} 221
934
+ Cape Lock 20 ↑ 38 ,< 188 '" 222
935
+
936
+ 多媒体键码值(keyCode)
937
+ 按键 键码
938
+ 音量加 175
939
+ 音量减 174
940
+ 停止 179
941
+ 静音 173
942
+ 浏览器 172
943
+ 邮件 180
944
+ 搜索 170
945
+ 收藏 171
946
+ **/
947
+ listenKeyboard(target: Window | Node | HTMLElement | typeof globalThis, eventName: "keyup" | "keypress" | "keydown", callback: (keyName: string, keyValue: string, otherCodeList: string[], event: KeyboardEvent) => void): {
948
+ removeListen(): void;
949
+ };
950
+ /**
951
+ * 自动锁对象,用于循环判断运行的函数,在循环外new后使用,注意,如果函数内部存在异步操作,需要使用await
952
+ * @example
953
+ let lock = new Utils.LockFunction(()=>{console.log(1)}))
954
+ lock.run();
955
+ > 1
956
+ * @example
957
+ let lock = new Utils.LockFunction(()=>{console.log(1)}),true) -- 异步操作
958
+ await lock.run();
959
+ > 1
960
+ **/
961
+ LockFunction: UtilsLockFunction;
962
+ /**
963
+ * 日志对象
964
+ * @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}}
965
+ * @example
966
+ let log = new Utils.Log(GM_info);
967
+ log.info("普通输出");
968
+ > 普通输出
969
+
970
+ log.success("成功输出");
971
+ > 成功输出
972
+
973
+ log.error("错误输出");
974
+ > 错误输出
975
+
976
+ log.warn("警告输出");
977
+ > 警告输出
978
+
979
+ log.tag = "自定义tag信息";
980
+ log.info("自定义info的颜色","#e0e0e0");
981
+ > 自定义info的颜色
982
+
983
+ log.config({
984
+ successColor: "#31dc02",
985
+ errorColor: "#e02d2d",
986
+ infoColor: "black",
987
+ })
988
+ log.success("颜色为#31dc02");
989
+ > 颜色为#31dc02
990
+ */
991
+ Log: UtilsLog;
992
+ /**
993
+ * 合并数组内的JSON的值字符串
994
+ * @param data 需要合并的数组
995
+ * @param handleFunc 处理的函数|JSON的key
996
+ * @example
997
+ * Utils.mergeArrayToString([{"name":"数组内数据部分字段合并成字符串->"},{"name":"mergeToString"}],(item)=>{return item["name"]});
998
+ * > '数组内数据部分字段合并成字符串->mergeToString'
999
+ **/
1000
+ mergeArrayToString(data: any[], handleFunc?: (val: any) => any): string;
1001
+ /**
1002
+ * 监听页面元素改变并处理
1003
+ * @param target 需要监听的元素,如果不存在,可以等待它出现
1004
+ * @param observer_config MutationObserver的配置
1005
+ * @example
1006
+ Utils.mutationObserver(document.querySelector("div.xxxx"),{
1007
+ "callback":(mutations, observer)=>{},
1008
+ "config":{childList:true,attributes:true}
1009
+ });
1010
+ * @example
1011
+ Utils.mutationObserver(document.querySelectorAll("div.xxxx"),{
1012
+ "callback":(mutations, observer)=>{},
1013
+ "config":{childList:true,attributes:true}}
1014
+ );
1015
+ * @example
1016
+ Utils.mutationObserver($("div.xxxx"),{
1017
+ "callback":(mutations, observer)=>{},
1018
+ "config":{childList:true,attributes:true}}
1019
+ );
1020
+ **/
1021
+ mutationObserver(target: HTMLElement | Node | NodeList | Document, observer_config: {
1022
+ config?: MutationObserverInit;
1023
+ callback: MutationCallback;
1024
+ }): MutationObserver;
1025
+ /**
1026
+ * 去除全局window下的Utils,返回控制权
1027
+ * @example
1028
+ * let utils = Utils.noConflict();
1029
+ * > ...
1030
+ */
1031
+ noConflict: () => Utils;
1032
+ /**
1033
+ * 恢复/释放该对象内的为function,让它无效/有效
1034
+ * @param needReleaseObject 需要操作的对象
1035
+ * @param needReleaseName 需要操作的对象的名字
1036
+ * @param functionNameList (可选)需要释放的方法,默认:全部方法
1037
+ * @param release (可选)
1038
+ * + true (默认) 释放该对象下的某些方法
1039
+ * + false 恢复该对象下的某些方法
1040
+ * @example
1041
+ // 释放该方法
1042
+ Utils.noConflictFunc(console,"console",["log"],true);
1043
+ console.log;
1044
+ > () => {}
1045
+
1046
+ * @example
1047
+ // 恢复该方法
1048
+ Utils.noConflictFunc(console,"console",["log"],false);
1049
+ console.log;
1050
+ > ƒ log() { [native code] }
1051
+
1052
+ * @example
1053
+ // 释放所有方法
1054
+ Utils.noConflictFunc(console,"console",[],true);
1055
+ console.debug;
1056
+ > () => {}
1057
+
1058
+ * @example
1059
+ // 恢复所有方法
1060
+ Utils.noConflictFunc(console,"console",[],false);
1061
+ console.debug;
1062
+ > ƒ log() { [native code] }
1063
+ **/
1064
+ noConflictFunc(needReleaseObject: object, needReleaseName: string, functionNameList?: any[], release?: boolean): void;
1065
+ /**
1066
+ * base64转blob
1067
+ * @param dataUri base64的数据
1068
+ * @returns blob的链接
1069
+ * @example
1070
+ * Utils.parseBase64ToBlob("data:image/jpeg;base64,.....");
1071
+ * > blob://xxxxxxx
1072
+ **/
1073
+ parseBase64ToBlob(dataUri: string): Blob;
1074
+ /**
1075
+ * base64转File对象
1076
+ * @param dataUri base64的数据
1077
+ * @param fileName (可选)文件名,默认:example
1078
+ * @returns blob的链接
1079
+ * @example
1080
+ * Utils.parseBase64ToFile("data:image/jpeg;base64,.....","测试文件");
1081
+ * > object
1082
+ **/
1083
+ parseBase64ToFile(dataUri: string, fileName?: string): File;
1084
+ /**
1085
+ * 将正则匹配到的结果取出最后一个值并转换成int格式
1086
+ * @param matchList 正则匹配的列表
1087
+ * @param defaultValue 正则匹配的列表为空时,或者正则匹配的列表最后一项不为Int,返回该默认值0
1088
+ * @example
1089
+ * Utils.parseInt(["dadaadada123124","123124"],0);
1090
+ * > 123124
1091
+ *
1092
+ * @example
1093
+ * Utils.parseInt(null,0);
1094
+ * > 0
1095
+ * @example
1096
+ * Utils.parseInt(["aaaaaa"]);
1097
+ * > 0
1098
+ *
1099
+ * @example
1100
+ * Utils.parseInt(["aaaaaa"],"66");
1101
+ * > 66
1102
+ *
1103
+ * @example
1104
+ * Utils.parseInt(["aaaaaaa"],"aa");
1105
+ * > NaN
1106
+ **/
1107
+ parseInt(matchList?: any[], defaultValue?: number): number;
1108
+ /**
1109
+ * blob转File对象
1110
+ * @param blobUrl 需要转换的blob的链接
1111
+ * @param fileName (可选)转换成的File对象的文件名称,默认:example
1112
+ * @example
1113
+ * Utils.parseBlobToFile("blob://xxxxx");
1114
+ * > object
1115
+ **/
1116
+ parseBlobToFile(blobUrl: string, fileName?: string): Promise<File | Error>;
1117
+ /**
1118
+ * 解析CDATA格式的内容字符串
1119
+ * @param text 传入CDATA字符串
1120
+ * @returns 返回解析出的内容
1121
+ * @example
1122
+ * let xml = "<root><![CDATA[This is some CDATA content.]]></root>";
1123
+ * console.log(Utils.parseCDATA(xml));
1124
+ * > This is some CDATA content.
1125
+ */
1126
+ parseCDATA(text: string): string;
1127
+ /**
1128
+ * 【异步函数】File对象转base64
1129
+ * @param fileObj 需要转换的File对象
1130
+ * @example
1131
+ * await Utils.parseFileToBase64(object);
1132
+ * > 'data:image/jpeg:base64/,xxxxxx'
1133
+ **/
1134
+ parseFileToBase64(fileObj: File): Promise<string>;
1135
+ /**
1136
+ * 解析字符串
1137
+ * @param text 要解析的 DOMString。它必须包含 HTML、xml、xhtml+xml 或 svg 文档。
1138
+ * @param mimeType (可选)解析成的类型
1139
+ * + (默认)text/html
1140
+ * + text/xml
1141
+ * + application/xml
1142
+ * + application/xhtml+xml
1143
+ * + image/svg+xml
1144
+ * @example
1145
+ * Utils.parseFromString("<p>123<p>");
1146
+ * > #document
1147
+ */
1148
+ parseFromString(text: string, mimeType?: "text/html" | "text/xml" | "application/xml" | "application/xhtml+xml" | "image/svg+xml"): HTMLElement | XMLDocument | SVGElement;
1149
+ /**
1150
+ * 将字符串进行正则转义
1151
+ * 例如:^替换$
1152
+ * 转换:\^替换\$
1153
+ */
1154
+ parseStringToRegExpString(text: string): string;
1155
+ /**
1156
+ * 阻止事件传递
1157
+ * @param element 要进行处理的元素
1158
+ * @param eventNameList (可选)要阻止的事件名|列表
1159
+ * @param capture (可选)是否捕获,默认false
1160
+ * @example
1161
+ * Utils.preventEvent(document.querySelector("a"),"click")
1162
+ * @example
1163
+ * Utils.preventEvent(event);
1164
+ */
1165
+ preventEvent(event: Event): boolean;
1166
+ /**
1167
+ * 阻止事件传递
1168
+ * @param element 要进行处理的元素
1169
+ * @param eventNameList (可选)要阻止的事件名|列表
1170
+ * @param capture (可选)是否捕获,默认false
1171
+ * @example
1172
+ * Utils.preventEvent(document.querySelector("a"),"click")
1173
+ * @example
1174
+ * Utils.preventEvent(event);
1175
+ */
1176
+ preventEvent(element: HTMLElement, eventNameList?: string | string[], capture?: boolean): boolean;
1177
+ /**
1178
+ * 在canvas元素节点上绘制进度圆圈
1179
+ * @example
1180
+ let progress = new Utils.Process({canvasNode:document.querySelector("canvas")});
1181
+ progress.draw();
1182
+ * **/
1183
+ Progress: UtilsProgress;
1184
+ /**
1185
+ * 劫持Event的isTrust为true,注入时刻,ducument-start
1186
+ * @param isTrustValue (可选)让isTrusted为true
1187
+ * @param filter (可选)过滤出需要的事件名,true为需要,false为不需要
1188
+ * @example
1189
+ * Utils.registerTrustClickEvent()
1190
+ */
1191
+ registerTrustClickEvent(isTrustValue?: boolean, filter?: (typeName: string) => boolean): void;
1192
+ /**
1193
+ * 将数字进行正/负转换
1194
+ * @param num 需要进行转换的数字
1195
+ */
1196
+ reverseNumber(num: number): number;
1197
+ /**
1198
+ * 将元素上的文本或元素使用光标进行选中
1199
+ *
1200
+ * 注意,如果设置startIndex和endIndex,且元素上并无可选则的坐标,那么会报错
1201
+ * @param element 目标元素
1202
+ * @param childTextNode 目标元素下的#text元素
1203
+ * @param startIndex (可选)开始坐标,可为空
1204
+ * @param endIndex (可选)结束坐标,可为空
1205
+ */
1206
+ selectElementText(element: HTMLElement | Element | Node, childTextNode: ChildNode, startIndex?: number, endIndex?: number): void;
1207
+ /**
1208
+ * 复制到剪贴板
1209
+ * @param data 需要复制到剪贴板的文本
1210
+ * @param info (可选)默认:text/plain
1211
+ * @example
1212
+ * Utils.setClip({1:2});
1213
+ * > {"1":2}
1214
+ * @example
1215
+ * Utils.setClip( ()=>{
1216
+ * console.log(1)
1217
+ * });
1218
+ * > ()=>{console.log(1)}
1219
+ * @example
1220
+ * Utils.setClip("xxxx");
1221
+ * > xxxx
1222
+ * @example
1223
+ * Utils.setClip("xxxx","html");
1224
+ * > xxxx
1225
+ * @example
1226
+ * Utils.setClip("xxxx","text/plain");
1227
+ * > xxxx
1228
+ **/
1229
+ setClip(data: any, info?: {
1230
+ type: string;
1231
+ mimetype: string;
1232
+ } | string): Promise<boolean>;
1233
+ /**
1234
+ * 【异步函数】等待N秒执行函数
1235
+ * @param callback 待执行的函数(字符串)
1236
+ * @param delayTime (可选)延时时间(ms),默认:0
1237
+ * @example
1238
+ * await Utils.setTimeout(()=>{}, 2500);
1239
+ * > ƒ tryCatchObj() {}
1240
+ * @example
1241
+ * await Utils.setTimeout("()=>{console.log(12345)}", 2500);
1242
+ * > ƒ tryCatchObj() {}
1243
+ **/
1244
+ setTimeout(callback: (() => void) | string, delayTime?: number): Promise<any>;
1245
+ /**
1246
+ * 【异步函数】延迟xxx毫秒
1247
+ * @param delayTime (可选)延时时间(ms),默认:0
1248
+ * @example
1249
+ * await Utils.sleep(2500)
1250
+ **/
1251
+ sleep(delayTime?: number): Promise<void>;
1252
+ /**
1253
+ * 向右拖动滑块
1254
+ * @param selector 选择器|元素
1255
+ * @param offsetX (可选)水平拖动长度,默认:window.innerWidth
1256
+ * @example
1257
+ * Utils.dragSlider("#xxxx");
1258
+ * @example
1259
+ * Utils.dragSlider("#xxxx",100);
1260
+ */
1261
+ dragSlider(selector: string | Element | Node, offsetX?: number): void;
1262
+ /**
1263
+ * 使目标元素进入全屏
1264
+ * @param element (可选)目标元素,默认:document.documentElement
1265
+ * @param options (可选)配置,一般不用
1266
+ * @example
1267
+ * Utils.enterFullScreen();
1268
+ */
1269
+ enterFullScreen(element: HTMLElement, options?: FullscreenOptions): void;
1270
+ /**
1271
+ * 使浏览器退出全屏
1272
+ * @param element (可选)目标元素,默认:document.documentElement
1273
+ * @example
1274
+ * Utils.exitFullScreen();
1275
+ */
1276
+ exitFullScreen(element?: HTMLElement): Promise<void>;
1277
+ /**
1278
+ * 数组按照内部某个值的大小比对排序,如[{"time":"2022-1-1"},{"time":"2022-2-2"}]
1279
+ * @param data 数据|获取数据的方法
1280
+ * @param getPropertyValueFunc 数组内部项的某个属性的值的方法,参数为这个项
1281
+ * @param sortByDesc (可选)排序方式
1282
+ * + true (默认)倒序(值最大排第一个,如:6、5、4、3...)
1283
+ * + false 升序(值最小排第一个,如:1、2、3、4...)
1284
+ * @returns 返回比较排序完成的数组
1285
+ * @example
1286
+ * Utils.sortListByProperty([{"time":"2022-1-1"},{"time":"2022-2-2"}],(item)=>{return item["time"]})
1287
+ * > [{time: '2022-2-2'},{time: '2022-1-1'}]
1288
+ * @example
1289
+ * Utils.sortListByProperty([{"time":"2022-1-1"},{"time":"2022-2-2"}],(item)=>{return item["time"]},false)
1290
+ * > [{time: '2022-1-1'},{time: '2022-2-2'}]
1291
+ **/
1292
+ sortListByProperty<T extends any[] | NodeList>(data: T, getPropertyValueFunc: string | ((value: T) => any), sortByDesc?: boolean): T;
1293
+ /**
1294
+ * 字符串转正则,用于把字符串中不规范的字符进行转义
1295
+ * @param targetString 需要进行转换的字符串
1296
+ * @param flags 正则标志
1297
+ */
1298
+ stringToRegular(targetString: string | RegExp, flags?: "g" | "i" | "m" | "u" | "y" | string): RegExp;
1299
+ /**
1300
+ * 字符串首字母转大写
1301
+ * @param targetString 目标字符串
1302
+ * @param otherStrToLowerCase (可选)剩余部分字符串转小写,默认false
1303
+ */
1304
+ stringTitleToUpperCase(targetString: string, otherStrToLowerCase?: boolean): string;
1305
+ /**
1306
+ * 判断目标字符串是否是以xxx开始
1307
+ *
1308
+ * 如果searchString是字符串数组,那么判断的结果则是字符串数组中的任意字符匹配到返回true
1309
+ * @param target 目标字符串
1310
+ * @param searchString 需要搜索的字符串
1311
+ * @param position (可选)目标字符串的判断起点,要求≥0,默认为0
1312
+ */
1313
+ startsWith(target: string, searchString: string | RegExp | string[], position?: number): boolean;
1314
+ /**
1315
+ * 字符串首字母转小写
1316
+ * @param targetString 目标字符串
1317
+ * @param otherStrToLowerCase (可选)剩余部分字符串转大写,默认false
1318
+ */
1319
+ stringTitleToLowerCase(targetString: string, otherStrToUpperCase?: boolean): string;
1320
+ /**
1321
+ * 字符串转Object对象,类似'{"test":""}' => {"test":""}
1322
+ * @param data
1323
+ * @param errorCallBack (可选)错误回调
1324
+ * @example
1325
+ * Utils.toJSON("{123:123}")
1326
+ * > {123:123}
1327
+ */
1328
+ toJSON<T extends AnyObject>(data: string | null, errorCallBack?: (error: Error) => void): T;
1329
+ /**
1330
+ * 对象转为UrlSearchParams格式的字符串
1331
+ * @param obj 目标对象,可以是对象组成的数组
1332
+ */
1333
+ toSearchParamsStr(obj: object | object[]): string;
1334
+ /**
1335
+ * 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
1336
+ * @returns 返回一个对象,其中包含 error 和 run 两个方法。
1337
+ * @example
1338
+ * Utils.tryCatch().error().run(()=>{console.log(1)});
1339
+ * > 1
1340
+ * @example
1341
+ * Utils.tryCatch().config({log:true}).error((error)=>{console.log(error)}).run(()=>{throw new Error('测试错误')});
1342
+ * > ()=>{throw new Error('测试错误')}出现错误
1343
+ */
1344
+ tryCatch: (...args: any[]) => {
1345
+ (): void;
1346
+ config(paramDetails: {
1347
+ log: boolean;
1348
+ }): any;
1349
+ error(handler: string | Function): any;
1350
+ run(callback: string | Function, __context__?: object | null | undefined): any;
1351
+ };
1352
+ /**
1353
+ * 数组去重,去除重复的值
1354
+ * @param uniqueArrayData 需要去重的数组
1355
+ * @param compareArrayData 用来比较的数组
1356
+ * @param compareFun 数组比较方法,如果值相同,去除该数据
1357
+ * @returns 返回去重完毕的数组
1358
+ * @example
1359
+ * Utils.uniqueArray([1,2,3],[1,2],(item,item2)=>{return item===item2 ? true:false});
1360
+ * > [3]
1361
+ *
1362
+ * @example
1363
+ * Utils.uniqueArray([1,2,3],[1,2]);
1364
+ * > [3]
1365
+ *
1366
+ * @example
1367
+ * Utils.uniqueArray([{"key":1,"value":2},{"key":2}],[{"key":1}],(item,item2)=>{return item["key"] === item2["key"] ? true:false});
1368
+ * > [{"key": 2}]
1369
+ **/
1370
+ uniqueArray<T extends any, TT extends any>(uniqueArrayData?: T[], compareArrayData?: TT[], compareFun?: (item1: T, item2: TT) => boolean): any[];
1371
+ /**
1372
+ * 等待函数数组全部执行完毕,注意,每个函数的顺序不是同步
1373
+ * @param data 需要遍历的数组
1374
+ * @param handleFunc 对该数组进行操作的函数,该函数的参数为数组格式的参数,[数组下标,数组项]
1375
+ * @example
1376
+ * await Utils.waitArrayLoopToEnd([callback,callback,callback],xxxcallback);
1377
+ **/
1378
+ waitArrayLoopToEnd(data: any[] | HTMLElement[], handleFunc: Function): Promise<void[]>;
1379
+ /**
1380
+ * 等待指定元素出现,支持多个selector
1381
+ * @param nodeSelectors 一个或多个节点选择器,必须为字符串类型
1382
+ * @example
1383
+ * Utils.waitNode("div.xxx").then( element =>{
1384
+ * console.log(element); // div.xxx => HTMLElement
1385
+ * })
1386
+ * @example
1387
+ * Utils.waitNode("div.xxx","a.xxx").then( (elementList)=>{
1388
+ * console.log(elementList[0]); // div.xxx => HTMLElement
1389
+ * console.log(elementList[1]); // a.xxx => HTMLElement
1390
+ * })
1391
+ */
1392
+ waitNode<T extends HTMLElement>(nodeSelector: string | [string]): Promise<T>;
1393
+ /**
1394
+ * 等待指定元素出现,支持多个selector
1395
+ * @param nodeSelectors 一个或多个节点选择器,必须为字符串类型
1396
+ * @example
1397
+ * Utils.waitNode("div.xxx").then( element =>{
1398
+ * console.log(element); // div.xxx => HTMLElement
1399
+ * })
1400
+ * @example
1401
+ * Utils.waitNode("div.xxx","a.xxx").then( (elementList)=>{
1402
+ * console.log(elementList[0]); // div.xxx => HTMLElement
1403
+ * console.log(elementList[1]); // a.xxx => HTMLElement
1404
+ * })
1405
+ */
1406
+ waitNode<T extends HTMLElement>(...nodeSelectors: string[]): Promise<T[]>;
1407
+ /**
1408
+ * 等待指定元素出现,支持多个selector
1409
+ * @param nodeSelectors 一个或多个节点选择器,必须为字符串类型
1410
+ * @example
1411
+ * Utils.waitNode("div.xxx").then( element =>{
1412
+ * console.log(element); // div.xxx => HTMLElement
1413
+ * })
1414
+ * @example
1415
+ * Utils.waitNode("div.xxx","a.xxx").then( (elementList)=>{
1416
+ * console.log(elementList[0]); // div.xxx => HTMLElement
1417
+ * console.log(elementList[1]); // a.xxx => HTMLElement
1418
+ * })
1419
+ */
1420
+ waitNode<T extends HTMLElement>(...nodeSelectors: string[]): Promise<T | T[]>;
1421
+ /**
1422
+ * 在规定时间内,等待任意元素出现,支持多个selector,如果未出现,则关闭监听
1423
+ * @param nodeSelectorsList 一个或多个节点选择器,必须为字符串类型
1424
+ * @param maxTime (可选)xx毫秒(ms)后关闭监听,默认:0(ms)
1425
+ * @example
1426
+ * Utils.waitNodeWithInterval("a.xxx",30000).then(element=>{
1427
+ * console.log(element);
1428
+ * })
1429
+ * @example
1430
+ * Utils.waitNodeWithInterval(["div.xxx","a.xxx"],30000).then(elementList=>{
1431
+ * console.log(elementList[0]); // div.xxx => HTMLElement
1432
+ * console.log(elementList[1]); // a.xxx => HTMLElement
1433
+ * })
1434
+ */
1435
+ waitNodeWithInterval<T extends HTMLElement>(nodeSelectorsList?: string[] | string, maxTime?: number): Promise<T | T[]>;
1436
+ /**
1437
+ * 等待任意元素出现,支持多个selector
1438
+ * @param nodeSelectors 一个或多个节点选择器,必须为字符串类型
1439
+ * @example
1440
+ * Utils.waitAnyNode("div.xxx","a.xxx").then( element =>{
1441
+ * console.log(element); // a.xxx => HTMLElement
1442
+ * })
1443
+ */
1444
+ waitAnyNode<T extends HTMLElement>(...nodeSelectors: any[]): Promise<T>;
1445
+ /**
1446
+ * 等待指定元素出现
1447
+ * @param nodeSelectors
1448
+ * @returns 当nodeSelectors为数组多个时,
1449
+ * 返回如:[ NodeList, NodeList ],
1450
+ * 当nodeSelectors为单个时,
1451
+ * 返回如:NodeList。
1452
+ * NodeList元素与页面存在强绑定,当已获取该NodeList,但是页面中却删除了,该元素在NodeList中会被自动删除
1453
+ * @example
1454
+ * Utils.waitNodeList("div.xxx").then( nodeList =>{
1455
+ * console.log(nodeList) // div.xxx => NodeList
1456
+ * })
1457
+ * @example
1458
+ * Utils.waitNodeList("div.xxx","a.xxx").then( nodeListArray =>{
1459
+ * console.log(nodeListArray[0]) // div.xxx => NodeList
1460
+ * console.log(nodeListArray[1]) // a.xxx => NodeList
1461
+ * })
1462
+ */
1463
+ waitNodeList<T extends HTMLElement>(nodeSelector: string): Promise<T>;
1464
+ /**
1465
+ * 等待指定元素出现,支持多个selector
1466
+ * @param nodeSelectors
1467
+ * @returns 当nodeSelectors为数组多个时,
1468
+ * 返回如:[ NodeList, NodeList ],
1469
+ * 当nodeSelectors为单个时,
1470
+ * 返回如:NodeList。
1471
+ * NodeList元素与页面存在强绑定,当已获取该NodeList,但是页面中却删除了,该元素在NodeList中会被自动删除
1472
+ * @example
1473
+ * Utils.waitNodeList("div.xxx").then( nodeList =>{
1474
+ * console.log(nodeList) // div.xxx => NodeList
1475
+ * })
1476
+ * @example
1477
+ * Utils.waitNodeList("div.xxx","a.xxx").then( nodeListArray =>{
1478
+ * console.log(nodeListArray[0]) // div.xxx => NodeList
1479
+ * console.log(nodeListArray[1]) // a.xxx => NodeList
1480
+ * })
1481
+ */
1482
+ waitNodeList<T extends HTMLElement>(...nodeSelectors: string[]): Promise<NodeListOf<T>[]>;
1483
+ /**
1484
+ * 等待任意元素出现,支持多个selector
1485
+ * @param nodeSelectors
1486
+ * @returns 返回NodeList
1487
+ * NodeList元素与页面存在强绑定,当已获取该NodeList,但是页面中却删除了,该元素在NodeList中会被自动删除
1488
+ * @example
1489
+ * Utils.waitAnyNodeList("div.xxx").then( nodeList =>{
1490
+ * console.log(nodeList) // div.xxx => NodeList
1491
+ * })
1492
+ * @example
1493
+ * Utils.waitAnyNodeList("div.xxx","a.xxx").then( nodeList =>{
1494
+ * console.log(nodeList) // a.xxx => NodeList
1495
+ * })
1496
+ */
1497
+ waitAnyNodeList<T extends HTMLElement>(...nodeSelectors: string[]): Promise<NodeListOf<T>[]>;
1498
+ /**
1499
+ * 等待对象上的属性出现
1500
+ * @param checkObj 检查的对象
1501
+ * @param checkPropertyName 检查的对象的属性名
1502
+ * @example
1503
+ * await Utils.waitProperty(window,"test");
1504
+ * console.log("test success set");
1505
+ *
1506
+ * window.test = 1;
1507
+ * > "test success set"
1508
+ *
1509
+ */
1510
+ waitProperty<T extends any>(checkObj: AnyObject | (() => AnyObject), checkPropertyName: string): Promise<T>;
1511
+ /**
1512
+ * 在规定时间内等待对象上的属性出现
1513
+ * @param checkObj 检查的对象
1514
+ * @param checkPropertyName 检查的对象的属性名
1515
+ * @param intervalTimer (可选)检查间隔时间(ms),默认250ms
1516
+ * @param maxTime (可选)限制在多长时间内,默认-1(不限制时间)
1517
+ * @example
1518
+ * await Utils.waitPropertyByInterval(window,"test");
1519
+ * console.log("test success set");
1520
+ */
1521
+ waitPropertyByInterval<T extends any>(checkObj: AnyObject | (() => AnyObject), checkPropertyName: string | ((obj: any) => boolean), intervalTimer?: number, maxTime?: number): Promise<T>;
1522
+ /**
1523
+ * 在规定时间内等待元素上的__vue__属性或者__vue__属性上的某个值出现出现
1524
+ * @param element 目标元素
1525
+ * @param propertyName (可选)vue上的属性名或者传递一个获取属性的方法返回boolean
1526
+ * @param timer (可选)间隔时间(ms),默认:250(ms)
1527
+ * @param maxTime(可选) 限制在多长时间内,默认:-1(不限制时间)
1528
+ * @param vueName (可选)vue挂载的属性名,默认:__vue__
1529
+ * @example
1530
+ * await Utils.waitVueByInterval(
1531
+ * function(){
1532
+ * return document.querySelector("a.xx")
1533
+ * },
1534
+ * function(__vue__){
1535
+ * return Boolean(__vue__.xxx == null);
1536
+ * },
1537
+ * 250,
1538
+ * 10000,
1539
+ * "__vue__"
1540
+ * )
1541
+ */
1542
+ waitVueByInterval(element: HTMLElement | (() => HTMLElement), propertyName: string | ((__vue__: any) => boolean), timer?: number, maxTime?: number, vueName?: "__vue__" | string): Promise<boolean>;
1543
+ /**
1544
+ * 观察对象的set、get
1545
+ * @param target 观察的对象
1546
+ * @param propertyName 观察的对象的属性名
1547
+ * @param getCallBack (可选)触发get的回调,可以自定义返回特定值
1548
+ * @param setCallBack (可选)触发set的回调,参数为将要设置的value
1549
+ * @example
1550
+ * Utils.watchObject(window,"test",()=>{return 111;},(value)=>{console.log("test出现,值是",value)});
1551
+ *
1552
+ * window.test = 1;
1553
+ * > test出现,值是 1
1554
+ * console.log(window.test);
1555
+ * > 111;
1556
+ */
1557
+ watchObject(target: AnyObject, propertyName: string, getCallBack: (value: any) => void, setCallBack: (value: any) => void): void;
1558
+ }
1559
+ declare let utils: Utils;
1560
+ export { utils as Utils };