-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-page.mjs
More file actions
32 lines (27 loc) · 877 Bytes
/
fetch-page.mjs
File metadata and controls
32 lines (27 loc) · 877 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
#!/usr/bin/env node
/**
* Fetch a web page using Plasmate and print the semantic content.
*/
import { execSync } from "node:child_process";
const url = process.argv[2] || "https://news.ycombinator.com";
try {
const output = execSync(`plasmate fetch "${url}"`, {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
});
const som = JSON.parse(output);
console.log(`Title: ${som.title ?? "N/A"}`);
console.log(`Language: ${som.lang ?? "N/A"}`);
console.log(`Regions: ${(som.regions ?? []).length}`);
console.log();
for (const region of som.regions ?? []) {
console.log(`[${region.role}] ${region.id}`);
for (const el of (region.elements ?? []).slice(0, 5)) {
const text = (el.text ?? "").slice(0, 80);
console.log(` ${el.role}: ${text}`);
}
}
} catch (err) {
console.error(`Error: ${err.message}`);
process.exit(1);
}