/
screenshot.js
71 lines (65 loc) · 1.85 KB
/
screenshot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const chromium = require('@sparticuz/chromium-min');
const puppeteer = require('puppeteer-core');
let _page;
async function getBrowser() {
// local development is broken for this 👇
// but it works in vercel so I'm not gonna touch it
return puppeteer.launch({
args: [...chromium.args, '--hide-scrollbars', '--disable-web-security'],
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath(
`https://github.com/Sparticuz/chromium/releases/download/v116.0.0/chromium-v116.0.0-pack.tar`
),
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
}
async function getPage() {
if (_page) return _page;
const browser = await getBrowser();
_page = await browser.newPage();
return _page;
}
function checkUrl(string) {
var url = '';
try {
url = new URL(string);
} catch (error) {
return false;
}
return true;
}
export async function getScreenshot(url, ratio = 1) {
const page = await getPage();
await page.goto(url, {
waitUntil: 'domcontentloaded',
});
await page.setViewport({
width: 1000,
height: 600,
devicePixelRatio: ratio,
});
const file = await page.screenshot();
return file;
}
module.exports = async (req, res) => {
if (!req.query.url) return res.status(400).send('No url query specified.');
if (!checkUrl(req.query.url))
return res.status(400).send('Invalid url query specified.');
try {
const file = await getScreenshot(req.query.url, req.query.ratio);
res.setHeader('Content-Type', 'image/png');
res.setHeader(
'Cache-Control',
'public, immutable, no-transform, s-maxage=604800, max-age=604800'
);
res.status(200).end(file);
} catch (error) {
console.error(error);
res
.status(500)
.send(
'The server encountered an error. You may have inputted an invalid query.'
);
}
};