|
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"; |
4 | 3 |
|
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>"); |
18 | 6 | } |
19 | 7 |
|
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"); |
24 | 12 | } |
25 | 13 |
|
26 | | -const outDir = "./www/static/showcase"; |
27 | 14 | const browser = await puppeteer.launch({ |
28 | 15 | defaultViewport: { width: 1200, height: 675 }, |
29 | | - headless: "new", |
| 16 | + headless: true, |
30 | 17 | }); |
31 | 18 | const page = await browser.newPage(); |
32 | 19 | await page.goto(url, { waitUntil: "networkidle2" }); |
33 | 20 | const raw = await page.screenshot(); |
34 | | - |
35 | 21 | await browser.close(); |
36 | 22 |
|
37 | | -if (!(raw instanceof Uint8Array)) { |
38 | | - // deno-lint-ignore no-console |
39 | | - console.log("Invalid Image"); |
40 | | - Deno.exit(0); |
41 | | -} |
42 | 23 | // convert to jpeg |
43 | 24 | 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`); |
45 | 26 | await Deno.writeFile(jpeg2x, await image2x.encodeJPEG(80)); |
46 | 27 |
|
47 | | -const jpeg1x = join(outDir, `${id}1x.jpg`); |
| 28 | +const jpeg1x = import.meta.resolve(`../static/showcase/${id}1x.jpg`); |
48 | 29 | const image1x = image2x.resize(image2x.width / 2, Image.RESIZE_AUTO); |
49 | 30 | await Deno.writeFile(jpeg1x, await image1x.encodeJPEG(80)); |
0 commit comments