UNPKG

@yiero/gmlib

Version:

GM Lib for Tampermonkey/ScriptCat

60 lines (59 loc) 1.86 kB
/** * 元素等待器选项 */ export interface IElementWaiterOption { /** * 监听器容器 * * @default document */ parent: HTMLElement | DocumentFragment | Document; /** * 超时时间 * * @default 20 */ timeoutPerSecond: number; /** * 监听到元素触发后, 延时获取元素的时间 * * @default .5 */ delayPerSecond: number; } /** * 延时获取并返回元素 * * 在指定延迟后尝试获取元素,成功则 resolve,失败则 reject */ export declare const returnElement: <T>(selector: string, options: IElementWaiterOption, resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void; /** * 通过定时器轮询获取元素 * * 当浏览器不支持 MutationObserver 时使用的降级方案 */ export declare const getElementByTimer: <T>(selector: string, options: IElementWaiterOption, resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void; /** * 等待页面中匹配指定选择器的元素出现 * * 当网站使用 Vue、React 等框架时,页面完全载入时(load 事件触发) * 并非所有元素都已加载到页面,此时需要使用此函数等待元素载入。 * * @param selector CSS 选择器 * @param options 配置选项(可选) * @returns Promise<T> 匹配的 HTMLElement 元素 * * @example * ```ts * // 等待 id 为 'app' 的元素出现 * const appElement = await elementWaiter('#app'); * console.log(appElement); * * // 指定父容器和超时时间 * const element = await elementWaiter('.item', { * parent: document.querySelector('.container'), * timeoutPerSecond: 10 * }); * ``` */ export declare function elementWaiter<T extends HTMLElement>(selector: string, options?: Partial<IElementWaiterOption>): Promise<T>;