fix(browser-tools): page selection and disconnect hang bugs - #24
Open
iRonin wants to merge 1 commit into
Open
Conversation
Fix two bugs across all 6 browser CDP scripts: 1. (await b.pages()).at(-1) grabs service_worker/background_page targets instead of real tabs. puppeteer's .pages() returns ALL targets including extension service workers and background pages. In users with extensions (near-universal), .at(-1) selects an un-actionable target, causing TypeErrors in screenshot/eval/cookies. Fix: filter by page.target().type() === 'page', fall back to pages[0]. 2. await b.disconnect() after puppeteer-core.connect() never resolves. Connecting to an already-running Chrome instance via CDP creates a connection that b.disconnect() will never close, causing all scripts to hang after completing their task. Fix: replace await b.disconnect() with process.exit(0). Affected files: browser-screenshot.js, browser-eval.js, browser-nav.js, browser-content.js, browser-cookies.js, browser-pick.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two bugs across all 6 browser CDP scripts that break common usage scenarios.
Bugs Fixed
1.
.at(-1)page selection grabs service workers, crashes scriptsIn puppeteer-core v23+,
browser.pages()returns all CDP targets including extension service workers and background pages. The original code used.at(-1)to pick the last page, which typically selects a service worker or background page when Chrome extensions are installed.Before: Crashes with
TypeError: Cannot read properties of undefinedin screenshot/eval/cookies when any extension is loaded.Fix: Filter targets by
page.target().type() === 'page', fall back topages[0].2.
b.disconnect()hangs indefinitelyCalling
await b.disconnect()after connecting to an already-running Chrome instance viapuppeteer-core.connect()never resolves — the promise hangs forever, making every script invocation requireSIGKILL.Before: Every script hangs after completing its task. Can't chain commands (e.g., nav → screenshot → eval) in automation.
Fix: Replace
await b.disconnect()withprocess.exit(0).Files Changed
Verified Working
All 6 tools tested end-to-end against a live Chrome instance with extensions loaded: