Skip to content

Commit 9ec38f5

Browse files
styflesimonw
authored andcommitted
Add puppeteer-screenshot (#207)
* Add puppeteer-screenshot * Change link to example.com Co-Authored-By: styfle <steven@ceriously.com> * Use npm install --save-exact
0 parents  commit 9ec38f5

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed

puppeteer-screenshot/chromium.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const chrome = require('chrome-aws-lambda');
2+
const puppeteer = require('puppeteer-core');
3+
4+
async function getScreenshot(url, type, quality, fullPage) {
5+
const browser = await puppeteer.launch({
6+
args: chrome.args,
7+
executablePath: await chrome.executablePath,
8+
headless: chrome.headless,
9+
});
10+
11+
const page = await browser.newPage();
12+
await page.goto(url);
13+
const file = await page.screenshot({ type, quality, fullPage });
14+
await browser.close();
15+
return file;
16+
}
17+
18+
module.exports = { getScreenshot };

puppeteer-screenshot/now.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 2,
3+
"builds": [
4+
{ "src": "public/*", "use": "@now/static" },
5+
{ "src": "screenshot.js", "use": "@now/node", "config": { "maxLambdaSize": "40mb" } }
6+
],
7+
"routes": [
8+
{ "src": "/", "dest": "/public/index.html" },
9+
{ "src": "/favicon.ico", "dest": "/public/favicon.ico" },
10+
{ "src": "/(.+)", "dest": "/screenshot.js" }
11+
]
12+
}

puppeteer-screenshot/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "puppeteer-screenshot",
3+
"version": "1.0.0",
4+
"description": "Take screenshot of a website",
5+
"private": true,
6+
"license": "MIT",
7+
"dependencies": {
8+
"chrome-aws-lambda": "1.11.1",
9+
"puppeteer-core": "1.11.0"
10+
}
11+
}
4.19 KB
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<meta http-equiv="x-ua-compatible" content="ie=edge">
5+
<title>Screenshot as a service</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
</head>
8+
<body>
9+
<h1>Screenshot as a service</h1>
10+
<p>Please provide a path to a website or choose one of the following:</p>
11+
<ul>
12+
<li><a href="/google.com">/google.com</a></li>
13+
<li><a href="/zeit.co/blog?type=png">/zeit.co/blog?type=png</a></li>
14+
<li><a href="/zeit.co/about?fullPage=true">/zeit.co/about?fullPage=true</a></li>
15+
<li><a href="/example.com?type=jpeg&quality=75&fullPage=true">/example.com?type=jpeg&quality=75&fullPage=true</a></li>
16+
</ul>
17+
</body>
18+
</html>

puppeteer-screenshot/screenshot.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { parse } = require('url');
2+
const { getScreenshot } = require('./chromium');
3+
const { getInt, getUrlFromPath, isValidUrl } = require('./validator');
4+
5+
module.exports = async function (req, res) {
6+
try {
7+
const { pathname = '/', query = {} } = parse(req.url, true);
8+
const { type = 'png', quality, fullPage } = query;
9+
const url = getUrlFromPath(pathname);
10+
const qual = getInt(quality);
11+
if (!isValidUrl(url)) {
12+
res.statusCode = 400;
13+
res.setHeader('Content-Type', 'text/html');
14+
res.end(`<h1>Bad Request</h1><p>The url <em>${url}</em> is not valid.</p>`);
15+
} else {
16+
const file = await getScreenshot(url, type, qual, fullPage);
17+
res.statusCode = 200;
18+
res.setHeader('Content-Type', `image/${type}`);
19+
res.end(file);
20+
}
21+
} catch (e) {
22+
res.statusCode = 500;
23+
res.setHeader('Content-Type', 'text/html');
24+
res.end('<h1>Server Error</h1><p>Sorry, there was a problem</p>');
25+
console.error(e.message);
26+
}
27+
};

puppeteer-screenshot/validator.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { URL } = require('url');
2+
3+
function getInt(str) {
4+
return /[0-9]+/.test(str) ? parseInt(str) : undefined;
5+
}
6+
7+
function getUrlFromPath(str) {
8+
let url = str.slice(1);
9+
if (!url.startsWith('http')) {
10+
return 'https://' + url;
11+
}
12+
return url;
13+
}
14+
15+
function isValidUrl(str) {
16+
try {
17+
const url = new URL(str);
18+
return url.hostname.includes('.');
19+
} catch(e) {
20+
console.error(e.message);
21+
return false;
22+
}
23+
}
24+
25+
module.exports = { getInt, getUrlFromPath, isValidUrl };

0 commit comments

Comments
 (0)