From bb4cd7195c2f88ec45f6aa4f6a544d9a0246ea54 Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Thu, 26 Jan 2023 23:11:48 +0000 Subject: [PATCH] ipc: add .on --- gluon.d.ts | 14 +++++++++++++- src/launcher/inject.js | 12 +++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/gluon.d.ts b/gluon.d.ts index 96c7615..815446b 100644 --- a/gluon.d.ts +++ b/gluon.d.ts @@ -179,7 +179,19 @@ type CDPApi = { * @default true */ useSessionId?: boolean - ): Promise + ): Promise, + + /** + * 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 = { diff --git a/src/launcher/inject.js b/src/launcher/inject.js index 208af8d..c2c1bee 100644 --- a/src/launcher/inject.js +++ b/src/launcher/inject.js @@ -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: () => {