Skip to content

Instantly share code, notes, and snippets.

@shrunyan
Created March 21, 2018 17:20
Show Gist options
  • Save shrunyan/c7ade04d59932d7e04a5701998097199 to your computer and use it in GitHub Desktop.
Save shrunyan/c7ade04d59932d7e04a5701998097199 to your computer and use it in GitHub Desktop.
Take screenshots of urls. Inspired by https://meowni.ca/posts/2017-puppeteer-tests/
const puppeteer = require('puppeteer')
const express = require('express')
const querystring = require('querystring')
const server = express()
/**
* Screenshot a url
* e.g. /screenshot?url=https://www.npmjs.com&width=800&height=600
*/
server.get('/screenshot', async (req, res) => {
try {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const path = `${querystring.escape(req.query.url)}_${req.query.height}_${req.query.width}.png`
await page.goto(req.query.url)
await page.setViewport({
height: Number(req.query.height),
width: Number(req.query.width)
})
await page.screenshot({
path: path
})
await browser.close()
res.send({
message: 'sweet shot'
})
} catch (err) {
console.error(err)
res.status(500).end()
}
})
server.listen(9000)
{
"name": "screenshots",
"version": "1.0.0",
"description":
"Take screenshots of urls. Inspired by https://meowni.ca/posts/2017-puppeteer-tests/",
"main": "index.js",
"license": "ISC",
"dependencies": {
"express": "4.16.3",
"puppeteer": "1.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment