Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-jobs-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

fix issue where scripts added via context.addInitScripts() were not being injected into new pages that were opened via popups (eg, clicking a link that opens a new page) and/or calling context.newPage(url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: addInitScripts() should be addInitScript() (no 's' at the end)

Prompt To Fix With AI
This is a comment left during a code review.
Path: .changeset/small-jobs-drum.md
Line: 5:5

Comment:
typo: `addInitScripts()` should be `addInitScript()` (no 's' at the end)

How can I resolve this? If you propose a fix, please make it concise.

81 changes: 81 additions & 0 deletions packages/core/lib/v3/tests/context-addInitScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,87 @@ test.describe("context.addInitScript", () => {
expect(observed).toEqual(payload);
});

test("applies script to newPage(url) on initial document", async () => {
const payload = { marker: "newPageUrl" };

await ctx.addInitScript((arg) => {
function setPayload(): void {
const root = document.documentElement;
if (!root) return;
root.dataset.initPayload = JSON.stringify(arg);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", setPayload, {
once: true,
});
} else {
setPayload();
}
}, payload);

const newPage = await ctx.newPage(
toDataUrl("<html><body>new page</body></html>"),
);
await newPage.waitForLoadState("load");

const observed = await newPage.evaluate(() => {
const raw = document.documentElement.dataset.initPayload;
return raw ? JSON.parse(raw) : undefined;
});
expect(observed).toEqual(payload);
});

test("applies script to pages opened via link clicks", async () => {
const payload = { marker: "linkClick" };

await ctx.addInitScript((arg) => {
function setPayload(): void {
const root = document.documentElement;
if (!root) return;
root.dataset.initPayload = JSON.stringify(arg);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", setPayload, {
once: true,
});
} else {
setPayload();
}
}, payload);

const popupUrl = toDataUrl("<html><body>popup</body></html>");
const openerHtml =
"<!DOCTYPE html>" +
"<html><body>" +
'<a id="open" target="_blank" href="' +
popupUrl +
'">open</a>' +
"</body></html>";

const opener = await ctx.awaitActivePage();
await opener.goto(toDataUrl(openerHtml), { waitUntil: "load" });
await opener.locator("#open").click();

const openerId = opener.targetId();
const deadline = Date.now() + 2000;
let popup = ctx.pages().find((p) => p.targetId() !== openerId);
while (!popup && Date.now() < deadline) {
await opener.waitForTimeout(25);
popup = ctx.pages().find((p) => p.targetId() !== openerId);
}
if (!popup) {
throw new Error("Popup page was not created");
}

await popup.waitForLoadState("load");

const observed = await popup.evaluate(() => {
const raw = document.documentElement.dataset.initPayload;
return raw ? JSON.parse(raw) : undefined;
});
expect(observed).toEqual(payload);
});

test("context.addInitScript installs a function callable from page.evaluate", async () => {
const page = await ctx.awaitActivePage();

Expand Down
7 changes: 1 addition & 6 deletions packages/core/lib/v3/understudy/cdp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,7 @@ export class CdpConnection implements CDPSessionLike {
await this.send("Target.setAutoAttach", {
autoAttach: true,
flatten: true,
waitForDebuggerOnStart: false,
filter: [
{ type: "worker", exclude: true },
{ type: "shared_worker", exclude: true },
{ type: "service_worker", exclude: true },
],
waitForDebuggerOnStart: true,
Copy link
Member

@pirate pirate Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might have a stealth/reliability impact, gaps in runtime performance metrics as a result of debuggerOnStart are one of the signals bot-blockers use iirc. Also there are weird situations with using waitForDebuggerOnStart on service workers.

might be worth only setting this if there are initScripts needed

Further reading on issues with waitForDebuggerOnStart:

});
await this.send("Target.setDiscoverTargets", { discover: true });
}
Expand Down
Loading
Loading