Spaces:
Running
Running
Download tools/shot.mjs from MacroSwan/CablePilot: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/spaces/MacroSwan/CablePilot/resolve/main/tools/shot.mjs
- Command line
-
hf download hf://spaces/MacroSwan/CablePilot/tools/shot.mjs
-
curl -L -o shot.mjs https://huggingface.co/spaces/MacroSwan/CablePilot/resolve/main/tools/shot.mjs
3.25 kB
| // node tools/shot.mjs <url> <out.png> [wait_s] [WxH] : real-time headless check over the DevTools protocol (no | |
| // Playwright): loads the page, waits, prints console messages and exceptions, saves a screenshot. | |
| import { spawn } from "node:child_process"; | |
| import { mkdtempSync, writeFileSync } from "node:fs"; | |
| import { tmpdir } from "node:os"; | |
| import { join } from "node:path"; | |
| const [url, out, waitS = "20", size = "1400x860"] = process.argv.slice(2); | |
| const [W, H] = size.split("x").map(Number); | |
| const port = 9300 + Math.floor(Math.random() * 500); | |
| const prof = mkdtempSync(join(process.env.SHOT_DIR || tmpdir(), "shot-")); | |
| const chr = spawn("chromium", ["--headless=new", "--no-sandbox", `--user-data-dir=${prof}`, "--enable-unsafe-swiftshader", | |
| "--use-angle=swiftshader", `--remote-debugging-port=${port}`, `--window-size=${W},${H}`, "about:blank"], { stdio: ["ignore", "ignore", "pipe"] }); | |
| let errlog = ""; chr.stderr.on("data", d => (errlog += d)); | |
| const sleep = ms => new Promise(r => setTimeout(r, ms)); | |
| let tab; | |
| for (let i = 0; i < 150 && !tab; i++) { | |
| await sleep(200); | |
| try { tab = (await (await fetch(`http://127.0.0.1:${port}/json`)).json()).find(t => t.type === "page"); } catch {} | |
| if (!tab && i > 20 && i % 10 === 0) try { tab = await (await fetch(`http://127.0.0.1:${port}/json/new?about:blank`, { method: "PUT" })).json(); } catch {} | |
| } | |
| if (!tab) { console.error("no DevTools page", errlog.slice(0, 800)); chr.kill(); process.exit(1); } | |
| const ws = new WebSocket(tab.webSocketDebuggerUrl); | |
| await new Promise(r => (ws.onopen = r)); | |
| let id = 0; const pending = new Map(); | |
| const send = (method, params = {}) => new Promise(r => { const i = ++id; pending.set(i, r); ws.send(JSON.stringify({ id: i, method, params })); }); | |
| ws.onmessage = ({ data }) => { | |
| const m = JSON.parse(data); | |
| if (m.id && pending.has(m.id)) { pending.get(m.id)(m.result); pending.delete(m.id); } | |
| if (m.method === "Runtime.consoleAPICalled") console.log("CONSOLE", m.params.type, m.params.args.map(a => a.value ?? a.description).join(" ")); | |
| if (m.method === "Runtime.exceptionThrown") console.log("EXCEPTION", m.params.exceptionDetails.exception?.description || m.params.exceptionDetails.text); | |
| if (m.method === "Log.entryAdded" && m.params.entry.level === "error") console.log("LOG", m.params.entry.text, m.params.entry.url || ""); | |
| }; | |
| await send("Runtime.enable"); await send("Log.enable"); await send("Page.enable"); | |
| await send("Emulation.setDeviceMetricsOverride", { width: W, height: H, deviceScaleFactor: 1, mobile: W < 600 }); | |
| await send("Page.navigate", { url }); | |
| await sleep(+waitS * 1000); | |
| const probe = await send("Runtime.evaluate", { expression: `JSON.stringify({phase: document.getElementById('phase')?.textContent, toys: document.getElementById('toys')?.textContent, clock: document.getElementById('clock')?.textContent, ms: document.getElementById('ms')?.textContent, contacts: document.getElementById('contacts')?.textContent, overflowX: document.documentElement.scrollWidth > innerWidth})`, returnByValue: true }); | |
| console.log("STATE", probe.result.value); | |
| const shot = await send("Page.captureScreenshot", { format: "png" }); | |
| writeFileSync(out, Buffer.from(shot.data, "base64")); | |
| ws.close(); chr.kill(); | |
| console.log("saved", out); | |