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
plugins.mjs · 11.1 KB · 124 lines
| 1 | // Plugins: a style rule installed from gitstable (a stand-in on the demo server, DBB_GITSTABLE), applied to its site only, |
| 2 | // live to open pages; all off, one off, update and remove; and refusals for CSS that could send, a lying server and more |
| 3 | // powers. Driven both through the automation API and through the Plugins panel. |
| 4 | export default async function (ctx) { |
| 5 | const { call: T, check, base, sleep } = ctx; |
| 6 | const port = new URL(base).port; |
| 7 | const cdp = await ctx.chrome(); |
| 8 | const q = (js) => cdp.ev(`(() => { ${js} })()`); |
| 9 | async function waitFor(fn, what, tries = 60) { |
| 10 | for (let i = 0; i < tries; i++) { const v = await fn(); if (v) return v; await sleep(100); } |
| 11 | throw new Error(`timed out waiting for ${what}`); |
| 12 | } |
| 13 | const width = async () => (await T("page.eval", { js: `getComputedStyle(document.getElementById('col')).maxWidth` })).value; |
| 14 | const widthIs = (w, what) => waitFor(async () => (await width()) === w, what).then(() => true, () => false); |
| 15 | const fails = (p) => p.then(() => null, (e) => e); |
| 16 | |
| 17 | let st = await T("plugins.state"); |
| 18 | check(st.plugins.length === 0 && st.origin === base && st.apiLevel === 2, "no plugins at first; installs come from the configured gitstable", JSON.stringify({ n: st.plugins.length, origin: st.origin })); |
| 19 | await T("page.goto", { url: base + "/plugin-page" }); |
| 20 | check((await width()) === "300px", "the demo page's column is 300px wide on its own"); |
| 21 | |
| 22 | // ---- look up, then install exactly what was shown ---- |
| 23 | const pv = await T("plugins.preview", { source: "demo/wide" }); |
| 24 | check(pv.tag === "2026.11v1" && /^[0-9a-f]{40}$/.test(pv.commit) && pv.testedHere, "looking up owner/repository finds its newest release and the commit it points to", JSON.stringify({ tag: pv.tag, commit: pv.commit })); |
| 25 | check(/can't run code, read the page or send anything/.test(pv.says) && pv.sites.join() === "127.0.0.1", "and says which sites it changes and that it can't run code, read or send", pv.says); |
| 26 | check((await width()) === "300px", "looking up installs nothing"); |
| 27 | await T("plugins.install", { token: pv.token }); |
| 28 | check(await widthIs("none", "the plugin on the open page"), "installing applies it to the open page at once", await width()); |
| 29 | const pill = () => q(`const b = document.getElementById('plugBtn'); return b.style.display === 'none' ? '' : b.textContent + (b.classList.contains('applied') ? '|on' : '|off') + (getComputedStyle(b).backgroundColor === getComputedStyle(document.getElementById('histBtn')).backgroundColor ? '' : '|looks-pressed')`); |
| 30 | check((await waitFor(pill, "the status bar's plugin item")) === "Plugin: Wide page|on", "the status bar names the plugin changing this page", await pill()); |
| 31 | await T("page.goto", { url: `http://localhost:${port}/plugin-page` }); |
| 32 | check((await width()) === "300px", "another site (localhost) is left alone"); |
| 33 | check((await pill()) === "Plugins|off", "and the status bar just says Plugins there", await pill()); |
| 34 | await T("page.goto", { url: base + "/plugin-page" }); |
| 35 | check(await widthIs("none", "the plugin after loading the page again"), "a page loaded later gets it too"); |
| 36 | const again = await fails(T("plugins.install", { token: pv.token })); |
| 37 | check(again?.code === "expired", "a look-up installs once", again?.code); |
| 38 | |
| 39 | // ---- switches ---- |
| 40 | await T("plugins.allOff", { off: true }); |
| 41 | check(await widthIs("300px", "all off"), "Turn all plugins off shows the site as it is, at once"); |
| 42 | check((await waitFor(async () => /off/.test(await pill()) && pill(), "the status bar to say off")) === "Plugin off|off", "and the status bar says the site's plugin is off", await pill()); |
| 43 | await T("plugins.allOff", { off: false }); |
| 44 | check(await widthIs("none", "back on"), "and on again"); |
| 45 | await T("plugins.enable", { id: "wide", enabled: false }); |
| 46 | check(await widthIs("300px", "one off"), "one plugin can be switched off"); |
| 47 | await T("plugins.enable", { id: "wide", enabled: true }); |
| 48 | check(await widthIs("none", "one on"), "and on"); |
| 49 | |
| 50 | // ---- refused ---- |
| 51 | const sneaky = await fails(T("plugins.preview", { source: "demo/sneaky" })); |
| 52 | check(sneaky?.code === "not-style-only" && /url/.test(sneaky.message), "CSS that could send something (url()) is refused", sneaky?.message); |
| 53 | const esc = await fails(T("plugins.preview", { source: "demo/escaped" })); |
| 54 | check(esc?.code === "not-style-only", "and so is CSS that hides one behind an escape", esc?.message); |
| 55 | const lie = await fails(T("plugins.preview", { source: "demo/tampered" })); |
| 56 | check(lie?.code === "mismatch", "a file that doesn't match its git blob id is refused", lie?.message); |
| 57 | const more = await fails(T("plugins.preview", { source: "demo/scripty" })); |
| 58 | check(more?.code === "bad-plugin" && /page-script/.test(more.message), "a plugin asking for powers this browser doesn't give is refused", more?.message); |
| 59 | const elsewhere = await fails(T("plugins.preview", { source: "https://evil.test/demo/wide" })); |
| 60 | check(elsewhere?.code === "bad-source", "an address on another site is refused: plugins come from gitstable only", elsewhere?.message); |
| 61 | check((await T("plugins.state")).plugins.length === 1, "and nothing refused was installed"); |
| 62 | |
| 63 | // ---- the Plugins panel ---- |
| 64 | await q(`document.getElementById('plugBtn').click()`); |
| 65 | await waitFor(() => q(`return document.getElementById('plugPanel').classList.contains('open') && !!document.querySelector('#plugPanel .pl[data-id="wide"]')`), "the panel with the plugin"); |
| 66 | const row = await q(`return document.querySelector('#plugPanel .pl[data-id="wide"]').textContent`); |
| 67 | check(/Wide page 2026\.11v1/.test(row) && /release 2026\.11v1 · commit [0-9a-f]{7}/.test(row), "clicking it opens Plugins, which lists it with its release and commit", row); |
| 68 | const type = (text) => q(`const i = document.getElementById('plugSource'); i.value = ${JSON.stringify(text)}; i.dispatchEvent(new Event('input')); document.getElementById('plugLook').click(); return true;`); |
| 69 | await type("demo/wide"); |
| 70 | const same = await waitFor(() => q(`const a = document.getElementById('plugAsk'); return a ? a.textContent : ''`), "the look-up of the installed release"); |
| 71 | const sameBtn = await q(`const b = document.getElementById('plugInstall'); return b ? getComputedStyle(b).display : 'none'`); |
| 72 | check(/installed already/.test(same) && sameBtn === "none", "looking up the release that is installed says so, and offers nothing to install", same); |
| 73 | await type("demo/sneaky"); |
| 74 | const err = await waitFor(() => q(`const e = document.querySelector('#plugPanel .err'); return e ? e.textContent : ''`), "the refusal in the panel"); |
| 75 | check(/refused/.test(err), "the panel says why a plugin is refused", err); |
| 76 | await type("demo/wide@2026.10v1"); |
| 77 | const ask = await waitFor(() => q(`const a = document.getElementById('plugAsk'); return a ? a.textContent : ''`), "the look-up in the panel"); |
| 78 | check(/Older than the installed 2026\.11v1/.test(ask) && /can't run code/.test(ask) && /Go back to 2026\.10v1/.test(ask), "looking up an older release says it is older and offers to go back to it", ask); |
| 79 | await q(`document.getElementById('plugInstall').click()`); |
| 80 | check(await widthIs("700px", "the older release"), "going back installs that release (its CSS is in force)", await width()); |
| 81 | check((await T("plugins.state")).plugins[0].version === "2026.10v1", "and the list shows its version"); |
| 82 | await q(`const b = [...document.querySelectorAll('#plugPanel .pl[data-id="wide"] button')].find((x) => /Remove/.test(x.textContent)); b.click(); b.click();`); |
| 83 | check(await widthIs("300px", "removed"), "Remove (asked twice) takes it off the open page"); |
| 84 | check((await T("plugins.state")).plugins.length === 0, "and off the list"); |
| 85 | check((await waitFor(async () => (await pill()) === "Plugins|off" || null, "the status bar to say Plugins").catch(() => false)) !== false, "and the status bar goes back to Plugins", await pill()); |
| 86 | await q(`setPlug(false)`); |
| 87 | await q(`setPlug(true)`); |
| 88 | await waitFor(() => q(`return !!document.getElementById('plugBrowse')`), "the Browse all plugins link"); |
| 89 | const nTabs = (await T("tabs.list")).length; |
| 90 | await q(`document.getElementById('plugBrowse').click()`); |
| 91 | const browsed = await waitFor(async () => { const t = await T("tabs.list"); return t.length === nTabs + 1 ? t.find((x) => x.active) : null; }, "the store tab"); |
| 92 | check(browsed.url === base + "/store/#plugins", "Browse all plugins opens gitstable's list of plugins in a new tab", browsed.url); |
| 93 | await T("tabs.close", { tab: browsed.id }); |
| 94 | |
| 95 | // ---- one click from the plugin's own page on gitstable (the list is fetched only on gitstable, at most every 5 minutes) ---- |
| 96 | const listHits = async () => (await (await fetch(base + "/o/_plugin-list-hits")).json()).hits; |
| 97 | await T("page.goto", { url: base + "/store/wide/" }); |
| 98 | const offer = await waitFor(async () => { const p = await pill(); return /^Install: /.test(p) ? p : null; }, "the install offer"); |
| 99 | check(/^Install: Wide page/.test(offer), "on a plugin's page on gitstable, the status bar offers to install it", offer); |
| 100 | const h0 = await listHits(); |
| 101 | await T("page.goto", { url: `http://localhost:${port}/plugin-page` }); |
| 102 | await T("page.goto", { url: base + "/repositories/demo/wide/" }); |
| 103 | await sleep(500); |
| 104 | check((await listHits()) === h0 && h0 >= 1, "the list was fetched on gitstable only, and not again within five minutes", `${h0} → ${await listHits()}`); |
| 105 | check(/^Install: Wide page/.test(await pill()), "the repository's page offers it too"); |
| 106 | await q(`document.getElementById('plugBtn').click()`); |
| 107 | const ask2 = await waitFor(() => q(`const a = document.getElementById('plugAsk'); return a ? a.textContent : ''`), "the look-up the offer started"); |
| 108 | check(/Wide page 2026\.11v1/.test(ask2) && /can't run code/.test(ask2) && (await T("plugins.state")).plugins.length === 0, "one click opens Plugins with it looked up, and installs nothing yet", ask2); |
| 109 | await q(`document.getElementById('plugInstall').click()`); |
| 110 | await waitFor(async () => (await T("plugins.state")).plugins.length === 1, "the install"); |
| 111 | // (Here the stand-in gitstable is on 127.0.0.1, a site this demo plugin changes, so the status bar now names it.) |
| 112 | check(!/^(Install|Update)/.test(await waitFor(async () => { const p = await pill(); return /^Install/.test(p) ? null : p; }, "the offer to go")), "once installed, the page stops offering it", await pill()); |
| 113 | await T("plugins.remove", { id: "wide" }); |
| 114 | await q(`setPlug(false)`); |
| 115 | |
| 116 | // ---- gitstable's Install button: a link back to the page with ?install=owner/repo@tag ---- |
| 117 | await T("page.goto", { url: base + "/store/wide/?install=demo/wide@2026.11v1" }); |
| 118 | const ask3 = await waitFor(() => q(`const a = document.getElementById('plugAsk'); return document.getElementById('plugPanel').classList.contains('open') && a ? a.textContent : ''`), "Plugins opened by the install link"); |
| 119 | check(/Wide page 2026\.11v1/.test(ask3) && /can't run code/.test(ask3) && (await T("plugins.state")).plugins.length === 0, "gitstable's Install link opens Plugins with that release looked up, and installs nothing", ask3); |
| 120 | await q(`setPlug(false)`); |
| 121 | await T("page.goto", { url: base + "/store/wide/?install=demo/sneaky@2026.11v1" }); |
| 122 | await sleep(1000); |
| 123 | check(!(await q(`return document.getElementById('plugPanel').classList.contains('open')`)), "a link naming a repository that isn't a listed plugin does nothing"); |
| 124 | } |