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