-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: Add ability to inject script to run on page load #568
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
base: main
Are you sure you want to change the base?
Conversation
| **Parameters:** | ||
|
|
||
| - **ignoreCache** (boolean) _(optional)_: Whether to ignore cache on reload. | ||
| - **initScript** (string) _(optional)_: (optional) A JavaScript function to be executed by the tool on new document for every page load before any other scripts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the PR! I have asked a few details on the issue. Do you think the new_page tool could be a better place for the init script? Any thoughts how to remove a previously specified init script and if removing it is needed? For your use case, could it be better to pass an init script for all pages via a CLI argument or do you want the LLM to generate the init script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't thought that much about the different approaches to be honest.
I would be fine to just close this PR for now as it's very minimal (it's just a snapshot basically of what I'm playing with locally at the moment) and keep the discussions in the issues for now to land on a good / better general solution for the problem I'm trying to solve :)
OrKoN
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I think we can land it with a few tweaks. Let me know if you still have a chance to update the PR.
| } | ||
|
|
||
| if (request.params.initScript) { | ||
| await page.evaluateOnNewDocument(request.params.initScript); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove the script once the navigation is over (see https://pptr.dev/api/puppeteer.page.removescripttoevaluateonnewdocument)
| .string() | ||
| .optional() | ||
| .describe( | ||
| '(optional) A JavaScript function to be executed by the tool on new document for every page load before any other scripts.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| '(optional) A JavaScript function to be executed by the tool on new document for every page load before any other scripts.', | |
| 'A JavaScript function to be executed by the tool on the new document before any other scripts for the next page load.', |
Adds a parameter "initScript" to be used to inject a script into the page load before other scripts execute.
Page.evaluateOnNewDocument
See #567