Skip to content

Commit cea7a2a

Browse files
authored
chore: update screenshot task script (#2881)
1 parent e0ca942 commit cea7a2a

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

www/utils/screenshot.ts

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,30 @@
1-
import puppeteer from "npm:puppeteer@22.4.1";
2-
import { Image } from "https://deno.land/x/imagescript@1.2.17/mod.ts";
3-
import { join } from "@std/path";
1+
import puppeteer from "npm:puppeteer@24.7.2";
2+
import { Image } from "https://deno.land/x/imagescript@1.3.0/mod.ts";
43

5-
const url = Deno.args[0];
6-
const id = Deno.args[1];
7-
8-
if (Deno.args.length == 0) {
9-
// deno-lint-ignore no-console
10-
console.log("Usage: screenshot <url> <id>");
11-
Deno.exit(0);
12-
}
13-
14-
if (!(url.match(/^http[s]?:\/\//)) || !url) {
15-
// deno-lint-ignore no-console
16-
console.log("Provided URL is Broken or Wrong");
17-
Deno.exit(0);
4+
if (Deno.args.length !== 2) {
5+
throw new Error("Usage: screenshot <url> <id>");
186
}
197

20-
if (!id) {
21-
// deno-lint-ignore no-console
22-
console.log("Provide id to Process");
23-
Deno.exit(0);
8+
const [url, id] = Deno.args;
9+
const parsedUrl = new URL(url);
10+
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
11+
throw new Error("Invalid URL");
2412
}
2513

26-
const outDir = "./www/static/showcase";
2714
const browser = await puppeteer.launch({
2815
defaultViewport: { width: 1200, height: 675 },
29-
headless: "new",
16+
headless: true,
3017
});
3118
const page = await browser.newPage();
3219
await page.goto(url, { waitUntil: "networkidle2" });
3320
const raw = await page.screenshot();
34-
3521
await browser.close();
3622

37-
if (!(raw instanceof Uint8Array)) {
38-
// deno-lint-ignore no-console
39-
console.log("Invalid Image");
40-
Deno.exit(0);
41-
}
4223
// convert to jpeg
4324
const image2x = await Image.decode(raw);
44-
const jpeg2x = join(outDir, `${id}2x.jpg`);
25+
const jpeg2x = import.meta.resolve(`../static/showcase/${id}2x.jpg`);
4526
await Deno.writeFile(jpeg2x, await image2x.encodeJPEG(80));
4627

47-
const jpeg1x = join(outDir, `${id}1x.jpg`);
28+
const jpeg1x = import.meta.resolve(`../static/showcase/${id}1x.jpg`);
4829
const image1x = image2x.resize(image2x.width / 2, Image.RESIZE_AUTO);
4930
await Deno.writeFile(jpeg1x, await image1x.encodeJPEG(80));

0 commit comments

Comments
 (0)