@whitesev/domutils 1.9.2 → 1.9.4

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 (51) hide show
  1. package/README.md +58 -58
  2. package/dist/index.amd.js +53 -30
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.amd.min.js +1 -1
  5. package/dist/index.amd.min.js.map +1 -1
  6. package/dist/index.cjs.js +53 -30
  7. package/dist/index.cjs.js.map +1 -1
  8. package/dist/index.cjs.min.js +1 -1
  9. package/dist/index.cjs.min.js.map +1 -1
  10. package/dist/index.esm.js +53 -30
  11. package/dist/index.esm.js.map +1 -1
  12. package/dist/index.esm.min.js +1 -1
  13. package/dist/index.esm.min.js.map +1 -1
  14. package/dist/index.iife.js +53 -30
  15. package/dist/index.iife.js.map +1 -1
  16. package/dist/index.iife.min.js +1 -1
  17. package/dist/index.iife.min.js.map +1 -1
  18. package/dist/index.system.js +53 -30
  19. package/dist/index.system.js.map +1 -1
  20. package/dist/index.system.min.js +1 -1
  21. package/dist/index.system.min.js.map +1 -1
  22. package/dist/index.umd.js +53 -30
  23. package/dist/index.umd.js.map +1 -1
  24. package/dist/index.umd.min.js +1 -1
  25. package/dist/index.umd.min.js.map +1 -1
  26. package/dist/types/src/ElementEvent.d.ts +2 -2
  27. package/dist/types/src/types/DOMUtilsCSSProperty.d.ts +36 -36
  28. package/dist/types/src/types/DOMUtilsEvent.d.ts +437 -420
  29. package/dist/types/src/types/WindowApi.d.ts +14 -14
  30. package/dist/types/src/types/env.d.ts +10 -10
  31. package/dist/types/src/types/global.d.ts +1 -1
  32. package/dist/types/src/types/gm.d.ts +2 -2
  33. package/index.ts +3 -3
  34. package/package.json +21 -22
  35. package/src/CommonUtils.ts +163 -163
  36. package/src/ElementAnimate.ts +290 -290
  37. package/src/ElementEvent.ts +1593 -1569
  38. package/src/ElementHandler.ts +43 -43
  39. package/src/ElementSelector.ts +307 -289
  40. package/src/ElementWait.ts +742 -742
  41. package/src/GlobalData.ts +5 -5
  42. package/src/OriginPrototype.ts +5 -5
  43. package/src/Utils.ts +388 -388
  44. package/src/WindowApi.ts +59 -59
  45. package/src/index.ts +2052 -2052
  46. package/src/types/DOMUtilsCSSProperty.d.ts +36 -36
  47. package/src/types/DOMUtilsEvent.d.ts +437 -420
  48. package/src/types/WindowApi.d.ts +14 -14
  49. package/src/types/env.d.ts +10 -10
  50. package/src/types/global.d.ts +1 -1
  51. package/src/types/gm.d.ts +2 -2
@@ -1,742 +1,742 @@
1
- import { ElementSelector, elementSelector } from "./ElementSelector";
2
- import type { WindowApiOption } from "./types/WindowApi";
3
- import { utils } from "./Utils";
4
- import { WindowApi } from "./WindowApi";
5
-
6
- class ElementWait extends ElementSelector {
7
- windowApi: typeof WindowApi.prototype;
8
- constructor(windowApiOption?: WindowApiOption) {
9
- super(windowApiOption);
10
- this.windowApi = new WindowApi(windowApiOption);
11
- }
12
- /**
13
- * 等待任意事件成立
14
- *
15
- * 运行方式为根据页面元素的改变而触发回调
16
- * @param checkFn 检测的函数
17
- * @param timeout 超时时间,默认0
18
- * @param parent (可选)父元素,默认document
19
- * @example
20
- * DOMUtils.wait(()=> {
21
- * let $test = document.querySelector("#test");
22
- * return {
23
- * success: $test !== null,
24
- * data: $test
25
- * }
26
- * })
27
- */
28
- wait<T>(
29
- checkFn: (...args: any[]) => {
30
- /**
31
- * 是否检测成功
32
- */
33
- success: boolean;
34
- /**
35
- * 返回的值
36
- */
37
- data: T;
38
- },
39
- timeout?: null | undefined,
40
- parent?: Node | Element | Document | HTMLElement
41
- ): Promise<T>;
42
- wait<T>(
43
- checkFn: (...args: any[]) => {
44
- /**
45
- * 是否检测成功
46
- */
47
- success: boolean;
48
- /**
49
- * 返回的值
50
- */
51
- data: T;
52
- },
53
- timeout?: number,
54
- parent?: Node | Element | Document | HTMLElement
55
- ): Promise<T | null>;
56
- wait<T>(
57
- checkFn: (...args: any[]) => {
58
- /**
59
- * 是否检测成功
60
- */
61
- success: boolean;
62
- /**
63
- * 返回的值
64
- */
65
- data: T;
66
- },
67
- timeout?: number | null | undefined,
68
- parent?: Node | Element | Document | HTMLElement
69
- ): Promise<T | null> {
70
- const UtilsContext = this;
71
- const __timeout__ = typeof timeout === "number" ? timeout : 0;
72
- return new Promise((resolve) => {
73
- const observer = utils.mutationObserver(parent || UtilsContext.windowApi.document, {
74
- config: {
75
- subtree: true,
76
- childList: true,
77
- attributes: true,
78
- },
79
- immediate: true,
80
- callback(_, __observer__) {
81
- const result = checkFn();
82
- if (result.success) {
83
- // 取消观察器
84
- if (typeof __observer__?.disconnect === "function") {
85
- __observer__.disconnect();
86
- }
87
- resolve(result.data);
88
- }
89
- },
90
- });
91
- if (__timeout__ > 0) {
92
- setTimeout(() => {
93
- // 取消观察器
94
- if (typeof observer?.disconnect === "function") {
95
- observer.disconnect();
96
- }
97
- resolve(null as T);
98
- }, __timeout__);
99
- }
100
- });
101
- }
102
- /**
103
- * 等待元素出现
104
- * @param selectorFn 获取元素的函数
105
- * @example
106
- * DOMUtils.waitNode(()=>document.querySelector("div")).then( $div =>{
107
- * console.log($div); // $div => HTMLDivELement
108
- * })
109
- */
110
- waitNode<K = HTMLElement>(selectorFn: () => K | null | undefined): Promise<K>;
111
- /**
112
- * 等待元素出现
113
- * @param selectorFn 获取元素的函数
114
- * @param timeout 超时时间,默认0
115
- * @example
116
- * DOMUtils.waitNode(()=>document.querySelector("div"), 1000).then( $div =>{
117
- * console.log($div); // $div => HTMLDivELement | null
118
- * })
119
- */
120
- waitNode<K = HTMLElement>(selectorFn: () => K | null | undefined, timeout: number): Promise<K | null | undefined>;
121
- /**
122
- * 等待元素出现
123
- * @param selectorFn 获取元素的函数
124
- * @param parent 父元素
125
- * @param timeout 超时时间,默认0
126
- * @example
127
- * DOMUtils.waitNode(()=>document.querySelector("div"), document).then( $div =>{
128
- * console.log($div); // $div => HTMLDivELement
129
- * })
130
- * DOMUtils.waitNode(()=>document.querySelector("div"), document, 1000).then( $div =>{
131
- * console.log($div); // $div => HTMLDivELement | null
132
- * })
133
- */
134
- waitNode<K = HTMLElement>(
135
- selectorFn: () => K | null | undefined,
136
- parent: Node | Element | Document | HTMLElement,
137
- timeout?: number
138
- ): Promise<K | null | undefined>;
139
- /**
140
- * 等待元素出现
141
- * @param selector CSS选择器
142
- * @param parent (可选)父元素,默认document
143
- * @example
144
- * DOMUtils.waitNode("div").then( $div =>{
145
- * console.log($div); // div => HTMLDivELement
146
- * })
147
- * DOMUtils.waitNode("div", document).then( $div =>{
148
- * console.log($div); // div => HTMLDivELement
149
- * })
150
- */
151
- waitNode<K extends keyof HTMLElementTagNameMap>(
152
- selector: K,
153
- parent?: Node | Element | Document | HTMLElement
154
- ): Promise<HTMLElementTagNameMap[K]>;
155
- waitNode<T extends Element = HTMLElement>(
156
- selector: string,
157
- parent?: Node | Element | Document | HTMLElement
158
- ): Promise<T>;
159
- /**
160
- * 等待元素出现
161
- * @param selectorList CSS选择器数组
162
- * @param parent (可选)父元素,默认document
163
- * @example
164
- * DOMUtils.waitNode(["div"]).then( ([$div]) =>{
165
- * console.log($div); // div => HTMLDivELement[]
166
- * })
167
- * DOMUtils.waitNode(["div"], document).then( ([$div]) =>{
168
- * console.log($div); // div => HTMLDivELement[]
169
- * })
170
- */
171
- waitNode<K extends keyof HTMLElementTagNameMap>(
172
- selectorList: K[],
173
- parent?: Node | Element | Document | HTMLElement
174
- ): Promise<HTMLElementTagNameMap[K][]>;
175
- waitNode<T extends Element[] = HTMLElement[]>(
176
- selectorList: string[],
177
- parent?: Node | Element | Document | HTMLElement
178
- ): Promise<T>;
179
- /**
180
- * 等待元素出现
181
- * @param selector CSS选择器
182
- * @param parent 父元素,默认document
183
- * @param timeout 超时时间,默认0
184
- * @example
185
- * DOMUtils.waitNode("div", document, 1000).then( $div =>{
186
- * console.log($div); // $div => HTMLDivELement | null
187
- * })
188
- */
189
- waitNode<K extends keyof HTMLElementTagNameMap>(
190
- selector: K,
191
- parent: Node | Element | Document | HTMLElement,
192
- timeout: number
193
- ): Promise<HTMLElementTagNameMap[K] | null>;
194
- waitNode<T extends Element = HTMLElement>(
195
- selector: string,
196
- parent: Node | Element | Document | HTMLElement,
197
- timeout: number
198
- ): Promise<T | null>;
199
- /**
200
- * 等待元素出现
201
- * @param selectorList CSS选择器数组
202
- * @param parent 父元素,默认document
203
- * @param timeout 超时时间,默认0
204
- * @example
205
- * DOMUtils.waitNode(["div"], document, 1000).then( ([$div]) =>{
206
- * console.log($div); // $div => HTMLDivELement[] | null
207
- * })
208
- */
209
- waitNode<K extends keyof HTMLElementTagNameMap>(
210
- selectorList: K[],
211
- parent: Node | Element | Document | HTMLElement,
212
- timeout: number
213
- ): Promise<HTMLElementTagNameMap[K] | null>;
214
- waitNode<T extends Element[] = HTMLElement[]>(
215
- selectorList: string[],
216
- parent: Node | Element | Document | HTMLElement,
217
- timeout: number
218
- ): Promise<T | null>;
219
- /**
220
- * 等待元素出现
221
- * @param selector CSS选择器
222
- * @param timeout 超时时间,默认0
223
- * @example
224
- * DOMUtils.waitNode("div", 1000).then( $div =>{
225
- * console.log($div); // $div => HTMLDivELement | null
226
- * })
227
- */
228
- waitNode<K extends keyof HTMLElementTagNameMap>(
229
- selector: K,
230
- timeout: number
231
- ): Promise<HTMLElementTagNameMap[K] | null>;
232
- waitNode<T extends Element = HTMLElement>(selector: string, timeout: number): Promise<T | null>;
233
- /**
234
- * 等待元素出现
235
- * @param selectorList CSS选择器数组
236
- * @param timeout 超时时间,默认0
237
- * @example
238
- * DOMUtils.waitNode(["div"], 1000).then( [$div] =>{
239
- * console.log($div); // $div => HTMLDivELement[] | null
240
- * })
241
- */
242
- waitNode<K extends keyof HTMLElementTagNameMap>(
243
- selectorList: K[],
244
- timeout: number
245
- ): Promise<HTMLElementTagNameMap[K] | null>;
246
- waitNode<T extends Element[] = HTMLElement[]>(selectorList: string[], timeout: number): Promise<T | null>;
247
- waitNode<T extends Element | Element[]>(...args: any[]): Promise<T | null> {
248
- // 过滤掉undefined
249
- args = args.filter((arg) => arg !== void 0);
250
- const UtilsContext = this;
251
- // 选择器
252
- const selector = args[0] as unknown as string | string[] | ((...args: any[]) => any);
253
- // 父元素(监听的元素)
254
- let parent: Element = UtilsContext.windowApi.document as any as Element;
255
- // 超时时间
256
- let timeout = 0;
257
- if (typeof args[0] !== "string" && !Array.isArray(args[0]) && typeof args[0] !== "function") {
258
- throw new TypeError("DOMUtils.waitNode 第一个参数必须是string|string[]|Function");
259
- }
260
- if (args.length === 1) {
261
- // 上面已做处理
262
- } else if (args.length === 2) {
263
- const secondParam = args[1];
264
- if (typeof secondParam === "number") {
265
- // "div",10000
266
- timeout = secondParam;
267
- } else if (typeof secondParam === "object" && secondParam instanceof Node) {
268
- // "div",document
269
- parent = secondParam as any as Element;
270
- } else {
271
- throw new TypeError("DOMUtils.waitNode 第二个参数必须是number|Node");
272
- }
273
- } else if (args.length === 3) {
274
- // "div",document,10000
275
- // 第二个参数,parent
276
- const secondParam = args[1];
277
- // 第三个参数,timeout
278
- const thirdParam = args[2];
279
- if (typeof secondParam === "object" && secondParam instanceof Node) {
280
- parent = secondParam as any as Element;
281
- if (typeof thirdParam === "number") {
282
- timeout = thirdParam;
283
- } else {
284
- throw new TypeError("DOMUtils.waitNode 第三个参数必须是number");
285
- }
286
- } else {
287
- throw new TypeError("DOMUtils.waitNode 第二个参数必须是Node");
288
- }
289
- } else {
290
- throw new TypeError("DOMUtils.waitNode 参数个数错误");
291
- }
292
- function getNode() {
293
- if (Array.isArray(selector)) {
294
- const result: T[] = [];
295
- for (let index = 0; index < selector.length; index++) {
296
- const node = elementSelector.selector(selector[index]);
297
- if (node) {
298
- result.push(node as any);
299
- }
300
- }
301
- if (result.length === selector.length) {
302
- return result;
303
- }
304
- } else if (typeof selector === "function") {
305
- return selector();
306
- } else {
307
- return elementSelector.selector(selector, parent);
308
- }
309
- }
310
- return UtilsContext.wait(
311
- () => {
312
- const node = getNode();
313
- if (node) {
314
- return {
315
- success: true,
316
- data: node,
317
- };
318
- } else {
319
- return {
320
- success: false,
321
- data: node,
322
- };
323
- }
324
- },
325
- timeout,
326
- parent
327
- );
328
- }
329
- /**
330
- * 等待任意元素出现
331
- * @param selectorList CSS选择器数组
332
- * @param parent (可选)监听的父元素
333
- * @example
334
- * DOMUtils.waitAnyNode(["div","div"]).then( $div =>{
335
- * console.log($div); // $div => HTMLDivELement 这里是第一个
336
- * })
337
- * DOMUtils.waitAnyNode(["a","div"], document).then( $a =>{
338
- * console.log($a); // $a => HTMLAnchorElement 这里是第一个
339
- * })
340
- */
341
- waitAnyNode<K extends keyof HTMLElementTagNameMap>(
342
- selectorList: K[],
343
- parent?: Node | Element | Document | HTMLElement
344
- ): Promise<HTMLElementTagNameMap[K]>;
345
- waitAnyNode<T extends Element = HTMLElement>(
346
- selectorList: string[],
347
- parent?: Node | Element | Document | HTMLElement
348
- ): Promise<T>;
349
- /**
350
- * 等待任意元素出现
351
- * @param selectorList CSS选择器数组
352
- * @param parent 父元素,默认document
353
- * @param timeout 超时时间,默认0
354
- * @example
355
- * DOMUtils.waitAnyNode(["div","div"], document, 10000).then( $div =>{
356
- * console.log($div); // $div => HTMLDivELement | null
357
- * })
358
- */
359
- waitAnyNode<K extends keyof HTMLElementTagNameMap>(
360
- selectorList: K[],
361
- parent: Node | Element | Document | HTMLElement,
362
- timeout: number
363
- ): Promise<HTMLElementTagNameMap[K] | null>;
364
- waitAnyNode<T extends Element = HTMLElement>(
365
- selectorList: string[],
366
- parent: Node | Element | Document | HTMLElement,
367
- timeout: number
368
- ): Promise<T | null>;
369
- /**
370
- * 等待任意元素出现
371
- * @param selectorList CSS选择器数组
372
- * @param timeout 超时时间,默认0
373
- * @example
374
- * DOMUtils.waitAnyNode(["div","div"], 10000).then( $div =>{
375
- * console.log($div); // $div => HTMLDivELement | null
376
- * })
377
- */
378
- waitAnyNode<K extends keyof HTMLElementTagNameMap>(
379
- selectorList: K[],
380
- timeout: number
381
- ): Promise<HTMLElementTagNameMap[K] | null>;
382
- waitAnyNode<T extends Element = HTMLElement>(selectorList: string[], timeout: number): Promise<T | null>;
383
- waitAnyNode<T extends Element = HTMLElement>(...args: any[]): Promise<T | null> {
384
- // 过滤掉undefined
385
- args = args.filter((arg) => arg !== void 0);
386
- const UtilsContext = this;
387
- // 选择器
388
- const selectorList = args[0] as unknown as string[];
389
- // 父元素(监听的元素)
390
- let parent: Element = UtilsContext.windowApi.document as any as Element;
391
- // 超时时间
392
- let timeout = 0;
393
- if (typeof args[0] !== "object" && !Array.isArray(args[0])) {
394
- throw new TypeError("DOMUtils.waitAnyNode 第一个参数必须是string[]");
395
- }
396
- if (args.length === 1) {
397
- // 上面已做处理
398
- } else if (args.length === 2) {
399
- const secondParam = args[1];
400
- if (typeof secondParam === "number") {
401
- // "div",10000
402
- timeout = secondParam;
403
- } else if (typeof secondParam === "object" && secondParam instanceof Node) {
404
- // "div",document
405
- parent = secondParam as any as Element;
406
- } else {
407
- throw new TypeError("DOMUtils.waitAnyNode 第二个参数必须是number|Node");
408
- }
409
- } else if (args.length === 3) {
410
- // "div",document,10000
411
- // 第二个参数,parent
412
- const secondParam = args[1];
413
- // 第三个参数,timeout
414
- const thirdParam = args[2];
415
- if (typeof secondParam === "object" && secondParam instanceof Node) {
416
- parent = secondParam as any as Element;
417
- if (typeof thirdParam === "number") {
418
- timeout = thirdParam;
419
- } else {
420
- throw new TypeError("DOMUtils.waitAnyNode 第三个参数必须是number");
421
- }
422
- } else {
423
- throw new TypeError("DOMUtils.waitAnyNode 第二个参数必须是Node");
424
- }
425
- } else {
426
- throw new TypeError("DOMUtils.waitAnyNode 参数个数错误");
427
- }
428
- const promiseList = selectorList.map((selector) => {
429
- return UtilsContext.waitNode<T>(selector, parent, timeout);
430
- });
431
- return Promise.any(promiseList);
432
- }
433
- /**
434
- * 等待元素数组出现
435
- * @param selector CSS选择器
436
- * @param parent (可选)监听的父元素
437
- * @example
438
- * DOMUtils.waitNodeList("div").then( $result =>{
439
- * console.log($result); // $result => NodeListOf<HTMLDivElement>
440
- * })
441
- * DOMUtils.waitNodeList("div", document).then( $result =>{
442
- * console.log($result); // $result => NodeListOf<HTMLDivElement>
443
- * })
444
- */
445
- waitNodeList<T extends keyof HTMLElementTagNameMap>(
446
- selector: T,
447
- parent?: Node | Element | Document | HTMLElement
448
- ): Promise<NodeListOf<HTMLElementTagNameMap[T]>>;
449
- waitNodeList<T extends NodeListOf<Element> = NodeListOf<HTMLElement>>(
450
- selector: string,
451
- parent?: Node | Element | Document | HTMLElement
452
- ): Promise<T>;
453
- /**
454
- * 等待元素数组出现
455
- * @param selectorList CSS选择器数组
456
- * @param parent (可选)监听的父元素
457
- * @example
458
- * DOMUtils.waitNodeList(["div"]).then( $result =>{
459
- * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
460
- * })
461
- * DOMUtils.waitNodeList(["div"], document).then( $result =>{
462
- * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
463
- * })
464
- */
465
- waitNodeList<K extends keyof HTMLElementTagNameMap>(
466
- selectorList: K[],
467
- parent?: Node | Element | Document | HTMLElement
468
- ): Promise<NodeListOf<HTMLElementTagNameMap[K]>[]>;
469
- waitNodeList<T extends NodeListOf<Element>[] = NodeListOf<HTMLElement>[]>(
470
- selectorList: string[],
471
- parent?: Node | Element | Document | HTMLElement
472
- ): Promise<T>;
473
- /**
474
- * 等待元素数组出现
475
- * @param selector CSS选择器
476
- * @param parent 监听的父元素
477
- * @param timeout 超时时间,默认0
478
- * @example
479
- * DOMUtils.waitNodeList("div", document, 10000).then( $result =>{
480
- * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
481
- * })
482
- */
483
- waitNodeList<T extends NodeListOf<Element> = NodeListOf<HTMLElement>>(
484
- selector: string,
485
- parent: Node | Element | Document | HTMLElement,
486
- timeout: number
487
- ): Promise<T | null>;
488
- waitNodeList<K extends keyof HTMLElementTagNameMap>(
489
- selector: K,
490
- parent: Node | Element | Document | HTMLElement,
491
- timeout: number
492
- ): Promise<NodeListOf<HTMLElementTagNameMap[K]> | null>;
493
- /**
494
- * 等待元素数组出现
495
- * @param selectorList CSS选择器数组
496
- * @param parent 监听的父元素
497
- * @param timeout 超时时间,默认0
498
- * @example
499
- * DOMUtils.waitNodeList(["div"], document, 10000).then( $result =>{
500
- * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
501
- * })
502
- */
503
- waitNodeList<K extends keyof HTMLElementTagNameMap>(
504
- selectorList: K[],
505
- parent: Node | Element | Document | HTMLElement,
506
- timeout: number
507
- ): Promise<NodeListOf<HTMLElementTagNameMap[K]>[] | null>;
508
- waitNodeList<T extends NodeListOf<Element>[] = NodeListOf<HTMLElement>[]>(
509
- selectorList: string[],
510
- parent: Node | Element | Document | HTMLElement,
511
- timeout: number
512
- ): Promise<T | null>;
513
- /**
514
- * 等待元素数组出现
515
- * @param selector CSS选择器数组
516
- * @param timeout 超时时间,默认0
517
- * @example
518
- * DOMUtils.waitNodeList("div", 10000).then( $result =>{
519
- * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
520
- * })
521
- */
522
- waitNodeList<K extends keyof HTMLElementTagNameMap>(
523
- selector: K[],
524
- timeout: number
525
- ): Promise<NodeListOf<HTMLElementTagNameMap[K]> | null>;
526
- waitNodeList<T extends NodeListOf<Element> = NodeListOf<HTMLElement>>(
527
- selector: string[],
528
- timeout: number
529
- ): Promise<T | null>;
530
- /**
531
- * 等待元素数组出现
532
- * @param selectorList CSS选择器数组
533
- * @param timeout 超时时间,默认0
534
- * @example
535
- * DOMUtils.waitNodeList(["div"], 10000).then( $result =>{
536
- * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
537
- * })
538
- */
539
- waitNodeList<K extends keyof HTMLElementTagNameMap>(
540
- selectorList: K[],
541
- timeout: number
542
- ): Promise<NodeListOf<HTMLElementTagNameMap[K]>[] | null>;
543
- waitNodeList<T extends NodeListOf<Element> = NodeListOf<HTMLElement>>(
544
- selectorList: string[],
545
- timeout: number
546
- ): Promise<T[] | null>;
547
- waitNodeList<T extends NodeListOf<Element> | NodeListOf<Element>[]>(...args: any[]): Promise<T | null> {
548
- // 过滤掉undefined
549
- args = args.filter((arg) => arg !== void 0);
550
- const UtilsContext = this;
551
- // 选择器数组
552
- const selector = args[0] as unknown as string | string[];
553
- // 父元素(监听的元素)
554
- let parent: Element = UtilsContext.windowApi.document as any as Element;
555
- // 超时时间
556
- let timeout = 0;
557
- if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
558
- throw new TypeError("DOMUtils.waitNodeList 第一个参数必须是string|string[]");
559
- }
560
- if (args.length === 1) {
561
- // 上面已做处理
562
- } else if (args.length === 2) {
563
- const secondParam = args[1];
564
- if (typeof secondParam === "number") {
565
- // "div",10000
566
- timeout = secondParam;
567
- } else if (typeof secondParam === "object" && secondParam instanceof Node) {
568
- // "div",document
569
- parent = secondParam as any as Element;
570
- } else {
571
- throw new TypeError("DOMUtils.waitNodeList 第二个参数必须是number|Node");
572
- }
573
- } else if (args.length === 3) {
574
- // "div",document,10000
575
- // 第二个参数,parent
576
- const secondParam = args[1];
577
- // 第三个参数,timeout
578
- const thirdParam = args[2];
579
- if (typeof secondParam === "object" && secondParam instanceof Node) {
580
- parent = secondParam as any as Element;
581
- if (typeof thirdParam === "number") {
582
- timeout = thirdParam;
583
- } else {
584
- throw new TypeError("DOMUtils.waitNodeList 第三个参数必须是number");
585
- }
586
- } else {
587
- throw new TypeError("DOMUtils.waitNodeList 第二个参数必须是Node");
588
- }
589
- } else {
590
- throw new TypeError("DOMUtils.waitNodeList 参数个数错误");
591
- }
592
- function getNodeList() {
593
- if (Array.isArray(selector)) {
594
- const result: T[] = [];
595
- for (let index = 0; index < selector.length; index++) {
596
- const nodeList = elementSelector.selectorAll(selector[index], parent);
597
- if (nodeList.length) {
598
- result.push(nodeList as any as T);
599
- }
600
- }
601
- if (result.length === selector.length) {
602
- return result;
603
- }
604
- } else {
605
- const nodeList = elementSelector.selectorAll(selector, parent);
606
- if (nodeList.length) {
607
- return nodeList;
608
- }
609
- }
610
- }
611
- return UtilsContext.wait<any>(
612
- () => {
613
- const node = getNodeList();
614
- if (node) {
615
- return {
616
- success: true,
617
- data: node,
618
- };
619
- } else {
620
- return {
621
- success: false,
622
- data: node,
623
- };
624
- }
625
- },
626
- timeout,
627
- parent
628
- );
629
- }
630
- /**
631
- * 等待任意元素数组出现
632
- * @param selectorList CSS选择器数组
633
- * @param parent (可选)监听的父元素
634
- * @example
635
- * DOMUtils.waitAnyNodeList(["div","a"]).then( $result =>{
636
- * console.log($result); // $result => NodeListOf<HTMLDivElement>
637
- * })
638
- * DOMUtils.waitAnyNodeList(["div","a"], document).then( $result =>{
639
- * console.log($result); // $result => NodeListOf<HTMLDivElement>
640
- * })
641
- */
642
- waitAnyNodeList<K extends keyof HTMLElementTagNameMap>(
643
- selectorList: K[],
644
- parent?: Node | Element | Document | HTMLElement
645
- ): Promise<NodeListOf<HTMLElementTagNameMap[K]>>;
646
- waitAnyNodeList<T extends Element = HTMLElement>(
647
- selectorList: string[],
648
- parent?: Node | Element | Document | HTMLElement
649
- ): Promise<NodeListOf<T>>;
650
- /**
651
- * 等待任意元素数组出现
652
- * @param selectorList CSS选择器数组
653
- * @param parent 父元素,默认document
654
- * @param timeout 超时时间,默认0
655
- * @example
656
- * DOMUtils.waitAnyNodeList(["div","a"], document, 10000).then( $result =>{
657
- * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
658
- * })
659
- */
660
- waitAnyNodeList<K extends keyof HTMLElementTagNameMap>(
661
- selectorList: K[],
662
- parent: Node | Element | Document | HTMLElement,
663
- timeout: number
664
- ): Promise<NodeListOf<HTMLElementTagNameMap[K]> | null>;
665
- waitAnyNodeList<T extends Element = HTMLElement>(
666
- selectorList: string[],
667
- parent: Node | Element | Document | HTMLElement,
668
- timeout: number
669
- ): Promise<NodeListOf<T> | null>;
670
- /**
671
- * 等待任意元素出现
672
- * @param selectorList CSS选择器数组
673
- * @param timeout 超时时间,默认0
674
- * @example
675
- * DOMUtils.waitAnyNodeList(["div","div"], 10000).then( $result =>{
676
- * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
677
- * })
678
- */
679
- waitAnyNodeList<K extends keyof HTMLElementTagNameMap>(
680
- selectorList: K[],
681
- timeout: number
682
- ): Promise<NodeListOf<HTMLElementTagNameMap[K]> | null>;
683
- waitAnyNodeList<T extends Element = HTMLElement>(
684
- selectorList: string[],
685
- timeout: number
686
- ): Promise<NodeListOf<T> | null>;
687
- waitAnyNodeList<T extends Element = HTMLElement>(...args: any[]): Promise<NodeListOf<T> | null> {
688
- // 过滤掉undefined
689
- args = args.filter((arg) => arg !== void 0);
690
- const UtilsContext = this;
691
- // 选择器数组
692
- const selectorList = args[0] as unknown as string[];
693
- // 父元素(监听的元素)
694
- let parent: Element = UtilsContext.windowApi.document as any as Element;
695
- // 超时时间
696
- let timeout = 0;
697
- if (!Array.isArray(args[0])) {
698
- throw new TypeError("DOMUtils.waitAnyNodeList 第一个参数必须是string[]");
699
- }
700
- if (args.length === 1) {
701
- // 上面已做处理
702
- } else if (args.length === 2) {
703
- const secondParam = args[1];
704
- if (typeof secondParam === "number") {
705
- // "div",10000
706
- timeout = secondParam;
707
- } else if (typeof secondParam === "object" && secondParam instanceof Node) {
708
- // "div",document
709
- parent = secondParam as any as Element;
710
- } else {
711
- throw new TypeError("DOMUtils.waitAnyNodeList 第二个参数必须是number|Node");
712
- }
713
- } else if (args.length === 3) {
714
- // "div",document,10000
715
- // 第二个参数,parent
716
- const secondParam = args[1];
717
- // 第三个参数,timeout
718
- const thirdParam = args[2];
719
- if (typeof secondParam === "object" && secondParam instanceof Node) {
720
- parent = secondParam as any as Element;
721
- if (typeof thirdParam === "number") {
722
- timeout = thirdParam;
723
- } else {
724
- throw new TypeError("DOMUtils.waitAnyNodeList 第三个参数必须是number");
725
- }
726
- } else {
727
- throw new TypeError("DOMUtils.waitAnyNodeList 第二个参数必须是Node");
728
- }
729
- } else {
730
- throw new TypeError("DOMUtils.waitAnyNodeList 参数个数错误");
731
- }
732
-
733
- const promiseList = selectorList.map((selector) => {
734
- return UtilsContext.waitNodeList<NodeListOf<T>>(selector, parent, timeout);
735
- });
736
- return Promise.any(promiseList);
737
- }
738
- }
739
-
740
- const elementWait = new ElementWait();
741
-
742
- export { elementWait, ElementWait };
1
+ import { ElementSelector, elementSelector } from "./ElementSelector";
2
+ import type { WindowApiOption } from "./types/WindowApi";
3
+ import { utils } from "./Utils";
4
+ import { WindowApi } from "./WindowApi";
5
+
6
+ class ElementWait extends ElementSelector {
7
+ windowApi: typeof WindowApi.prototype;
8
+ constructor(windowApiOption?: WindowApiOption) {
9
+ super(windowApiOption);
10
+ this.windowApi = new WindowApi(windowApiOption);
11
+ }
12
+ /**
13
+ * 等待任意事件成立
14
+ *
15
+ * 运行方式为根据页面元素的改变而触发回调
16
+ * @param checkFn 检测的函数
17
+ * @param timeout 超时时间,默认0
18
+ * @param parent (可选)父元素,默认document
19
+ * @example
20
+ * DOMUtils.wait(()=> {
21
+ * let $test = document.querySelector("#test");
22
+ * return {
23
+ * success: $test !== null,
24
+ * data: $test
25
+ * }
26
+ * })
27
+ */
28
+ wait<T>(
29
+ checkFn: (...args: any[]) => {
30
+ /**
31
+ * 是否检测成功
32
+ */
33
+ success: boolean;
34
+ /**
35
+ * 返回的值
36
+ */
37
+ data: T;
38
+ },
39
+ timeout?: null | undefined,
40
+ parent?: Node | Element | Document | HTMLElement
41
+ ): Promise<T>;
42
+ wait<T>(
43
+ checkFn: (...args: any[]) => {
44
+ /**
45
+ * 是否检测成功
46
+ */
47
+ success: boolean;
48
+ /**
49
+ * 返回的值
50
+ */
51
+ data: T;
52
+ },
53
+ timeout?: number,
54
+ parent?: Node | Element | Document | HTMLElement
55
+ ): Promise<T | null>;
56
+ wait<T>(
57
+ checkFn: (...args: any[]) => {
58
+ /**
59
+ * 是否检测成功
60
+ */
61
+ success: boolean;
62
+ /**
63
+ * 返回的值
64
+ */
65
+ data: T;
66
+ },
67
+ timeout?: number | null | undefined,
68
+ parent?: Node | Element | Document | HTMLElement
69
+ ): Promise<T | null> {
70
+ const UtilsContext = this;
71
+ const __timeout__ = typeof timeout === "number" ? timeout : 0;
72
+ return new Promise((resolve) => {
73
+ const observer = utils.mutationObserver(parent || UtilsContext.windowApi.document, {
74
+ config: {
75
+ subtree: true,
76
+ childList: true,
77
+ attributes: true,
78
+ },
79
+ immediate: true,
80
+ callback(_, __observer__) {
81
+ const result = checkFn();
82
+ if (result.success) {
83
+ // 取消观察器
84
+ if (typeof __observer__?.disconnect === "function") {
85
+ __observer__.disconnect();
86
+ }
87
+ resolve(result.data);
88
+ }
89
+ },
90
+ });
91
+ if (__timeout__ > 0) {
92
+ setTimeout(() => {
93
+ // 取消观察器
94
+ if (typeof observer?.disconnect === "function") {
95
+ observer.disconnect();
96
+ }
97
+ resolve(null as T);
98
+ }, __timeout__);
99
+ }
100
+ });
101
+ }
102
+ /**
103
+ * 等待元素出现
104
+ * @param selectorFn 获取元素的函数
105
+ * @example
106
+ * DOMUtils.waitNode(()=>document.querySelector("div")).then( $div =>{
107
+ * console.log($div); // $div => HTMLDivELement
108
+ * })
109
+ */
110
+ waitNode<K = HTMLElement>(selectorFn: () => K | null | undefined): Promise<K>;
111
+ /**
112
+ * 等待元素出现
113
+ * @param selectorFn 获取元素的函数
114
+ * @param timeout 超时时间,默认0
115
+ * @example
116
+ * DOMUtils.waitNode(()=>document.querySelector("div"), 1000).then( $div =>{
117
+ * console.log($div); // $div => HTMLDivELement | null
118
+ * })
119
+ */
120
+ waitNode<K = HTMLElement>(selectorFn: () => K | null | undefined, timeout: number): Promise<K | null | undefined>;
121
+ /**
122
+ * 等待元素出现
123
+ * @param selectorFn 获取元素的函数
124
+ * @param parent 父元素
125
+ * @param timeout 超时时间,默认0
126
+ * @example
127
+ * DOMUtils.waitNode(()=>document.querySelector("div"), document).then( $div =>{
128
+ * console.log($div); // $div => HTMLDivELement
129
+ * })
130
+ * DOMUtils.waitNode(()=>document.querySelector("div"), document, 1000).then( $div =>{
131
+ * console.log($div); // $div => HTMLDivELement | null
132
+ * })
133
+ */
134
+ waitNode<K = HTMLElement>(
135
+ selectorFn: () => K | null | undefined,
136
+ parent: Node | Element | Document | HTMLElement,
137
+ timeout?: number
138
+ ): Promise<K | null | undefined>;
139
+ /**
140
+ * 等待元素出现
141
+ * @param selector CSS选择器
142
+ * @param parent (可选)父元素,默认document
143
+ * @example
144
+ * DOMUtils.waitNode("div").then( $div =>{
145
+ * console.log($div); // div => HTMLDivELement
146
+ * })
147
+ * DOMUtils.waitNode("div", document).then( $div =>{
148
+ * console.log($div); // div => HTMLDivELement
149
+ * })
150
+ */
151
+ waitNode<K extends keyof HTMLElementTagNameMap>(
152
+ selector: K,
153
+ parent?: Node | Element | Document | HTMLElement
154
+ ): Promise<HTMLElementTagNameMap[K]>;
155
+ waitNode<T extends Element = HTMLElement>(
156
+ selector: string,
157
+ parent?: Node | Element | Document | HTMLElement
158
+ ): Promise<T>;
159
+ /**
160
+ * 等待元素出现
161
+ * @param selectorList CSS选择器数组
162
+ * @param parent (可选)父元素,默认document
163
+ * @example
164
+ * DOMUtils.waitNode(["div"]).then( ([$div]) =>{
165
+ * console.log($div); // div => HTMLDivELement[]
166
+ * })
167
+ * DOMUtils.waitNode(["div"], document).then( ([$div]) =>{
168
+ * console.log($div); // div => HTMLDivELement[]
169
+ * })
170
+ */
171
+ waitNode<K extends keyof HTMLElementTagNameMap>(
172
+ selectorList: K[],
173
+ parent?: Node | Element | Document | HTMLElement
174
+ ): Promise<HTMLElementTagNameMap[K][]>;
175
+ waitNode<T extends Element[] = HTMLElement[]>(
176
+ selectorList: string[],
177
+ parent?: Node | Element | Document | HTMLElement
178
+ ): Promise<T>;
179
+ /**
180
+ * 等待元素出现
181
+ * @param selector CSS选择器
182
+ * @param parent 父元素,默认document
183
+ * @param timeout 超时时间,默认0
184
+ * @example
185
+ * DOMUtils.waitNode("div", document, 1000).then( $div =>{
186
+ * console.log($div); // $div => HTMLDivELement | null
187
+ * })
188
+ */
189
+ waitNode<K extends keyof HTMLElementTagNameMap>(
190
+ selector: K,
191
+ parent: Node | Element | Document | HTMLElement,
192
+ timeout: number
193
+ ): Promise<HTMLElementTagNameMap[K] | null>;
194
+ waitNode<T extends Element = HTMLElement>(
195
+ selector: string,
196
+ parent: Node | Element | Document | HTMLElement,
197
+ timeout: number
198
+ ): Promise<T | null>;
199
+ /**
200
+ * 等待元素出现
201
+ * @param selectorList CSS选择器数组
202
+ * @param parent 父元素,默认document
203
+ * @param timeout 超时时间,默认0
204
+ * @example
205
+ * DOMUtils.waitNode(["div"], document, 1000).then( ([$div]) =>{
206
+ * console.log($div); // $div => HTMLDivELement[] | null
207
+ * })
208
+ */
209
+ waitNode<K extends keyof HTMLElementTagNameMap>(
210
+ selectorList: K[],
211
+ parent: Node | Element | Document | HTMLElement,
212
+ timeout: number
213
+ ): Promise<HTMLElementTagNameMap[K] | null>;
214
+ waitNode<T extends Element[] = HTMLElement[]>(
215
+ selectorList: string[],
216
+ parent: Node | Element | Document | HTMLElement,
217
+ timeout: number
218
+ ): Promise<T | null>;
219
+ /**
220
+ * 等待元素出现
221
+ * @param selector CSS选择器
222
+ * @param timeout 超时时间,默认0
223
+ * @example
224
+ * DOMUtils.waitNode("div", 1000).then( $div =>{
225
+ * console.log($div); // $div => HTMLDivELement | null
226
+ * })
227
+ */
228
+ waitNode<K extends keyof HTMLElementTagNameMap>(
229
+ selector: K,
230
+ timeout: number
231
+ ): Promise<HTMLElementTagNameMap[K] | null>;
232
+ waitNode<T extends Element = HTMLElement>(selector: string, timeout: number): Promise<T | null>;
233
+ /**
234
+ * 等待元素出现
235
+ * @param selectorList CSS选择器数组
236
+ * @param timeout 超时时间,默认0
237
+ * @example
238
+ * DOMUtils.waitNode(["div"], 1000).then( [$div] =>{
239
+ * console.log($div); // $div => HTMLDivELement[] | null
240
+ * })
241
+ */
242
+ waitNode<K extends keyof HTMLElementTagNameMap>(
243
+ selectorList: K[],
244
+ timeout: number
245
+ ): Promise<HTMLElementTagNameMap[K] | null>;
246
+ waitNode<T extends Element[] = HTMLElement[]>(selectorList: string[], timeout: number): Promise<T | null>;
247
+ waitNode<T extends Element | Element[]>(...args: any[]): Promise<T | null> {
248
+ // 过滤掉undefined
249
+ args = args.filter((arg) => arg !== void 0);
250
+ const UtilsContext = this;
251
+ // 选择器
252
+ const selector = args[0] as unknown as string | string[] | ((...args: any[]) => any);
253
+ // 父元素(监听的元素)
254
+ let parent: Element = UtilsContext.windowApi.document as any as Element;
255
+ // 超时时间
256
+ let timeout = 0;
257
+ if (typeof args[0] !== "string" && !Array.isArray(args[0]) && typeof args[0] !== "function") {
258
+ throw new TypeError("DOMUtils.waitNode 第一个参数必须是string|string[]|Function");
259
+ }
260
+ if (args.length === 1) {
261
+ // 上面已做处理
262
+ } else if (args.length === 2) {
263
+ const secondParam = args[1];
264
+ if (typeof secondParam === "number") {
265
+ // "div",10000
266
+ timeout = secondParam;
267
+ } else if (typeof secondParam === "object" && secondParam instanceof Node) {
268
+ // "div",document
269
+ parent = secondParam as any as Element;
270
+ } else {
271
+ throw new TypeError("DOMUtils.waitNode 第二个参数必须是number|Node");
272
+ }
273
+ } else if (args.length === 3) {
274
+ // "div",document,10000
275
+ // 第二个参数,parent
276
+ const secondParam = args[1];
277
+ // 第三个参数,timeout
278
+ const thirdParam = args[2];
279
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
280
+ parent = secondParam as any as Element;
281
+ if (typeof thirdParam === "number") {
282
+ timeout = thirdParam;
283
+ } else {
284
+ throw new TypeError("DOMUtils.waitNode 第三个参数必须是number");
285
+ }
286
+ } else {
287
+ throw new TypeError("DOMUtils.waitNode 第二个参数必须是Node");
288
+ }
289
+ } else {
290
+ throw new TypeError("DOMUtils.waitNode 参数个数错误");
291
+ }
292
+ function getNode() {
293
+ if (Array.isArray(selector)) {
294
+ const result: T[] = [];
295
+ for (let index = 0; index < selector.length; index++) {
296
+ const node = elementSelector.selector(selector[index]);
297
+ if (node) {
298
+ result.push(node as any);
299
+ }
300
+ }
301
+ if (result.length === selector.length) {
302
+ return result;
303
+ }
304
+ } else if (typeof selector === "function") {
305
+ return selector();
306
+ } else {
307
+ return elementSelector.selector(selector, parent);
308
+ }
309
+ }
310
+ return UtilsContext.wait(
311
+ () => {
312
+ const node = getNode();
313
+ if (node) {
314
+ return {
315
+ success: true,
316
+ data: node,
317
+ };
318
+ } else {
319
+ return {
320
+ success: false,
321
+ data: node,
322
+ };
323
+ }
324
+ },
325
+ timeout,
326
+ parent
327
+ );
328
+ }
329
+ /**
330
+ * 等待任意元素出现
331
+ * @param selectorList CSS选择器数组
332
+ * @param parent (可选)监听的父元素
333
+ * @example
334
+ * DOMUtils.waitAnyNode(["div","div"]).then( $div =>{
335
+ * console.log($div); // $div => HTMLDivELement 这里是第一个
336
+ * })
337
+ * DOMUtils.waitAnyNode(["a","div"], document).then( $a =>{
338
+ * console.log($a); // $a => HTMLAnchorElement 这里是第一个
339
+ * })
340
+ */
341
+ waitAnyNode<K extends keyof HTMLElementTagNameMap>(
342
+ selectorList: K[],
343
+ parent?: Node | Element | Document | HTMLElement
344
+ ): Promise<HTMLElementTagNameMap[K]>;
345
+ waitAnyNode<T extends Element = HTMLElement>(
346
+ selectorList: string[],
347
+ parent?: Node | Element | Document | HTMLElement
348
+ ): Promise<T>;
349
+ /**
350
+ * 等待任意元素出现
351
+ * @param selectorList CSS选择器数组
352
+ * @param parent 父元素,默认document
353
+ * @param timeout 超时时间,默认0
354
+ * @example
355
+ * DOMUtils.waitAnyNode(["div","div"], document, 10000).then( $div =>{
356
+ * console.log($div); // $div => HTMLDivELement | null
357
+ * })
358
+ */
359
+ waitAnyNode<K extends keyof HTMLElementTagNameMap>(
360
+ selectorList: K[],
361
+ parent: Node | Element | Document | HTMLElement,
362
+ timeout: number
363
+ ): Promise<HTMLElementTagNameMap[K] | null>;
364
+ waitAnyNode<T extends Element = HTMLElement>(
365
+ selectorList: string[],
366
+ parent: Node | Element | Document | HTMLElement,
367
+ timeout: number
368
+ ): Promise<T | null>;
369
+ /**
370
+ * 等待任意元素出现
371
+ * @param selectorList CSS选择器数组
372
+ * @param timeout 超时时间,默认0
373
+ * @example
374
+ * DOMUtils.waitAnyNode(["div","div"], 10000).then( $div =>{
375
+ * console.log($div); // $div => HTMLDivELement | null
376
+ * })
377
+ */
378
+ waitAnyNode<K extends keyof HTMLElementTagNameMap>(
379
+ selectorList: K[],
380
+ timeout: number
381
+ ): Promise<HTMLElementTagNameMap[K] | null>;
382
+ waitAnyNode<T extends Element = HTMLElement>(selectorList: string[], timeout: number): Promise<T | null>;
383
+ waitAnyNode<T extends Element = HTMLElement>(...args: any[]): Promise<T | null> {
384
+ // 过滤掉undefined
385
+ args = args.filter((arg) => arg !== void 0);
386
+ const UtilsContext = this;
387
+ // 选择器
388
+ const selectorList = args[0] as unknown as string[];
389
+ // 父元素(监听的元素)
390
+ let parent: Element = UtilsContext.windowApi.document as any as Element;
391
+ // 超时时间
392
+ let timeout = 0;
393
+ if (typeof args[0] !== "object" && !Array.isArray(args[0])) {
394
+ throw new TypeError("DOMUtils.waitAnyNode 第一个参数必须是string[]");
395
+ }
396
+ if (args.length === 1) {
397
+ // 上面已做处理
398
+ } else if (args.length === 2) {
399
+ const secondParam = args[1];
400
+ if (typeof secondParam === "number") {
401
+ // "div",10000
402
+ timeout = secondParam;
403
+ } else if (typeof secondParam === "object" && secondParam instanceof Node) {
404
+ // "div",document
405
+ parent = secondParam as any as Element;
406
+ } else {
407
+ throw new TypeError("DOMUtils.waitAnyNode 第二个参数必须是number|Node");
408
+ }
409
+ } else if (args.length === 3) {
410
+ // "div",document,10000
411
+ // 第二个参数,parent
412
+ const secondParam = args[1];
413
+ // 第三个参数,timeout
414
+ const thirdParam = args[2];
415
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
416
+ parent = secondParam as any as Element;
417
+ if (typeof thirdParam === "number") {
418
+ timeout = thirdParam;
419
+ } else {
420
+ throw new TypeError("DOMUtils.waitAnyNode 第三个参数必须是number");
421
+ }
422
+ } else {
423
+ throw new TypeError("DOMUtils.waitAnyNode 第二个参数必须是Node");
424
+ }
425
+ } else {
426
+ throw new TypeError("DOMUtils.waitAnyNode 参数个数错误");
427
+ }
428
+ const promiseList = selectorList.map((selector) => {
429
+ return UtilsContext.waitNode<T>(selector, parent, timeout);
430
+ });
431
+ return Promise.any(promiseList);
432
+ }
433
+ /**
434
+ * 等待元素数组出现
435
+ * @param selector CSS选择器
436
+ * @param parent (可选)监听的父元素
437
+ * @example
438
+ * DOMUtils.waitNodeList("div").then( $result =>{
439
+ * console.log($result); // $result => NodeListOf<HTMLDivElement>
440
+ * })
441
+ * DOMUtils.waitNodeList("div", document).then( $result =>{
442
+ * console.log($result); // $result => NodeListOf<HTMLDivElement>
443
+ * })
444
+ */
445
+ waitNodeList<T extends keyof HTMLElementTagNameMap>(
446
+ selector: T,
447
+ parent?: Node | Element | Document | HTMLElement
448
+ ): Promise<NodeListOf<HTMLElementTagNameMap[T]>>;
449
+ waitNodeList<T extends NodeListOf<Element> = NodeListOf<HTMLElement>>(
450
+ selector: string,
451
+ parent?: Node | Element | Document | HTMLElement
452
+ ): Promise<T>;
453
+ /**
454
+ * 等待元素数组出现
455
+ * @param selectorList CSS选择器数组
456
+ * @param parent (可选)监听的父元素
457
+ * @example
458
+ * DOMUtils.waitNodeList(["div"]).then( $result =>{
459
+ * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
460
+ * })
461
+ * DOMUtils.waitNodeList(["div"], document).then( $result =>{
462
+ * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
463
+ * })
464
+ */
465
+ waitNodeList<K extends keyof HTMLElementTagNameMap>(
466
+ selectorList: K[],
467
+ parent?: Node | Element | Document | HTMLElement
468
+ ): Promise<NodeListOf<HTMLElementTagNameMap[K]>[]>;
469
+ waitNodeList<T extends NodeListOf<Element>[] = NodeListOf<HTMLElement>[]>(
470
+ selectorList: string[],
471
+ parent?: Node | Element | Document | HTMLElement
472
+ ): Promise<T>;
473
+ /**
474
+ * 等待元素数组出现
475
+ * @param selector CSS选择器
476
+ * @param parent 监听的父元素
477
+ * @param timeout 超时时间,默认0
478
+ * @example
479
+ * DOMUtils.waitNodeList("div", document, 10000).then( $result =>{
480
+ * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
481
+ * })
482
+ */
483
+ waitNodeList<T extends NodeListOf<Element> = NodeListOf<HTMLElement>>(
484
+ selector: string,
485
+ parent: Node | Element | Document | HTMLElement,
486
+ timeout: number
487
+ ): Promise<T | null>;
488
+ waitNodeList<K extends keyof HTMLElementTagNameMap>(
489
+ selector: K,
490
+ parent: Node | Element | Document | HTMLElement,
491
+ timeout: number
492
+ ): Promise<NodeListOf<HTMLElementTagNameMap[K]> | null>;
493
+ /**
494
+ * 等待元素数组出现
495
+ * @param selectorList CSS选择器数组
496
+ * @param parent 监听的父元素
497
+ * @param timeout 超时时间,默认0
498
+ * @example
499
+ * DOMUtils.waitNodeList(["div"], document, 10000).then( $result =>{
500
+ * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
501
+ * })
502
+ */
503
+ waitNodeList<K extends keyof HTMLElementTagNameMap>(
504
+ selectorList: K[],
505
+ parent: Node | Element | Document | HTMLElement,
506
+ timeout: number
507
+ ): Promise<NodeListOf<HTMLElementTagNameMap[K]>[] | null>;
508
+ waitNodeList<T extends NodeListOf<Element>[] = NodeListOf<HTMLElement>[]>(
509
+ selectorList: string[],
510
+ parent: Node | Element | Document | HTMLElement,
511
+ timeout: number
512
+ ): Promise<T | null>;
513
+ /**
514
+ * 等待元素数组出现
515
+ * @param selector CSS选择器数组
516
+ * @param timeout 超时时间,默认0
517
+ * @example
518
+ * DOMUtils.waitNodeList("div", 10000).then( $result =>{
519
+ * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
520
+ * })
521
+ */
522
+ waitNodeList<K extends keyof HTMLElementTagNameMap>(
523
+ selector: K[],
524
+ timeout: number
525
+ ): Promise<NodeListOf<HTMLElementTagNameMap[K]> | null>;
526
+ waitNodeList<T extends NodeListOf<Element> = NodeListOf<HTMLElement>>(
527
+ selector: string[],
528
+ timeout: number
529
+ ): Promise<T | null>;
530
+ /**
531
+ * 等待元素数组出现
532
+ * @param selectorList CSS选择器数组
533
+ * @param timeout 超时时间,默认0
534
+ * @example
535
+ * DOMUtils.waitNodeList(["div"], 10000).then( $result =>{
536
+ * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
537
+ * })
538
+ */
539
+ waitNodeList<K extends keyof HTMLElementTagNameMap>(
540
+ selectorList: K[],
541
+ timeout: number
542
+ ): Promise<NodeListOf<HTMLElementTagNameMap[K]>[] | null>;
543
+ waitNodeList<T extends NodeListOf<Element> = NodeListOf<HTMLElement>>(
544
+ selectorList: string[],
545
+ timeout: number
546
+ ): Promise<T[] | null>;
547
+ waitNodeList<T extends NodeListOf<Element> | NodeListOf<Element>[]>(...args: any[]): Promise<T | null> {
548
+ // 过滤掉undefined
549
+ args = args.filter((arg) => arg !== void 0);
550
+ const UtilsContext = this;
551
+ // 选择器数组
552
+ const selector = args[0] as unknown as string | string[];
553
+ // 父元素(监听的元素)
554
+ let parent: Element = UtilsContext.windowApi.document as any as Element;
555
+ // 超时时间
556
+ let timeout = 0;
557
+ if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
558
+ throw new TypeError("DOMUtils.waitNodeList 第一个参数必须是string|string[]");
559
+ }
560
+ if (args.length === 1) {
561
+ // 上面已做处理
562
+ } else if (args.length === 2) {
563
+ const secondParam = args[1];
564
+ if (typeof secondParam === "number") {
565
+ // "div",10000
566
+ timeout = secondParam;
567
+ } else if (typeof secondParam === "object" && secondParam instanceof Node) {
568
+ // "div",document
569
+ parent = secondParam as any as Element;
570
+ } else {
571
+ throw new TypeError("DOMUtils.waitNodeList 第二个参数必须是number|Node");
572
+ }
573
+ } else if (args.length === 3) {
574
+ // "div",document,10000
575
+ // 第二个参数,parent
576
+ const secondParam = args[1];
577
+ // 第三个参数,timeout
578
+ const thirdParam = args[2];
579
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
580
+ parent = secondParam as any as Element;
581
+ if (typeof thirdParam === "number") {
582
+ timeout = thirdParam;
583
+ } else {
584
+ throw new TypeError("DOMUtils.waitNodeList 第三个参数必须是number");
585
+ }
586
+ } else {
587
+ throw new TypeError("DOMUtils.waitNodeList 第二个参数必须是Node");
588
+ }
589
+ } else {
590
+ throw new TypeError("DOMUtils.waitNodeList 参数个数错误");
591
+ }
592
+ function getNodeList() {
593
+ if (Array.isArray(selector)) {
594
+ const result: T[] = [];
595
+ for (let index = 0; index < selector.length; index++) {
596
+ const nodeList = elementSelector.selectorAll(selector[index], parent);
597
+ if (nodeList.length) {
598
+ result.push(nodeList as any as T);
599
+ }
600
+ }
601
+ if (result.length === selector.length) {
602
+ return result;
603
+ }
604
+ } else {
605
+ const nodeList = elementSelector.selectorAll(selector, parent);
606
+ if (nodeList.length) {
607
+ return nodeList;
608
+ }
609
+ }
610
+ }
611
+ return UtilsContext.wait<any>(
612
+ () => {
613
+ const node = getNodeList();
614
+ if (node) {
615
+ return {
616
+ success: true,
617
+ data: node,
618
+ };
619
+ } else {
620
+ return {
621
+ success: false,
622
+ data: node,
623
+ };
624
+ }
625
+ },
626
+ timeout,
627
+ parent
628
+ );
629
+ }
630
+ /**
631
+ * 等待任意元素数组出现
632
+ * @param selectorList CSS选择器数组
633
+ * @param parent (可选)监听的父元素
634
+ * @example
635
+ * DOMUtils.waitAnyNodeList(["div","a"]).then( $result =>{
636
+ * console.log($result); // $result => NodeListOf<HTMLDivElement>
637
+ * })
638
+ * DOMUtils.waitAnyNodeList(["div","a"], document).then( $result =>{
639
+ * console.log($result); // $result => NodeListOf<HTMLDivElement>
640
+ * })
641
+ */
642
+ waitAnyNodeList<K extends keyof HTMLElementTagNameMap>(
643
+ selectorList: K[],
644
+ parent?: Node | Element | Document | HTMLElement
645
+ ): Promise<NodeListOf<HTMLElementTagNameMap[K]>>;
646
+ waitAnyNodeList<T extends Element = HTMLElement>(
647
+ selectorList: string[],
648
+ parent?: Node | Element | Document | HTMLElement
649
+ ): Promise<NodeListOf<T>>;
650
+ /**
651
+ * 等待任意元素数组出现
652
+ * @param selectorList CSS选择器数组
653
+ * @param parent 父元素,默认document
654
+ * @param timeout 超时时间,默认0
655
+ * @example
656
+ * DOMUtils.waitAnyNodeList(["div","a"], document, 10000).then( $result =>{
657
+ * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
658
+ * })
659
+ */
660
+ waitAnyNodeList<K extends keyof HTMLElementTagNameMap>(
661
+ selectorList: K[],
662
+ parent: Node | Element | Document | HTMLElement,
663
+ timeout: number
664
+ ): Promise<NodeListOf<HTMLElementTagNameMap[K]> | null>;
665
+ waitAnyNodeList<T extends Element = HTMLElement>(
666
+ selectorList: string[],
667
+ parent: Node | Element | Document | HTMLElement,
668
+ timeout: number
669
+ ): Promise<NodeListOf<T> | null>;
670
+ /**
671
+ * 等待任意元素出现
672
+ * @param selectorList CSS选择器数组
673
+ * @param timeout 超时时间,默认0
674
+ * @example
675
+ * DOMUtils.waitAnyNodeList(["div","div"], 10000).then( $result =>{
676
+ * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
677
+ * })
678
+ */
679
+ waitAnyNodeList<K extends keyof HTMLElementTagNameMap>(
680
+ selectorList: K[],
681
+ timeout: number
682
+ ): Promise<NodeListOf<HTMLElementTagNameMap[K]> | null>;
683
+ waitAnyNodeList<T extends Element = HTMLElement>(
684
+ selectorList: string[],
685
+ timeout: number
686
+ ): Promise<NodeListOf<T> | null>;
687
+ waitAnyNodeList<T extends Element = HTMLElement>(...args: any[]): Promise<NodeListOf<T> | null> {
688
+ // 过滤掉undefined
689
+ args = args.filter((arg) => arg !== void 0);
690
+ const UtilsContext = this;
691
+ // 选择器数组
692
+ const selectorList = args[0] as unknown as string[];
693
+ // 父元素(监听的元素)
694
+ let parent: Element = UtilsContext.windowApi.document as any as Element;
695
+ // 超时时间
696
+ let timeout = 0;
697
+ if (!Array.isArray(args[0])) {
698
+ throw new TypeError("DOMUtils.waitAnyNodeList 第一个参数必须是string[]");
699
+ }
700
+ if (args.length === 1) {
701
+ // 上面已做处理
702
+ } else if (args.length === 2) {
703
+ const secondParam = args[1];
704
+ if (typeof secondParam === "number") {
705
+ // "div",10000
706
+ timeout = secondParam;
707
+ } else if (typeof secondParam === "object" && secondParam instanceof Node) {
708
+ // "div",document
709
+ parent = secondParam as any as Element;
710
+ } else {
711
+ throw new TypeError("DOMUtils.waitAnyNodeList 第二个参数必须是number|Node");
712
+ }
713
+ } else if (args.length === 3) {
714
+ // "div",document,10000
715
+ // 第二个参数,parent
716
+ const secondParam = args[1];
717
+ // 第三个参数,timeout
718
+ const thirdParam = args[2];
719
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
720
+ parent = secondParam as any as Element;
721
+ if (typeof thirdParam === "number") {
722
+ timeout = thirdParam;
723
+ } else {
724
+ throw new TypeError("DOMUtils.waitAnyNodeList 第三个参数必须是number");
725
+ }
726
+ } else {
727
+ throw new TypeError("DOMUtils.waitAnyNodeList 第二个参数必须是Node");
728
+ }
729
+ } else {
730
+ throw new TypeError("DOMUtils.waitAnyNodeList 参数个数错误");
731
+ }
732
+
733
+ const promiseList = selectorList.map((selector) => {
734
+ return UtilsContext.waitNodeList<NodeListOf<T>>(selector, parent, timeout);
735
+ });
736
+ return Promise.any(promiseList);
737
+ }
738
+ }
739
+
740
+ const elementWait = new ElementWait();
741
+
742
+ export { elementWait, ElementWait };