Skip to content

fix(browser-tools): page selection and disconnect hang bugs - #24

Open
iRonin wants to merge 1 commit into
badlogic:mainfrom
iRonin:fix/browser-tools-page-selection-and-disconnect
Open

fix(browser-tools): page selection and disconnect hang bugs#24
iRonin wants to merge 1 commit into
badlogic:mainfrom
iRonin:fix/browser-tools-page-selection-and-disconnect

Conversation

@iRonin

@iRonin iRonin commented Apr 8, 2026

Copy link
Copy Markdown

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 scripts

In 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 undefined in screenshot/eval/cookies when any extension is loaded.

Fix: Filter targets by page.target().type() === 'page', fall back to pages[0].

2. b.disconnect() hangs indefinitely

Calling await b.disconnect() after connecting to an already-running Chrome instance via puppeteer-core.connect() never resolves — the promise hangs forever, making every script invocation require SIGKILL.

Before: Every script hangs after completing its task. Can't chain commands (e.g., nav → screenshot → eval) in automation.

Fix: Replace await b.disconnect() with process.exit(0).

Files Changed

File Change
browser-screenshot.js Fix page selection + remove disconnect()
browser-eval.js Fix page selection + remove disconnect()
browser-nav.js Fix page selection + remove disconnect()
browser-content.js Fix page selection (already had process.exit)
browser-cookies.js Fix page selection + remove disconnect()
browser-pick.js Fix page selection + remove disconnect()

Verified Working

All 6 tools tested end-to-end against a live Chrome instance with extensions loaded:

  • Navigate ✅
  • Screenshot ✅
  • JS Eval ✅
  • Cookies ✅
  • Pick helper injection ✅
  • Content extraction ✅

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant