forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot.ts
More file actions
32 lines (26 loc) · 817 Bytes
/
screenshot.ts
File metadata and controls
32 lines (26 loc) · 817 Bytes
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
import { launch } from "astral";
import { Image } from "imagescript";
if (Deno.args.length !== 2) {
throw new Error("Usage: screenshot <url> <id>");
}
const [url, id] = Deno.args;
const parsedUrl = new URL(url);
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
throw new Error("Invalid URL");
}
const browser = await launch();
const page = await browser.newPage(url);
await page.waitForNetworkIdle();
const raw = await page.screenshot();
await browser.close();
// convert to jpeg
const image2x = await Image.decode(raw);
await Deno.writeFile(
`./www/static/showcase/${id}2x.jpg`,
await image2x.encodeJPEG(80),
);
const image1x = image2x.resize(image2x.width / 2, Image.RESIZE_AUTO);
await Deno.writeFile(
`./www/static/showcase/${id}1x.jpg`,
await image1x.encodeJPEG(80),
);