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
ui.mjs · 12.1 KB · 167 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 | // Back and Forward are greyed out when there is nowhere to go that way. |
| 62 | const navBtns = () => c.ev(`(document.getElementById('back').disabled ? 'back-off' : 'back-on') + ' ' + (document.getElementById('fwd').disabled ? 'fwd-off' : 'fwd-on')`); |
| 63 | const waitNav = async (want) => { let v = ""; for (let i = 0; i < 50; i++) { v = await navBtns(); if (v === want) break; await sleep(100); } return v; }; |
| 64 | const navTab = await T("tabs.new", { url: base + "/free", wait: true }); |
| 65 | let nb = await waitNav("back-off fwd-off"); |
| 66 | check(nb === "back-off fwd-off", "a new tab's first page: Back and Forward are both greyed out", nb); |
| 67 | await T("page.goto", { url: base + "/links" }); |
| 68 | nb = await waitNav("back-on fwd-off"); |
| 69 | check(nb === "back-on fwd-off", "after going to a second page, Back works and Forward is still greyed out", nb); |
| 70 | await c.ev(`document.getElementById('back').click(); 1`); |
| 71 | nb = await waitNav("back-off fwd-on"); |
| 72 | check(nb === "back-off fwd-on", "after going back, Forward works and Back is greyed out", nb); |
| 73 | await T("tabs.close", { tab: navTab.id }); |
| 74 | |
| 75 | // Hovering a link shows its address in the status area: green for this site, amber for another, and not by colour alone. |
| 76 | 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 })()`); |
| 77 | await T("page.goto", { url: base + "/links" }); |
| 78 | await sleep(500); |
| 79 | check((await linkBar()) === null, "no link under the pointer: the link bar is hidden"); |
| 80 | await T("page.hover", { selector: "#same" }); |
| 81 | await sleep(300); |
| 82 | let lb = await linkBar(); |
| 83 | 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); |
| 84 | await T("page.hover", { selector: "#other" }); |
| 85 | await sleep(300); |
| 86 | lb = await linkBar(); |
| 87 | 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); |
| 88 | await T("page.hover", { selector: "#plain" }); |
| 89 | await sleep(300); |
| 90 | check((await linkBar()) === null, "moving off the link hides the bar again"); |
| 91 | await T("page.hover", { selector: "#same" }); |
| 92 | await sleep(300); |
| 93 | await T("page.goto", { url: base + "/free" }); |
| 94 | await sleep(400); |
| 95 | check((await linkBar()) === null, "navigating away clears a stale link address"); |
| 96 | |
| 97 | // Reload button: reloads the page, and is a Stop button while a page is loading. |
| 98 | await T("page.goto", { url: base + "/free" }); |
| 99 | await T("page.eval", { js: "window.__marker = 'before-reload'" }); |
| 100 | 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"); |
| 101 | // Drawn icons, not emoji: emoji depend on the installed font (on Linux some show as empty boxes). |
| 102 | const icons = await c.ev(`(() => ["newTab", "back", "fwd", "keepBtn", "starBtn", "galleryBtn", "tourBtn", "bmBtn", "chatBtn", "readerBtn", "keyBtn", "inspectBtn"] |
| 103 | .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" : ''; }) |
| 104 | .filter(Boolean).join(", "))()`); |
| 105 | check(icons === "", "every toolbar button has a drawn icon and no emoji", icons); |
| 106 | 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"); |
| 107 | await c.ev(`document.getElementById('reload').click()`); |
| 108 | let gone = false; |
| 109 | for (let i = 0; i < 60 && !gone; i++) { await sleep(100); gone = (await T("page.eval", { js: "window.__marker === undefined" })).value === true; } |
| 110 | check(gone, "clicking it reloads the page (a value the page held is gone)"); |
| 111 | // while a slow page loads it is a Stop button, and clicking it stops the load |
| 112 | const slow = T("page.goto", { url: base + "/slow", wait: false }); |
| 113 | await sleep(500); |
| 114 | const during = await c.ev(`(() => { const b = document.getElementById('reload'); return b.getAttribute('aria-label') + '|' + b.title })()`); |
| 115 | check(/^Stop\|Stop/.test(during), "while a page is loading the button is a Stop button", during); |
| 116 | await slow.catch(() => {}); |
| 117 | await c.ev(`document.getElementById('reload').click()`); |
| 118 | let back = false; |
| 119 | for (let i = 0; i < 60 && !back; i++) { await sleep(100); back = await c.ev(`document.getElementById('reload').getAttribute('aria-label') === 'Reload'`); } |
| 120 | check(back, "clicking Stop ends the load and the button goes back to Reload"); |
| 121 | |
| 122 | // find in page: the page's own find, with a count, next and previous, per tab |
| 123 | const findTab = await T("tabs.new", { url: base + "/keep-me" }); |
| 124 | await sleep(600); |
| 125 | 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') }))()`); |
| 126 | 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())); }; |
| 127 | const fkey = (k, shift = false) => c.ev(`document.getElementById('findIn').dispatchEvent(new KeyboardEvent('keydown', { key: ${JSON.stringify(k)}, shiftKey: ${shift}, bubbles: true }))`); |
| 128 | await c.ev(`onTabCmd("find")`); |
| 129 | let f = await waitFb((v) => v.open, "the find bar"); |
| 130 | check(f.open && f.focus === "findIn", "Edit ▸ Find (⌘F) opens the find bar with the typing in it", JSON.stringify(f)); |
| 131 | await c.ev(`(() => { const i = document.getElementById('findIn'); i.value = 'marmalade'; i.dispatchEvent(new Event('input')); })()`); |
| 132 | f = await waitFb((v) => v.n === "1 of 2", "the first match"); |
| 133 | check(f.focus === "findIn", "typing finds the matches and counts them (1 of 2), and the typing stays in the bar", JSON.stringify(f)); |
| 134 | await fkey("Enter"); |
| 135 | f = await waitFb((v) => v.n === "2 of 2", "the next match"); |
| 136 | check(true, "Enter goes to the next match"); |
| 137 | await fkey("Enter", true); |
| 138 | f = await waitFb((v) => v.n === "1 of 2", "the previous match"); |
| 139 | check(true, "Shift+Enter goes back to the previous one"); |
| 140 | await c.ev(`onTabCmd("find-next")`); |
| 141 | f = await waitFb((v) => v.n === "2 of 2", "Find Next"); |
| 142 | check(true, "Find Next (⌘G) goes to the next match"); |
| 143 | await c.ev(`(() => { const i = document.getElementById('findIn'); i.value = 'zebrafinchqq'; i.dispatchEvent(new Event('input')); })()`); |
| 144 | f = await waitFb((v) => v.n === "No matches", "no matches"); |
| 145 | check(f.none, "text that is not on the page says No matches", JSON.stringify(f)); |
| 146 | await c.ev(`(() => { const i = document.getElementById('findIn'); i.value = 'quick brown'; i.dispatchEvent(new Event('input')); })()`); |
| 147 | await waitFb((v) => v.n === "1 of 1", "the phrase"); |
| 148 | // another tab has its own find: the bar goes away there, and comes back here |
| 149 | const otherTab = await T("tabs.new", { url: base + "/free" }); |
| 150 | await sleep(400); |
| 151 | f = await fb(); |
| 152 | check(!f.open, "another tab does not show this tab's find bar", JSON.stringify(f)); |
| 153 | await T("tabs.activate", { tab: findTab.id }); |
| 154 | f = await waitFb((v) => v.open && v.v === "quick brown", "the find bar back on its tab"); |
| 155 | check(true, "switching back shows the find bar with what was being found"); |
| 156 | await fkey("Escape"); |
| 157 | f = await waitFb((v) => !v.open, "the bar to close"); |
| 158 | const sel = (await T("page.eval", { js: "String(getSelection())" })).value; |
| 159 | check(sel === "quick brown", "Esc closes the find bar and leaves the match selected on the page (⌘C copies it)", JSON.stringify(sel)); |
| 160 | await c.ev(`onTabCmd("find")`); |
| 161 | f = await waitFb((v) => v.open, "the bar again"); |
| 162 | const selAll = await c.ev(`(() => { const i = document.getElementById('findIn'); return i.selectionStart === 0 && i.selectionEnd === i.value.length })()`); |
| 163 | check(f.v === "quick brown" && selAll, "opening it again offers the last search, selected so typing replaces it", JSON.stringify(f)); |
| 164 | await fkey("Escape"); |
| 165 | await T("tabs.close", { tab: otherTab.id }).catch(() => {}); |
| 166 | await T("tabs.close", { tab: findTab.id }).catch(() => {}); |
| 167 | } |