This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
51 lines (48 loc) · 1.56 KB
/
index.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
const express = require("express")
const { chromium } = require("playwright-chromium")
const { firefox } = require("playwright-firefox")
const app = express()
app.use(express.static("./public"))
const port = process.env.PORT || 3000;
app.get("/browser/:name", async (req, res) => {
const browserName = req.params["name"] || "chromium"
if (!["chromium", "firefox"].includes(browserName)) {
return res.status(500).send(`invalid browser name (${browserName})!`)
}
const url = req.query.url || "https://microsoft.com"
const waitUntil = req.query.waitUntil || "load"
const width = req.query.width ? parseInt(req.query.width, 10) : 1920
const height = req.query.height ? parseInt(req.query.height, 10) : 1080
console.log(`Incoming request for browser '${browserName}' and URL '${url}'`)
try {
/** @type {import('playwright-chromium').Browser} */
const browser = await { chromium, firefox }[browserName].launch({
chromiumSandbox: false
})
const page = await browser.newPage({
viewport: {
width,
height
}
})
await page.goto(url, {
timeout: 10 * 1000,
waitUntil
})
if (req.query.timeout) {
await page.waitForTimeout(parseInt(req.query.timeout, 10))
}
const data = await page.screenshot({
type: "png"
})
await browser.close()
res.contentType("image/png")
res.set("Content-Disposition", "inline;");
res.send(data)
} catch (err) {
res.status(500).send(`Something went wrong: ${err}`)
}
});
app.listen(port, () => {
console.log(`Listening on port ${port}!`);
});