Files
sweet/www/utils/screenshot.ts
T
zhizhengand李嘉图·M·路 cb402b9839 fix(utils): restore missing dependencies for screenshot utility (#3016)
The relevant dependencies were lost in previous cleanups. This situation
caused the screenshot function to fail.

This PR properly cleans up the functional function and restores its
operation.

---------

Co-authored-by: 李嘉图·M·路 <146103794+Ricardo-M-Zheng@users.noreply.github.com>
2025-06-11 17:47:57 +02:00

33 lines
825 B
TypeScript

import { launch } from "@astral/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),
);