I’m trying to switch from PhantomJS to Puppeteer. In my old code, I used the onInitialized method to set a global variable that the page could use when it first loaded. But I can’t figure out how to do this in Puppeteer.
I’ve tried a few things:
Using evaluate() after creating the page but before loading it. This sets the variable, but it gets reset when the page loads.
Setting it in the domcontentloaded event. This works, but it’s too late for my needs.
Setting it after page.goto(). This is definitely too late.
hey sophialee92, have u tried using page.evaluateOnNewDocument()? it runs before any scripts on the page, so it should work like phantomjs’s onInitialized. heres an example:
I’ve been using Puppeteer for a while now, and I found that the setExtraHTTPHeaders method can be quite useful for this scenario. It allows you to set custom headers that persist through navigation, which you can then access in your page scripts. Here’s how I typically do it:
This approach has worked well for me as it doesn’t require request interception and allows for more complex configurations. Just remember to parse the JSON on the page side. It’s a bit of a workaround, but it’s reliable and doesn’t interfere with page loading.
I’ve encountered a similar challenge when migrating from PhantomJS to Puppeteer. The solution that worked for me was using page.setRequestInterception(true) combined with page.on(‘request’). This approach allows you to intercept and modify requests before they’re sent, giving you an opportunity to inject your global variables.