Created
March 21, 2018 17:20
-
-
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/
This file contains 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 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) |
This file contains 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
{ | |
"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