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:
parent
b8c3686e6d
commit
3b44de444f
26
README.md
26
README.md
@ -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.
|
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
|
## Default runtime settings
|
||||||
|
|
||||||
**1. Uses Headless mode**
|
**1. Uses Headless mode**
|
||||||
|
Loading…
Reference in New Issue
Block a user