Skip to content

proxy page.evaluate() #688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 23, 2025
Merged

proxy page.evaluate() #688

merged 5 commits into from
Apr 23, 2025

Conversation

seanmcguire12
Copy link
Member

@seanmcguire12 seanmcguire12 commented Apr 19, 2025

why

  • sometimes our browser side scripts will get overwritten or removed
  • this is problematic, and results in an error like: window._browser_side_function is not a function

what changed

  • wrapped .evaluate() in our own .evaluate() function which checks to make sure our browser side functions still exist before attempting to call them. if they do not exist, they are re-injected

test plan

  • added a test

Copy link

changeset-bot bot commented Apr 19, 2025

🦋 Changeset detected

Latest commit: 7cb8562

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

This PR includes changesets to release 1 package
Name Type
@browserbasehq/stagehand 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 seanmcguire12 marked this pull request as ready for review April 21, 2025 18:48
Copy link

@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.

PR Summary

This PR implements script persistence by wrapping page.evaluate() to ensure browser-side functions remain available, with automatic re-injection if scripts are removed or overwritten.

  • Added ensureStagehandScript() in lib/StagehandPage.ts to verify and re-inject scripts before evaluation
  • Introduced __stagehandInjected flag in lib/dom/global.d.ts to track script injection state
  • Modified page proxy to intercept evaluate-related methods (evaluate, evaluateHandle, $eval, $$eval) for script verification
  • Added test case in addInitScript.test.ts to verify automatic re-injection when scripts are deleted
  • Implemented guard clause in script injection to prevent duplicate injections

4 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

@seanmcguire12 seanmcguire12 merged commit 7d81b3c into main Apr 23, 2025
13 checks passed
@github-actions github-actions bot mentioned this pull request Apr 23, 2025
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.

2 participants