Skip to content

feat: support external browser + page in puppeteer options#201677

Open
Adi1231234 wants to merge 1 commit intowwebjs:mainfrom
Adi1231234:feat/external-browser-page
Open

feat: support external browser + page in puppeteer options#201677
Adi1231234 wants to merge 1 commit intowwebjs:mainfrom
Adi1231234:feat/external-browser-page

Conversation

@Adi1231234
Copy link
Contributor

Adds support for passing a pre-existing browser and page via options.puppeteer, so the Client can work with a browser you already control (e.g. Electron's built-in Chromium, or a shared browser pool).

When both browser and page are provided, the Client skips puppeteer.launch() / puppeteer.connect() and uses them directly. Existing behavior is unchanged when these options are not set.

What changed

initialize() - new if branch before the existing browserWSEndpoint check:

if (puppeteerOpts.browser && puppeteerOpts.page) {
    browser = puppeteerOpts.browser;
    page = puppeteerOpts.page;
}

destroy() / logout() - use browser.process() to decide cleanup:

  • process() !== null (we launched it) -> browser.close() as before
  • process() === null (external browser) -> browser.disconnect() only

Without this, browser.close() sends CDP Browser.close which kills the entire external browser, not just this client's session. disconnect() only closes the WebSocket, leaving the browser running.

This is the same pattern puppeteer uses internally in Symbol.dispose.

Example

const browser = await puppeteer.connect({ browserWSEndpoint: '...' });
const page = /* your page */;

const client = new Client({
    puppeteer: { browser, page }
});
await client.initialize();

@github-actions github-actions bot added the api changes API modifications label Mar 20, 2026
allow passing a pre-existing `browser` and `page` via
`options.puppeteer` so the Client can work with a browser
you already control.

when both are provided, the Client skips puppeteer.launch()
and uses them directly.

also uses browser.process() in destroy/logout to decide
cleanup: close() for launched browsers, disconnect() for
connected ones.
@Adi1231234 Adi1231234 force-pushed the feat/external-browser-page branch from b654043 to ff2e280 Compare March 20, 2026 19:51
@github-actions github-actions bot added the typings Type definitions label Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api changes API modifications typings Type definitions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant