Skip to content

Conversation

@seanmcguire12
Copy link
Member

@seanmcguire12 seanmcguire12 commented Nov 21, 2025

why

  • adds a public facing API to inject scripts into new pages before existing JS is run

what changed

  • added context.addInitScripts() which calls Page.addScriptToEvaluateOnNewDocument whenever we attach to a page target

test plan

  • added unit tests which check that the injected scripts are run before existing scripts

@changeset-bot
Copy link

changeset-bot bot commented Nov 21, 2025

🦋 Changeset detected

Latest commit: b45d574

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@browserbasehq/stagehand Patch
@browserbasehq/stagehand-evals Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@seanmcguire12
Copy link
Member Author

@greptileai

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 21, 2025

Greptile Overview

Greptile Summary

This PR adds context.addInitScript() which injects JavaScript into pages before any existing scripts run, mirroring Playwright's API. The implementation properly handles script normalization (functions, strings, or file paths), applies scripts to all existing pages and their OOPIF sessions, and ensures new pages receive the scripts automatically.

Major changes:

  • Added normalizeInitScriptSource() to handle function serialization with arguments, file path loading, and sourceURL annotations
  • Implemented context.addInitScript() that stores scripts and applies them to all pages
  • Added page.registerInitScript() to fan out scripts to all CDP sessions (main + OOPIFs)
  • Enhanced OOPIF adoption to replay init scripts via applyInitScriptsToSession()
  • Comprehensive test coverage validates script execution order and cross-navigation persistence

Confidence Score: 5/5

  • This PR is safe to merge with no issues found
  • The implementation is well-designed with proper error handling, follows existing patterns in the codebase, includes comprehensive test coverage, and correctly handles complex scenarios like OOPIF sessions and navigation persistence
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
.changeset/tender-moons-laugh.md 5/5 Standard changeset file documenting the new context.addInitScript() feature
packages/core/lib/v3/tests/context-addInitScript.spec.ts 5/5 Comprehensive test coverage for init script injection, including navigation persistence and argument passing
packages/core/lib/v3/understudy/context.ts 5/5 Implements context.addInitScript() with proper script normalization, applies scripts to existing and new pages
packages/core/lib/v3/understudy/page.ts 5/5 Handles per-page init script registration and application to all sessions including OOPIFs

Sequence Diagram

sequenceDiagram
    participant User
    participant V3Context
    participant Page
    participant CDPSession
    participant Browser

    User->>V3Context: addInitScript(script, arg?)
    V3Context->>V3Context: normalizeInitScriptSource(script, arg)
    V3Context->>V3Context: store script in initScripts[]
    V3Context->>V3Context: pages()
    loop For each existing page
        V3Context->>Page: registerInitScript(source)
        Page->>Page: check if script already registered
        Page->>Page: add to initScripts[]
        Page->>CDPSession: Page.addScriptToEvaluateOnNewDocument
        loop For all sessions (main + OOPIFs)
            CDPSession->>Browser: Register init script
        end
    end

    Note over User,Browser: When new page is created

    User->>V3Context: newPage()
    V3Context->>Browser: Target.createTarget
    Browser->>V3Context: attachedToTarget event
    V3Context->>Page: create Page instance
    V3Context->>V3Context: applyInitScriptsToPage(page)
    loop For each stored script
        V3Context->>Page: registerInitScript(source)
        Page->>CDPSession: Page.addScriptToEvaluateOnNewDocument
        CDPSession->>Browser: Register init script
    end

    Note over User,Browser: When OOPIF is attached

    Browser->>Page: adoptOopifSession(childSession)
    Page->>Page: applyInitScriptsToSession(childSession)
    loop For each stored script
        Page->>CDPSession: Page.addScriptToEvaluateOnNewDocument
        CDPSession->>Browser: Register init script for OOPIF
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@seanmcguire12 seanmcguire12 marked this pull request as ready for review November 22, 2025 00:13
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@seanmcguire12 seanmcguire12 merged commit 2b70347 into main Nov 24, 2025
15 checks passed
@github-actions github-actions bot mentioned this pull request Nov 21, 2025
seanmcguire12 pushed a commit that referenced this pull request Nov 24, 2025
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @browserbasehq/stagehand@3.0.4

### Patch Changes

- [#1281](#1281)
[`fa18cfd`](fa18cfd)
Thanks [@monadoid](https://github.com/monadoid)! - Add Browserbase
session URL and debug URL accessors

- [#1264](#1264)
[`767d168`](767d168)
Thanks [@Kylejeong2](https://github.com/Kylejeong2)! - feat: adding gpt
5.1 to stagehand

- [#1282](#1282)
[`f27a99c`](f27a99c)
Thanks [@tkattkat](https://github.com/tkattkat)! - Add support for zod
4, while maintaining backwards compatibility for zod 3

- [#1295](#1295)
[`91a1ca0`](91a1ca0)
Thanks [@tkattkat](https://github.com/tkattkat)! - Patch zod handling of
non objects in extract

- [#1298](#1298)
[`1dd7d43`](1dd7d43)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - log
Browserbase session status when websocket is closed due to session
timeout

- [#1284](#1284)
[`c0f3b98`](c0f3b98)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix:
waitForDomNetworkQuiet() causing `act()` to hang indefinitely

- [#1246](#1246)
[`44bb4f5`](44bb4f5)
Thanks [@filip-michalsky](https://github.com/filip-michalsky)! - make ci
faster

- [#1300](#1300)
[`2b70347`](2b70347)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add support
for context.addInitScript()

## @browserbasehq/stagehand-evals@1.1.4

### Patch Changes

- Updated dependencies
\[[`fa18cfd`](fa18cfd),
[`767d168`](767d168),
[`f27a99c`](f27a99c),
[`91a1ca0`](91a1ca0),
[`1dd7d43`](1dd7d43),
[`c0f3b98`](c0f3b98),
[`44bb4f5`](44bb4f5),
[`2b70347`](2b70347)]:
    -   @browserbasehq/stagehand@3.0.4

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

3 participants