readme: add evaluate snippet (#524)

* readme: add evaluate snippet

pretty important method that we don't have a prominent example for.

* feedback

* Update README.md
This commit is contained in:
Eric Bidelman 2017-08-24 09:13:49 -07:00 committed by GitHub
parent b8c3686e6d
commit 3b44de444f

View File

@ -70,6 +70,32 @@ const puppeteer = require('puppeteer');
See [`Page.pdf()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions) for more information about creating pdfs.
**Example** - evaluate script in the context of the page
```js
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// Get the "viewport" of the page, as reported by the page.
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
};
});
console.log('Dimensions:', dimensions);
browser.close();
})();
See [`Page.evaluate()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageevaluatepagefunction-args) for more information on `evaluate` and related methods like `evaluateOnNewDocument` and `exposeFunction`.
## Default runtime settings
**1. Uses Headless mode**