Repository navigation
Add --init-script option for running scripts before page load - #207
Open
ChrisJr404 wants to merge 1 commit into
Open
ChrisJr404 wants to merge 1 commit into
ChrisJr404 wants to merge 1 commit into
Conversation
Registers JavaScript as a Playwright init script so it runs on every navigation before the page's own scripts, useful for seeding Math.random, freezing Date or hiding navigator.webdriver. Available on shot, multi, javascript, accessibility, pdf, html and har. Refs simonw#147
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a
--init-scriptoption that runs JavaScript before the page's own scripts, closing #147.The existing
-j/--javascriptoption runs code after the page has loaded, but sometimes you need to run something first - to seedMath.random(), freezeDate, or deletenavigator.webdriverso a site doesn't detect the headless browser (the news.com.au example in the issue).--init-scriptregisters the code as a Playwright init script, which fires on every navigation before any page scripts run.I wired it in at the
_browser_context()level withcontext.add_init_script(), so a single implementation covers all of the navigating commands -shot,multi,javascript,accessibility,pdf,htmlandhar- and it naturally applies to every page in themulticommand's shared context. The option takesmultiple=Trueso you can register several scripts. Everything is additive and default behaviour is unchanged.Following the shape of your prototype in fae9bab, though registering at the context level rather than per-page.
Testing: I added three end-to-end tests against the real browser in
test_shot_scraper.py- one basic injection, one that proves the script runs before the page's own inline<script>(the page observes the injected value at parse time), and one for multiple--init-scriptoptions. The full non-video suite passes locally, and I regenerated the Cog help blocks in the docs (cog --checkis clean) plus added a short docs section under javascript.md.📚 Documentation preview 📚: https://shot-scraper--207.org.readthedocs.build/en/207/