Last active
September 19, 2017 08:58
-
-
Save embiem/d0f32e495aa9c6e12597a9c5f8f204fe to your computer and use it in GitHub Desktop.
Create a full-height screenshot of a website using [puppeteer](https://github.com/GoogleChrome/puppeteer)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const width = 1200; | |
const url = process.argv.length > 2 ? process.argv[2] : 'https://ea.com'; | |
const screenshotPath = process.argv.length > 3 ? process.argv[3] : 'screenshot.png'; | |
console.log(`${url} --> ${screenshotPath}`); | |
(async () => { | |
const browser = await puppeteer.launch(); | |
try { | |
const page = await browser.newPage(); | |
// pre-set the width, otherwise it will be 800px by default. | |
await page.setViewport({ width, height: 600 }); | |
// go to the url & get the height of entire site | |
await page.goto(url); | |
const height = await page.evaluate(() => { | |
return document.body.clientHeight | |
}); | |
console.log(`Dimensions: ${width} x ${height}`); | |
// set the final dimensions | |
await page.setViewport({ width, height }); | |
// take the screenshot and save it to the given path | |
await page.screenshot({path: screenshotPath}); | |
console.log('screenshot saved to ', screenshotPath); | |
} catch (err) { | |
console.error(err); | |
} finally { | |
await browser.close(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment