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
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 56 minutes ago
History
ui.mjs · 11.1 KB · 153 lines
| 1 | // The chrome UI: password-manager offers and panel, locking, and the built-in Help page. |
| 2 | import { fileURLToPath } from "node:url"; |
| 3 | |
| 4 | export default async function (ctx) { |
| 5 | const { call: T, check, base, sleep } = ctx; |
| 6 | const c = await ctx.chrome(); |
| 7 | const bar = () => c.ev(`(() => { const b = document.getElementById('offerbar'); return b.classList.contains('show') ? b.innerText.replace(/\\s+/g,' ').trim() : '' })()`); |
| 8 | |
| 9 | await T("vault.unlock", { master: ctx.vaultMaster }); |
| 10 | const start = (await T("vault.state")).count; |
| 11 | |
| 12 | // a login page we have a saved login for → the fill offer |
| 13 | await T("page.goto", { url: base + "/login" }); |
| 14 | await sleep(900); |
| 15 | let t = await bar(); |
| 16 | check(/Saved login for/.test(t) && /Sign in as demo/.test(t), "fill offer on a login page with a saved login", t); |
| 17 | |
| 18 | // a real typed submit with NEW credentials → the save offer, which survives the navigation |
| 19 | await T("page.type", { selector: "input[name=username]", text: "newuser" }); |
| 20 | await T("page.type", { selector: "input[name=password]", text: "Sup3r-secret-pw" }); |
| 21 | await T("page.press", { key: "Enter" }); |
| 22 | await sleep(1500); |
| 23 | t = await bar(); |
| 24 | check(/Save password for/.test(t) && t.includes("newuser"), "save offer after a real submit (and it survives the navigation)", t); |
| 25 | check(!(await c.ev(`document.documentElement.innerHTML`)).includes("Sup3r-secret-pw"), "the password is NOT present anywhere in the chrome UI's DOM"); |
| 26 | await c.ev(`[...document.querySelectorAll('#offerbar .sbtn')].find(b => b.textContent === 'Save').click(); 1`); |
| 27 | await sleep(700); |
| 28 | check((await T("vault.state")).count === start + 1, "clicking Save stores the login", `${start} → ${(await T("vault.state")).count}`); |
| 29 | check((await bar()) === "", "the offer bar goes away after saving"); |
| 30 | |
| 31 | // a page-forged submit event must NOT raise a save offer (only trusted input counts) |
| 32 | await T("page.goto", { url: base + "/login" }); |
| 33 | await sleep(900); |
| 34 | await c.ev(`tabs[0].fillOffer = null; tabs[0].saveOffer = null; 1`); |
| 35 | await T("page.eval", { js: `(() => { document.querySelector('input[name=username]').value='fake'; document.querySelector('input[name=password]').value='fake-pw-zzz'; document.querySelector('form').dispatchEvent(new Event('submit',{bubbles:true,cancelable:true})); })()` }); |
| 36 | await sleep(800); |
| 37 | check((await c.ev(`!!tabs[0].saveOffer`)) === false, "a page-forged submit event does not trigger a save offer"); |
| 38 | |
| 39 | // the panel |
| 40 | await c.ev(`document.getElementById('keyBtn').click(); 1`); |
| 41 | await sleep(600); |
| 42 | const panel = await c.ev(`document.getElementById('vaultPanel').innerText.replace(/\\s+/g,' ')`); |
| 43 | check(/saved/.test(panel) && /newuser/.test(panel) && /demo/.test(panel), "the 🔑 panel lists the logins", panel.slice(0, 80)); |
| 44 | check(!panel.includes("Sup3r-secret-pw") && !panel.includes("demo-pass"), "the panel shows no passwords"); |
| 45 | await c.ev(`document.getElementById('keyBtn').click(); 1`); |
| 46 | |
| 47 | // locking |
| 48 | await c.ev(`window.x402.vault.lock(); 1`); |
| 49 | await sleep(400); |
| 50 | check((await T("vault.state")).unlocked === false, "lock() locks the vault"); |
| 51 | await T("login").then(() => check(false, "login worked while locked"), (e) => check(e.code === "locked", "login is refused while locked")); |
| 52 | |
| 53 | // Help renders, at an anchor |
| 54 | const help = new URL("../../dist/help.html", import.meta.url); |
| 55 | const nt = await T("tabs.new", { url: fileURLToPath(help) ? help.href + "#automation" : "", wait: true }); |
| 56 | await sleep(500); |
| 57 | const title = (await T("page.eval", { tab: nt.id, js: "document.title + ' | ' + location.hash + ' | h2s=' + document.querySelectorAll('h2').length" })).value; |
| 58 | check(/dbbasic-browser Help/.test(title) && /#automation/.test(title), "the Help page renders in a tab, at its anchor", title); |
| 59 | await T("tabs.close", { tab: nt.id }); |
| 60 | |
| 61 | // Hovering a link shows its address in the status area: green for this site, amber for another, and not by colour alone. |
| 62 | const linkBar = () => c.ev(`(() => { const e = document.getElementById('link'); return e.classList.contains('show') ? { text: e.innerText.replace(/\\s+/g, ' ').trim(), cls: e.className } : null })()`); |
| 63 | await T("page.goto", { url: base + "/links" }); |
| 64 | await sleep(500); |
| 65 | check((await linkBar()) === null, "no link under the pointer: the link bar is hidden"); |
| 66 | await T("page.hover", { selector: "#same" }); |
| 67 | await sleep(300); |
| 68 | let lb = await linkBar(); |
| 69 | check(lb && /same/.test(lb.cls) && lb.text.includes(base + "/free?from=links") && /this site/.test(lb.text), "hovering a link on this site shows its full address, marked 'this site' (green)", lb && lb.text); |
| 70 | await T("page.hover", { selector: "#other" }); |
| 71 | await sleep(300); |
| 72 | lb = await linkBar(); |
| 73 | check(lb && /other/.test(lb.cls) && /localhost/.test(lb.text) && /other site/.test(lb.text), "a link to another site is marked 'other site' (amber)", lb && lb.text); |
| 74 | await T("page.hover", { selector: "#plain" }); |
| 75 | await sleep(300); |
| 76 | check((await linkBar()) === null, "moving off the link hides the bar again"); |
| 77 | await T("page.hover", { selector: "#same" }); |
| 78 | await sleep(300); |
| 79 | await T("page.goto", { url: base + "/free" }); |
| 80 | await sleep(400); |
| 81 | check((await linkBar()) === null, "navigating away clears a stale link address"); |
| 82 | |
| 83 | // Reload button: reloads the page, and is a Stop button while a page is loading. |
| 84 | await T("page.goto", { url: base + "/free" }); |
| 85 | await T("page.eval", { js: "window.__marker = 'before-reload'" }); |
| 86 | check(await c.ev(`(() => { const b = document.getElementById('reload'); return !!b && !!b.querySelector('svg') && b.getAttribute('aria-label') === 'Reload' && /Reload/.test(b.title) })()`), "the toolbar has a reload button"); |
| 87 | // Drawn icons, not emoji: emoji depend on the installed font (on Linux some show as empty boxes). |
| 88 | const icons = await c.ev(`(() => ["newTab", "back", "fwd", "keepBtn", "starBtn", "galleryBtn", "tourBtn", "bmBtn", "chatBtn", "readerBtn", "keyBtn", "inspectBtn"] |
| 89 | .map((id) => { const b = document.getElementById(id); const s = b && b.querySelector('svg.ico'); return !s ? id + ": no icon" : /\\p{Extended_Pictographic}/u.test(b.textContent) ? id + ": emoji" : s.getAttribute('aria-hidden') !== 'true' ? id + ": icon not hidden" : ''; }) |
| 90 | .filter(Boolean).join(", "))()`); |
| 91 | check(icons === "", "every toolbar button has a drawn icon and no emoji", icons); |
| 92 | check(await c.ev(`!!document.getElementById('back').getAttribute('aria-label') && !!document.getElementById('fwd').getAttribute('aria-label') && !!document.getElementById('newTab').getAttribute('aria-label')`), "icon-only buttons have a name for screen readers"); |
| 93 | await c.ev(`document.getElementById('reload').click()`); |
| 94 | let gone = false; |
| 95 | for (let i = 0; i < 60 && !gone; i++) { await sleep(100); gone = (await T("page.eval", { js: "window.__marker === undefined" })).value === true; } |
| 96 | check(gone, "clicking it reloads the page (a value the page held is gone)"); |
| 97 | // while a slow page loads it is a Stop button, and clicking it stops the load |
| 98 | const slow = T("page.goto", { url: base + "/slow", wait: false }); |
| 99 | await sleep(500); |
| 100 | const during = await c.ev(`(() => { const b = document.getElementById('reload'); return b.getAttribute('aria-label') + '|' + b.title })()`); |
| 101 | check(/^Stop\|Stop/.test(during), "while a page is loading the button is a Stop button", during); |
| 102 | await slow.catch(() => {}); |
| 103 | await c.ev(`document.getElementById('reload').click()`); |
| 104 | let back = false; |
| 105 | for (let i = 0; i < 60 && !back; i++) { await sleep(100); back = await c.ev(`document.getElementById('reload').getAttribute('aria-label') === 'Reload'`); } |
| 106 | check(back, "clicking Stop ends the load and the button goes back to Reload"); |
| 107 | |
| 108 | // find in page: the page's own find, with a count, next and previous, per tab |
| 109 | const findTab = await T("tabs.new", { url: base + "/keep-me" }); |
| 110 | await sleep(600); |
| 111 | const fb = () => c.ev(`(() => ({ open: document.getElementById('findBar').classList.contains('open'), v: document.getElementById('findIn').value, n: document.getElementById('findCount').textContent, focus: document.activeElement && document.activeElement.id, none: document.getElementById('findBar').classList.contains('none') }))()`); |
| 112 | const waitFb = async (pred, what) => { for (let i = 0; i < 50; i++) { const v = await fb(); if (pred(v)) return v; await sleep(100); } throw new Error("timed out waiting for " + what + ": " + JSON.stringify(await fb())); }; |
| 113 | const fkey = (k, shift = false) => c.ev(`document.getElementById('findIn').dispatchEvent(new KeyboardEvent('keydown', { key: ${JSON.stringify(k)}, shiftKey: ${shift}, bubbles: true }))`); |
| 114 | await c.ev(`onTabCmd("find")`); |
| 115 | let f = await waitFb((v) => v.open, "the find bar"); |
| 116 | check(f.open && f.focus === "findIn", "Edit ▸ Find (⌘F) opens the find bar with the typing in it", JSON.stringify(f)); |
| 117 | await c.ev(`(() => { const i = document.getElementById('findIn'); i.value = 'marmalade'; i.dispatchEvent(new Event('input')); })()`); |
| 118 | f = await waitFb((v) => v.n === "1 of 2", "the first match"); |
| 119 | check(f.focus === "findIn", "typing finds the matches and counts them (1 of 2), and the typing stays in the bar", JSON.stringify(f)); |
| 120 | await fkey("Enter"); |
| 121 | f = await waitFb((v) => v.n === "2 of 2", "the next match"); |
| 122 | check(true, "Enter goes to the next match"); |
| 123 | await fkey("Enter", true); |
| 124 | f = await waitFb((v) => v.n === "1 of 2", "the previous match"); |
| 125 | check(true, "Shift+Enter goes back to the previous one"); |
| 126 | await c.ev(`onTabCmd("find-next")`); |
| 127 | f = await waitFb((v) => v.n === "2 of 2", "Find Next"); |
| 128 | check(true, "Find Next (⌘G) goes to the next match"); |
| 129 | await c.ev(`(() => { const i = document.getElementById('findIn'); i.value = 'zebrafinchqq'; i.dispatchEvent(new Event('input')); })()`); |
| 130 | f = await waitFb((v) => v.n === "No matches", "no matches"); |
| 131 | check(f.none, "text that is not on the page says No matches", JSON.stringify(f)); |
| 132 | await c.ev(`(() => { const i = document.getElementById('findIn'); i.value = 'quick brown'; i.dispatchEvent(new Event('input')); })()`); |
| 133 | await waitFb((v) => v.n === "1 of 1", "the phrase"); |
| 134 | // another tab has its own find: the bar goes away there, and comes back here |
| 135 | const otherTab = await T("tabs.new", { url: base + "/free" }); |
| 136 | await sleep(400); |
| 137 | f = await fb(); |
| 138 | check(!f.open, "another tab does not show this tab's find bar", JSON.stringify(f)); |
| 139 | await T("tabs.activate", { tab: findTab.id }); |
| 140 | f = await waitFb((v) => v.open && v.v === "quick brown", "the find bar back on its tab"); |
| 141 | check(true, "switching back shows the find bar with what was being found"); |
| 142 | await fkey("Escape"); |
| 143 | f = await waitFb((v) => !v.open, "the bar to close"); |
| 144 | const sel = (await T("page.eval", { js: "String(getSelection())" })).value; |
| 145 | check(sel === "quick brown", "Esc closes the find bar and leaves the match selected on the page (⌘C copies it)", JSON.stringify(sel)); |
| 146 | await c.ev(`onTabCmd("find")`); |
| 147 | f = await waitFb((v) => v.open, "the bar again"); |
| 148 | const selAll = await c.ev(`(() => { const i = document.getElementById('findIn'); return i.selectionStart === 0 && i.selectionEnd === i.value.length })()`); |
| 149 | check(f.v === "quick brown" && selAll, "opening it again offers the last search, selected so typing replaces it", JSON.stringify(f)); |
| 150 | await fkey("Escape"); |
| 151 | await T("tabs.close", { tab: otherTab.id }).catch(() => {}); |
| 152 | await T("tabs.close", { tab: findTab.id }).catch(() => {}); |
| 153 | } |