(options?: {
/**
* 视频选择器
*/
videoSelector?: string;
/**
* 音频选择器
*/
audioSelector?: string;
/**
* 根元素
*/
root?: HTMLElement | Document;
timeout?: number;
filter?: (video: HTMLVideoElement | HTMLAudioElement) => boolean;
})
| 2 | * 等待视频加载并获取视频 |
| 3 | */ |
| 4 | export async function waitForMedia(options?: { |
| 5 | /** |
| 6 | * 视频选择器 |
| 7 | */ |
| 8 | videoSelector?: string; |
| 9 | /** |
| 10 | * 音频选择器 |
| 11 | */ |
| 12 | audioSelector?: string; |
| 13 | /** |
| 14 | * 根元素 |
| 15 | */ |
| 16 | root?: HTMLElement | Document; |
| 17 | timeout?: number; |
| 18 | filter?: (video: HTMLVideoElement | HTMLAudioElement) => boolean; |
| 19 | }) { |
| 20 | const timeoutMs = options?.timeout ?? 3 * 60 * 1000; |
| 21 | |
| 22 | const res = await new Promise<HTMLVideoElement | HTMLAudioElement>((resolve, reject) => { |
| 23 | // eslint-disable-next-line prefer-const |
| 24 | let timeoutId: ReturnType<typeof setTimeout> | undefined = undefined; |
| 25 | const interval = setInterval(() => { |
| 26 | const video = (options?.root || document).querySelector<HTMLVideoElement | HTMLAudioElement>( |
| 27 | `${options?.videoSelector || 'video'},${options?.audioSelector || 'audio'}` |
| 28 | ); |
| 29 | if (video && (!options?.filter || options.filter(video))) { |
| 30 | clearInterval(interval); |
| 31 | if (timeoutId !== undefined) { |
| 32 | clearTimeout(timeoutId); |
| 33 | } |
| 34 | resolve(video); |
| 35 | } |
| 36 | }, 200); |
| 37 | |
| 38 | timeoutId = setTimeout(() => { |
| 39 | clearInterval(interval); |
| 40 | reject(new Error('视频/音频未找到,或者加载超时。')); |
| 41 | }, timeoutMs); |
| 42 | }); |
| 43 | |
| 44 | return res; |
| 45 | } |
| 46 | |
| 47 | export function waitForElement( |
| 48 | selector: string | { (): HTMLElement | undefined }, |
no outgoing calls
no test coverage detected