askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
print-zoom.mjs · 7.4 KB · 97 lines
| 1 | // Print, Save as PDF and zoom: what the File and View menus, ⌘P / ⌘⇧S / ⌘+ ⌘− ⌘0 and the page's right-click menu run. |
| 2 | // The print dialog is recorded, not shown (a scripted run must not put a dialog on anyone's screen); the PDF goes to the |
| 3 | // suite's download folder without a dialog. Menu commands are sent as the menu sends them; keys are sent to the page. |
| 4 | import { readFileSync, readdirSync, existsSync } from "node:fs"; |
| 5 | import { join } from "node:path"; |
| 6 | |
| 7 | export default async function (ctx) { |
| 8 | const { call: T, check, base, sleep, tmp } = ctx; |
| 9 | const c = await ctx.chrome(); |
| 10 | async function waitFor(fn, what, tries = 60) { |
| 11 | for (let i = 0; i < tries; i++) { const v = await fn().catch(() => null); if (v) return v; await sleep(100); } |
| 12 | throw new Error("timed out waiting for " + what); |
| 13 | } |
| 14 | const active = async () => (await T("tabs.list")).find((t) => t.active); |
| 15 | const menu = (cmd) => c.ev(`(onTabCmd(${JSON.stringify(cmd)}), true)`); |
| 16 | const badge = () => c.ev(`(() => { const b = document.getElementById("zoomBtn"); return b.hidden ? "" : b.textContent; })()`); |
| 17 | const dpr = async () => (await T("page.eval", { js: "window.devicePixelRatio" })).value; |
| 18 | const mod = process.platform === "darwin" ? "meta" : "control"; |
| 19 | const key = (keyCode, mods = [mod]) => c.ev(`(() => { active.wv.focus(); active.wv.sendInputEvent({ type: "keyDown", keyCode: ${JSON.stringify(keyCode)}, modifiers: ${JSON.stringify(mods)} }); active.wv.sendInputEvent({ type: "keyUp", keyCode: ${JSON.stringify(keyCode)}, modifiers: ${JSON.stringify(mods)} }); return true; })()`); |
| 20 | |
| 21 | const page = await T("tabs.new", { url: base + "/free" }); |
| 22 | await T("tabs.activate", { tab: page.id }); |
| 23 | await waitFor(async () => (await active()).id === page.id && !(await active()).loading, "the page"); |
| 24 | await T("page.eval", { js: `document.title = "A page: to print / save", true` }); |
| 25 | const base1 = await dpr(); |
| 26 | |
| 27 | // ---- zoom ---- |
| 28 | check((await badge()) === "", "at 100% no zoom size shows next to the address"); |
| 29 | await menu("zoom-in"); |
| 30 | await waitFor(async () => (await badge()) === "110%", "110%"); |
| 31 | check(Math.abs((await dpr()) / base1 - 1.1) < 0.02, "View ▸ Zoom In makes the page 110%, and the size shows next to the address", `${await badge()}, ratio ${(await dpr()) / base1}`); |
| 32 | await menu("zoom-in"); |
| 33 | check(await waitFor(async () => (await badge()) === "125%", "125%").then(() => true, () => false), "again: 125%, Chrome's next step"); |
| 34 | await menu("zoom-out"); |
| 35 | check(await waitFor(async () => (await badge()) === "110%", "110%").then(() => true, () => false), "View ▸ Zoom Out steps back to 110%"); |
| 36 | |
| 37 | // the site remembers it: another page of the same site opens at that size |
| 38 | const other = await T("tabs.new", { url: base + "/headers?zoom=1" }); |
| 39 | await T("tabs.activate", { tab: other.id }); |
| 40 | await waitFor(async () => !(await active()).loading, "the second page"); |
| 41 | check(await waitFor(async () => (await badge()) === "110%", "the site's size").then(() => true, () => false), "another page of the same site opens at the site's size", await badge()); |
| 42 | check(Math.abs((await dpr()) / base1 - 1.1) < 0.02, "and is drawn at that size"); |
| 43 | const saved = join(tmp, "profile-print-zoom", "zoom.json"); |
| 44 | const file = existsSync(saved) ? JSON.parse(readFileSync(saved, "utf8")) : null; |
| 45 | check(file && file.sites && file.sites["127.0.0.1"] === 1.1, "the size is saved for the next start", JSON.stringify(file)); |
| 46 | |
| 47 | // keys sent to the page reach the menu when the page doesn't take them |
| 48 | await key("="); |
| 49 | const viaKey = await waitFor(async () => ((await badge()) === "125%" ? true : null), "⌘= from the page").catch(() => false); |
| 50 | check(viaKey === true, "⌘= while the page has the keyboard zooms in", await badge()); |
| 51 | await key("0"); |
| 52 | const reset = await waitFor(async () => ((await badge()) === "" ? true : null), "⌘0 from the page").catch(() => false); |
| 53 | check(reset === true && Math.abs((await dpr()) / base1 - 1) < 0.02, "⌘0 goes back to actual size, and the size next to the address goes away"); |
| 54 | await T("tabs.activate", { tab: page.id }); |
| 55 | check(await waitFor(async () => ((await badge()) === "" ? true : null), "the first tab back at 100%").then(() => true, () => false), "and the site's other open tab follows"); |
| 56 | const after = existsSync(saved) ? JSON.parse(readFileSync(saved, "utf8")) : null; |
| 57 | check(after && !("127.0.0.1" in after.sites), "a site back at 100% is forgotten", JSON.stringify(after)); |
| 58 | await T("tabs.close", { tab: other.id }).catch(() => {}); |
| 59 | |
| 60 | // ---- print ---- |
| 61 | let since = (await T("x402.events", { since: 0 })).next; |
| 62 | const nextEvent = async (kind) => waitFor(async () => { const r = await T("x402.events", { since }); const e = r.events.find((x) => x.kind === kind); if (e) since = r.next; return e; }, kind); |
| 63 | await menu("print"); |
| 64 | const pr = await nextEvent("print-dialog").catch(() => null); |
| 65 | check(!!pr && /\/free/.test(pr.url), "File ▸ Print… opens the print dialog for the page (recorded here, not shown)", JSON.stringify(pr)); |
| 66 | await sleep(400); // a menu click and a key press this close together count as one press |
| 67 | await key("p"); |
| 68 | const prKey = await nextEvent("print-dialog").catch(() => null); |
| 69 | check(!!prKey, "⌘P while the page has the keyboard does the same"); |
| 70 | // a page with its own print (a document editor) keeps ⌘P |
| 71 | await T("page.eval", { js: "window.__ownPrint = 0, addEventListener('keydown', (e) => { if (e.code === 'KeyP' && (e.metaKey || e.ctrlKey)) { e.preventDefault(); __ownPrint++; } }), true" }); |
| 72 | await sleep(400); |
| 73 | await key("p"); |
| 74 | await sleep(800); |
| 75 | const stolen = (await T("x402.events", { since })).events.some((e) => e.kind === "print-dialog"); |
| 76 | check(!stolen && (await T("page.eval", { js: "window.__ownPrint" })).value === 1, "a page with its own print (a document editor) gets ⌘P instead, as in Chrome"); |
| 77 | |
| 78 | // ---- save as PDF ---- |
| 79 | const dir = join(tmp, "downloads-print-zoom"); |
| 80 | const pdfs = () => (existsSync(dir) ? readdirSync(dir).filter((f) => f.endsWith(".pdf")) : []); |
| 81 | await menu("save-pdf"); |
| 82 | const got = await waitFor(async () => pdfs()[0], "the PDF", 100).catch(() => null); |
| 83 | check(got === "A page_ to print - save.pdf", "File ▸ Save as PDF… saves the page, named after its title (no slashes or colons)", String(got)); |
| 84 | const bytes = got ? readFileSync(join(dir, got)) : Buffer.alloc(0); |
| 85 | check(bytes.subarray(0, 5).toString() === "%PDF-" && bytes.length > 1000, "and it is a real PDF", `${bytes.length} bytes`); |
| 86 | await menu("save-pdf"); |
| 87 | check(await waitFor(async () => (pdfs().length === 2 ? true : null), "a second PDF").then(() => true, () => false), "saving again never overwrites the first", pdfs().join(" | ")); |
| 88 | |
| 89 | // ---- the right-click menu runs the same ---- |
| 90 | await c.ev(`(() => { active.wv.sendInputEvent({ type: "mouseMove", x: 300, y: 300 }); active.wv.sendInputEvent({ type: "mouseDown", x: 300, y: 300, button: "right", clickCount: 1 }); active.wv.sendInputEvent({ type: "mouseUp", x: 300, y: 300, button: "right", clickCount: 1 }); return true; })()`); |
| 91 | const cm = await nextEvent("context-menu").catch(() => null); |
| 92 | check(!!cm && cm.items.includes("print") && cm.items.includes("save-pdf"), "right-click on the page offers Print… and Save as PDF…", cm && cm.items.join(",")); |
| 93 | await T("menu.pick", { id: "save-pdf" }); |
| 94 | check(await waitFor(async () => (pdfs().length === 3 ? true : null), "a third PDF").then(() => true, () => false), "and Save as PDF… there saves it too"); |
| 95 | |
| 96 | await T("tabs.close", { tab: page.id }).catch(() => {}); |
| 97 | } |