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
build-chrome.mjs · 2.9 KB · 53 lines
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * Build dist/chrome.html: src/chrome.html with the loading animations and the toolbar icons inlined. |
| 4 | * |
| 5 | * The chrome page's CSP forbids loading external files, and both must be real inline <svg> (so `currentColor` and the |
| 6 | * --throb-* / --icon-accent variables follow the UI theme). So at build time: |
| 7 | * - the SVGs in assets/throbbers/svg/ are embedded as a JS object at the `THROBBERS` marker; |
| 8 | * - the SVGs in assets/icons/svg/ are embedded as a JS object at the `ICONS` marker (for icons the page swaps, like |
| 9 | * the star), and each `<!--icon:NAME-->` in the markup is replaced by that icon. |
| 10 | */ |
| 11 | import { mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; |
| 12 | import { dirname, join } from "node:path"; |
| 13 | import { fileURLToPath } from "node:url"; |
| 14 | |
| 15 | const root = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 16 | |
| 17 | /** name → SVG markup for every plain `name.svg` in a folder (the fixed-colour -dark/-light variants are for <img> use). */ |
| 18 | function readSvgs(dir, tidy) { |
| 19 | const out = {}; |
| 20 | for (const f of readdirSync(dir).sort()) { |
| 21 | if (!/^[a-z0-9-]+\.svg$/.test(f) || /-(dark|light)\.svg$/.test(f)) continue; |
| 22 | out[f.replace(/\.svg$/, "")] = tidy(readFileSync(join(dir, f), "utf8")); |
| 23 | } |
| 24 | if (Object.keys(out).length === 0) throw new Error(`no SVGs found in ${dir}`); |
| 25 | return out; |
| 26 | } |
| 27 | // <title> would add a native tooltip; the aria-label stays for assistive tech. |
| 28 | const throbbers = readSvgs(join(root, "assets", "throbbers", "svg"), (s) => s.replace(/<title>.*?<\/title>/s, "").trim()); |
| 29 | // An icon sits next to its button's words (or the button has its own aria-label), so it is decoration: hidden from |
| 30 | // assistive tech, no tooltip of its own. |
| 31 | const icons = readSvgs(join(root, "assets", "icons", "svg"), (s) => |
| 32 | s.replace(/<title>.*?<\/title>/s, "").replace(/\s*role="img"\s*aria-label="[^"]*"/, ' class="ico" aria-hidden="true" focusable="false"').trim()); |
| 33 | |
| 34 | // Escape "</" so nothing inside the JSON can ever close the surrounding <script>. |
| 35 | const asJs = (o) => JSON.stringify(o).replace(/<\//g, "<\\/"); |
| 36 | const inline = (html, marker, o) => { |
| 37 | if (!html.includes(marker)) throw new Error(`marker ${marker} not found in src/chrome.html`); |
| 38 | return html.replace(marker, () => asJs(o)); |
| 39 | }; |
| 40 | |
| 41 | let html = readFileSync(join(root, "src", "chrome.html"), "utf8"); |
| 42 | html = inline(html, "/*__THROBBERS__*/ {}", throbbers); |
| 43 | html = inline(html, "/*__ICONS__*/ {}", icons); |
| 44 | let placed = 0; |
| 45 | html = html.replace(/<!--icon:([a-z0-9-]+)-->/g, (_, name) => { |
| 46 | if (!icons[name]) throw new Error(`<!--icon:${name}--> in src/chrome.html, but there is no assets/icons/svg/${name}.svg`); |
| 47 | placed++; |
| 48 | return icons[name]; |
| 49 | }); |
| 50 | |
| 51 | mkdirSync(join(root, "dist"), { recursive: true }); |
| 52 | writeFileSync(join(root, "dist", "chrome.html"), html); |
| 53 | console.log(`chrome.html: inlined ${Object.keys(throbbers).length} throbbers and ${Object.keys(icons).length} icons (${placed} placed in the toolbar)`); |