d-_-b
d-_-b

Reputation: 23181

How to get the document response and run javascript as soon as possible in Puppeteer?

Most Puppeteer questions focus around how to ensure the page has fully loaded and rendered. My question is the opposite:

Using Puppeteer, how can I get javascript access as soon as possible without having to wait for the load or domcontentloaded events?

When is it safe to run page.evaluate at the earliest opportunity, if I don't want/need to wait for the full page to render?

Upvotes: 0

Views: 270

Answers (1)

Thomas Dondorf
Thomas Dondorf

Reputation: 25240

You are looking for the page.evaluateOnNewDocument function. From the docs:

The function is invoked 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.

Be aware that you have to call page.evaluateOnNewDocument before page.goto.

Upvotes: 1

Related Questions