fix(browser): remote profile tab ops follow-up (#1060) (thanks @mukhtharcm)

Landed via follow-up to #1057.

Gate: pnpm lint && pnpm build && pnpm test
This commit is contained in:
Peter Steinberger
2026-01-17 01:28:22 +00:00
committed by GitHub
parent e16ce1a0a1
commit a76cbc43bb
8 changed files with 232 additions and 48 deletions

View File

@@ -480,3 +480,31 @@ export async function closePageByTargetIdViaPlaywright(opts: {
}
await page.close();
}
/**
* Focus a page/tab by targetId using the persistent Playwright connection.
* Used for remote profiles where HTTP-based /json/activate can be ephemeral.
*/
export async function focusPageByTargetIdViaPlaywright(opts: {
cdpUrl: string;
targetId: string;
}): Promise<void> {
const { browser } = await connectBrowser(opts.cdpUrl);
const page = await findPageByTargetId(browser, opts.targetId);
if (!page) {
throw new Error("tab not found");
}
try {
await page.bringToFront();
} catch (err) {
const session = await page.context().newCDPSession(page);
try {
await session.send("Page.bringToFront");
return;
} catch {
throw err;
} finally {
await session.detach().catch(() => {});
}
}
}