Skip to content
This repository has been archived by the owner. It is now read-only.

Add puppeteer-screenshot #207

Merged
merged 4 commits into from Dec 29, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to鈥
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,18 @@
const chrome = require('chrome-aws-lambda');
const puppeteer = require('puppeteer-core');

async function getScreenshot(url, type, quality, fullPage) {
const browser = await puppeteer.launch({
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless,
});

const page = await browser.newPage();
await page.goto(url);
const file = await page.screenshot({ type, quality, fullPage });
await browser.close();
return file;
}

module.exports = { getScreenshot };
@@ -0,0 +1,12 @@
{
"version": 2,
"builds": [
{ "src": "public/*", "use": "@now/static" },
{ "src": "screenshot.js", "use": "@now/node", "config": { "maxLambdaSize": "40mb" } }
],
"routes": [
{ "src": "/", "dest": "/public/index.html" },
{ "src": "/favicon.ico", "dest": "/public/favicon.ico" },
{ "src": "/(.+)", "dest": "/screenshot.js" }
]
}
@@ -0,0 +1,11 @@
{
"name": "puppeteer-screenshot",
"version": "1.0.0",
"description": "Take screenshot of a website",
"private": true,
"license": "MIT",
"dependencies": {
"chrome-aws-lambda": "1.11.1",
"puppeteer-core": "1.11.0"
}
}
Binary file not shown.
@@ -0,0 +1,18 @@
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Screenshot as a service</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Screenshot as a service</h1>
<p>Please provide a path to a website or choose one of the following:</p>
<ul>
<li><a href="/google.com">/google.com</a></li>
<li><a href="/zeit.co/blog?type=png">/zeit.co/blog?type=png</a></li>
<li><a href="/zeit.co/about?fullPage=true">/zeit.co/about?fullPage=true</a></li>
<li><a href="/example.com?type=jpeg&quality=75&fullPage=true">/example.com?type=jpeg&quality=75&fullPage=true</a></li>
</ul>
</body>
</html>
@@ -0,0 +1,27 @@
const { parse } = require('url');
const { getScreenshot } = require('./chromium');
const { getInt, getUrlFromPath, isValidUrl } = require('./validator');

module.exports = async function (req, res) {
try {
const { pathname = '/', query = {} } = parse(req.url, true);
const { type = 'png', quality, fullPage } = query;
const url = getUrlFromPath(pathname);
const qual = getInt(quality);
if (!isValidUrl(url)) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/html');
res.end(`<h1>Bad Request</h1><p>The url <em>${url}</em> is not valid.</p>`);
} else {
const file = await getScreenshot(url, type, qual, fullPage);
res.statusCode = 200;
res.setHeader('Content-Type', `image/${type}`);
res.end(file);
}
} catch (e) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/html');
res.end('<h1>Server Error</h1><p>Sorry, there was a problem</p>');
console.error(e.message);
}
};
@@ -0,0 +1,25 @@
const { URL } = require('url');

function getInt(str) {
return /[0-9]+/.test(str) ? parseInt(str) : undefined;
}

function getUrlFromPath(str) {
let url = str.slice(1);
if (!url.startsWith('http')) {
return 'https://' + url;
}
return url;
}

function isValidUrl(str) {
try {
const url = new URL(str);
return url.hostname.includes('.');
} catch(e) {
console.error(e.message);
return false;
}
}

module.exports = { getInt, getUrlFromPath, isValidUrl };
ProTip! Use n and p to navigate between commits in a pull request.
You can鈥檛 perform that action at this time.