ipc: add .on

This commit is contained in:
CanadaHonk 2023-01-26 23:11:48 +00:00
parent e1bb814c88
commit bb4cd7195c
2 changed files with 24 additions and 2 deletions

14
gluon.d.ts vendored
View File

@ -179,7 +179,19 @@ type CDPApi = {
* @default true
*/
useSessionId?: boolean
): Promise<any>
): Promise<any>,
/**
* Hook into a specific CDP method being called.
* @returns Function to unhook/unsubscribe.
*/
on(
/** Method of CDP command to hook into. */
method: string,
/** Callback to run when the given method is emitted. */
callback: (message: any) => void
): (() => void)
};
type IdleAutoOptions = {

View File

@ -109,7 +109,17 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN
ipc: IPC,
cdp: {
send: (method, params, useSessionId = true) => CDP.sendMessage(method, params, useSessionId ? sessionId : undefined)
send: (method, params, useSessionId = true) => CDP.sendMessage(method, params, useSessionId ? sessionId : undefined),
on: (method, handler, once = false) => {
const unhook = CDP.onMessage(msg => {
if (msg.method === method) {
handler(msg);
if (once) unhook();
}
});
return unhook;
}
},
close: () => {