Skip to content
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

--init-script support #147

Open
simonw opened this issue Mar 25, 2024 · 3 comments
Open

--init-script support #147

simonw opened this issue Mar 25, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Mar 25, 2024

Init scripts are special JavaScript that gets run to prime the page before the URL is loaded:

https://playwright.dev/python/docs/api/class-page#page-add-init-script

Adds a script which would be evaluated in one of the following scenarios:

  • Whenever the page is navigated.
  • Whenever the child frame is attached or navigated. In this case, the script is evaluated in the context of the newly attached frame.

The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend the JavaScript environment, e.g. to seed Math.random.

This should be an option for shot and javascript and more.

@simonw simonw added the enhancement New feature or request label Mar 25, 2024
simonw added a commit that referenced this issue Mar 25, 2024
@simonw
Copy link
Owner Author

simonw commented Mar 25, 2024

One thing this can be useful for is taking screenshots of pages that detect and block headless Chrome. They seem to often do that by looking for navigator.webdriver.

https://www.news.com.au/ is an example:

shot-scraper https://www.news.com.au/  -h 600

www-news-com-au

But using the prototype from fae9bab and a tip from https://stackoverflow.com/a/75771301/6083

shot-scraper https://www.news.com.au/ -h 600 \
  --init-script 'delete Object.getPrototypeOf(navigator).webdriver' \
  --user-agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0'

www-news-com-au

@simonw
Copy link
Owner Author

simonw commented Mar 25, 2024

Asked ChatGPT for more ideas of things to do with init scripts: https://chat.openai.com/share/71c5302f-bb92-4bd8-8eb3-311d855311b0

A few that I really liked

browser_context.add_init_script("""
    Date.now = function() { return new Date('2024-01-01T00:00:00Z').getTime(); };
""")


browser_context.add_init_script("""
    const originalFetch = window.fetch;
    window.fetch = async function(...args) {
        if (args[0].includes('api.example.com')) {
            return new Response(JSON.stringify({ mocked: true }), { status: 200 });
        }
        return originalFetch(...args);
    };
""")

browser_context.add_init_script("""
    localStorage.setItem('key', 'value');
    document.cookie = 'name=value; path=/';
""")

@simonw
Copy link
Owner Author

simonw commented Mar 25, 2024

Claude 3 Opus suggested "Simulate a specific device":

   page.add_init_script("""
       Object.defineProperty(window, 'innerWidth', {
           writable: true,
           configurable: true,
           value: 375,
       });
       Object.defineProperty(window, 'innerHeight', {
           writable: true,
           configurable: true,
           value: 812,
       });
   """)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant