MCPcopy
hub / github.com/ocsjs/ocsjs / waitForMedia

Function waitForMedia

packages/scripts/src/utils/study.ts:4–45  ·  view source on GitHub ↗
(options?: {
	/**
	 * 视频选择器
	 */
	videoSelector?: string;
	/**
	 * 音频选择器
	 */
	audioSelector?: string;
	/**
	 * 根元素
	 */
	root?: HTMLElement | Document;
	timeout?: number;
	filter?: (video: HTMLVideoElement | HTMLAudioElement) => boolean;
})

Source from the content-addressed store, hash-verified

2 * 等待视频加载并获取视频
3 */
4export 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
47export function waitForElement(
48 selector: string | { (): HTMLElement | undefined },

Callers 10

mediaFunction · 0.90
watchMediaFunction · 0.90
studyFunction · 0.90
watchMediaFunction · 0.90
watchMediaFunction · 0.90
doWorkFunction · 0.90
setFunction · 0.90
watchFunction · 0.90
watchXnkFunction · 0.90
jobFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected