chore: update screenshot task script (#2881)
This commit is contained in:
+11
-30
@@ -1,49 +1,30 @@
|
|||||||
import puppeteer from "npm:puppeteer@22.4.1";
|
import puppeteer from "npm:puppeteer@24.7.2";
|
||||||
import { Image } from "https://deno.land/x/imagescript@1.2.17/mod.ts";
|
import { Image } from "https://deno.land/x/imagescript@1.3.0/mod.ts";
|
||||||
import { join } from "@std/path";
|
|
||||||
|
|
||||||
const url = Deno.args[0];
|
if (Deno.args.length !== 2) {
|
||||||
const id = Deno.args[1];
|
throw new Error("Usage: screenshot <url> <id>");
|
||||||
|
|
||||||
if (Deno.args.length == 0) {
|
|
||||||
// deno-lint-ignore no-console
|
|
||||||
console.log("Usage: screenshot <url> <id>");
|
|
||||||
Deno.exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(url.match(/^http[s]?:\/\//)) || !url) {
|
const [url, id] = Deno.args;
|
||||||
// deno-lint-ignore no-console
|
const parsedUrl = new URL(url);
|
||||||
console.log("Provided URL is Broken or Wrong");
|
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
|
||||||
Deno.exit(0);
|
throw new Error("Invalid URL");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!id) {
|
|
||||||
// deno-lint-ignore no-console
|
|
||||||
console.log("Provide id to Process");
|
|
||||||
Deno.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
const outDir = "./www/static/showcase";
|
|
||||||
const browser = await puppeteer.launch({
|
const browser = await puppeteer.launch({
|
||||||
defaultViewport: { width: 1200, height: 675 },
|
defaultViewport: { width: 1200, height: 675 },
|
||||||
headless: "new",
|
headless: true,
|
||||||
});
|
});
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await page.goto(url, { waitUntil: "networkidle2" });
|
await page.goto(url, { waitUntil: "networkidle2" });
|
||||||
const raw = await page.screenshot();
|
const raw = await page.screenshot();
|
||||||
|
|
||||||
await browser.close();
|
await browser.close();
|
||||||
|
|
||||||
if (!(raw instanceof Uint8Array)) {
|
|
||||||
// deno-lint-ignore no-console
|
|
||||||
console.log("Invalid Image");
|
|
||||||
Deno.exit(0);
|
|
||||||
}
|
|
||||||
// convert to jpeg
|
// convert to jpeg
|
||||||
const image2x = await Image.decode(raw);
|
const image2x = await Image.decode(raw);
|
||||||
const jpeg2x = join(outDir, `${id}2x.jpg`);
|
const jpeg2x = import.meta.resolve(`../static/showcase/${id}2x.jpg`);
|
||||||
await Deno.writeFile(jpeg2x, await image2x.encodeJPEG(80));
|
await Deno.writeFile(jpeg2x, await image2x.encodeJPEG(80));
|
||||||
|
|
||||||
const jpeg1x = join(outDir, `${id}1x.jpg`);
|
const jpeg1x = import.meta.resolve(`../static/showcase/${id}1x.jpg`);
|
||||||
const image1x = image2x.resize(image2x.width / 2, Image.RESIZE_AUTO);
|
const image1x = image2x.resize(image2x.width / 2, Image.RESIZE_AUTO);
|
||||||
await Deno.writeFile(jpeg1x, await image1x.encodeJPEG(80));
|
await Deno.writeFile(jpeg1x, await image1x.encodeJPEG(80));
|
||||||
|
|||||||
Reference in New Issue
Block a user