A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
omnibox.mjs · 16.0 KB · 220 lines
1// The address bar: completes sites you use, suggests open tabs, kept copies and searches as you type, hands questions to the
2// assistant, never sends an address for suggestions, and passes ⌘K to the page.
3export default async function (ctx) {
4 const { call: T, check, base, sleep } = ctx;
5 const cdp = await ctx.chrome();
6 const q = (js) => cdp.ev(`(() => { ${js} })()`);
7 async function waitFor(fn, what, tries = 60) {
8 for (let i = 0; i < tries; i++) { const v = await fn(); if (v) return v; await sleep(100); }
9 throw new Error(`timed out waiting for ${what}`);
10 }
11 const port = new URL(base).port;
12 const local = `http://localhost:${port}`;
13 // type as a person does: the text arrives through input events
14 const type = (text, inputType = "insertText") => q(`const u = document.getElementById('url'); u.focus(); u.value = ${JSON.stringify(text)}; u.dispatchEvent(new InputEvent('input', { inputType: ${JSON.stringify(inputType)} })); return true`);
15 const key = (k, extra = {}) => q(`document.getElementById('url').dispatchEvent(new KeyboardEvent('keydown', Object.assign({ key: ${JSON.stringify(k)}, bubbles: true }, ${JSON.stringify(extra)}))); return true`);
16 const rows = () => q(`return [...document.querySelectorAll('#omni.open .or')].map((r) => r.dataset.kind + ':' + r.querySelector('.tt').textContent)`);
17 const bar = () => q(`const u = document.getElementById('url'); return { v: u.value, s: u.selectionStart, e: u.selectionEnd }`);
18 const asked = async () => (await fetch(base + "/suggest/_asked")).json();
19 const active = async () => (await T("tabs.list")).find((t) => t.active);
20
21 // tabs to find: a page on "localhost" and one with its own ⌘K box
22 const cmdk = await T("tabs.new", { url: base + "/cmdk" });
23 await T("tabs.new", { url: local + "/free" });
24 await sleep(400);
25 const kept = await T("tabs.new", { url: base + "/keep-me" });
26 await sleep(300);
27 await T("keep.tab", { tab: kept.id });
28
29 // a site you use is completed inline, and Enter goes there with its own scheme and port
30 await T("tabs.new", { url: base + "/free" });
31 await sleep(300);
32 await type("loc");
33 await waitFor(async () => (await rows()).length, "the suggestions");
34 const b = await bar();
35 check(b.v === `localhost:${port}` && b.s === 3 && b.e === b.v.length, "typing the start of a site you use completes it, with the rest selected so typing on replaces it", JSON.stringify(b));
36 check((await rows())[0] === `go:localhost:${port}`, "and the first row says Enter goes there", JSON.stringify(await rows()));
37 await key("Enter");
38 await waitFor(async () => (await active()).url.startsWith(local + "/"), "the completed site to open");
39 check(true, "Enter opens the completed site (plain http, its port kept)");
40
41 // deleting does not complete again
42 await type("lo", "deleteContentBackward");
43 await waitFor(async () => (await rows()).length, "rows after deleting");
44 check((await bar()).v === "lo" && (await rows())[0] === "search:lo", "while deleting, nothing is completed", JSON.stringify(await bar()));
45 await key("Escape");
46
47 // an open tab is offered, and picking it switches to it
48 await type("command k");
49 await waitFor(async () => (await rows()).some((r) => r === "tab:Command K"), "the open tab row");
50 const r1 = await rows();
51 check(r1[0] === "search:command k" && r1.at(-1) === "ask:command k", "a search is first, the open tab is listed, and asking the assistant is last", JSON.stringify(r1));
52 for (let i = 0; i < r1.indexOf("tab:Command K"); i++) await key("ArrowDown");
53 await key("Enter");
54 await waitFor(async () => (await active()).id === cmdk.id, "the switch to the tab");
55 check(true, "↓ and Enter on an open tab switches to it");
56
57 // kept copies, and search suggestions from the search site
58 await type("marmalade");
59 await waitFor(async () => (await rows()).some((r) => r === "kept:Marmalade notes"), "the kept copy row");
60 check(true, "a kept copy is found by its title");
61 await waitFor(async () => (await rows()).includes("suggest:marmalade recipe"), "search suggestions");
62 check(true, "search suggestions arrive from the search site");
63 await key("Escape");
64
65 // addresses are never sent for suggestions; nor anything while they are switched off
66 await type("127.0.0.1/private/path");
67 await type("intranet.example/secret");
68 await type("localhost:1234");
69 await sleep(500);
70 await type("pear tart");
71 await waitFor(async () => (await rows()).includes("suggest:pear tart recipe"), "suggestions for a search");
72 await q(`[...document.querySelectorAll('#omni .of a')][0].click(); return true`);
73 await type("plum jam");
74 await sleep(500);
75 const sent = await asked(); // (read before turning them back on, which asks for what is typed then, by design)
76 await q(`[...document.querySelectorAll('#omni .of a')][0].click(); return true`); // back on
77 check(sent.includes("pear tart") && !sent.some((x) => /127\.0\.0\.1|intranet|localhost|plum/.test(x)), "only searches are sent for suggestions: never an address, and nothing while switched off", JSON.stringify(sent));
78 await key("Escape");
79 await key("Escape");
80
81 // what you type is not overwritten by a page that finishes loading meanwhile
82 await T("tabs.new", { url: base + "/free" });
83 await sleep(300);
84 await type("half typed");
85 await q(`active.wv.loadURL(${JSON.stringify(base + "/free?later=1")}); return true`);
86 await sleep(700);
87 check((await bar()).v === "half typed", "a page loading while you type does not overwrite the address bar", JSON.stringify(await bar()));
88 await key("Escape");
89 await key("Escape");
90 check((await bar()).v.includes("/free?later=1"), "Esc twice puts the page's address back");
91
92 // ⌘K reaches the page even when the address bar has the focus (as right after a new tab)
93 await T("tabs.activate", { tab: cmdk.id });
94 await sleep(200);
95 await q(`document.getElementById('url').focus(); document.dispatchEvent(new KeyboardEvent('keydown', { key: 'k', metaKey: true, bubbles: true })); return true`);
96 const got = await waitFor(async () => { const v = await T("page.eval", { js: "({ n: window.__cmdk || 0, f: document.activeElement && document.activeElement.id })" }); return v.value && v.value.n ? v.value : null; }, "the page's ⌘K", 30).catch(() => null);
97 check(got && got.n === 1 && got.f === "box", "⌘K in the address bar goes to the page, whose own search box gets the focus", JSON.stringify(got || { page: (await T("page.eval", { js: "({ url: location.href, keys: window.__keys || [] })" })).value, chrome: await q(`return { id: active.id, url: active.url, ready: active.ready }`), cmdk: cmdk.id }));
98
99 // the tab keys belong to the browser, even on a page that swallows every key it gets
100 const greedy = await T("tabs.new", { url: base + "/greedy" });
101 await sleep(500);
102 const before = (await T("tabs.list")).map((t) => t.id);
103 const next = before[before.indexOf(greedy.id) + 1] ?? before[0];
104 await q(`active.wv.focus(); return true`);
105 await T("page.eval", { js: "document.getElementById('box').focus(), true" });
106 const mod = process.platform === "darwin" ? "meta" : "control"; // ⌘⇧] on a Mac, Ctrl+Shift+] elsewhere
107 await q(`active.wv.sendInputEvent({ type: "keyDown", keyCode: "]", modifiers: ["${mod}", "shift"] }); active.wv.sendInputEvent({ type: "keyUp", keyCode: "]", modifiers: ["${mod}", "shift"] }); return true`);
108 const moved = await waitFor(async () => (await active()).id === next, "the next tab", 30).catch(() => false);
109 const seen = (await fetch(base + "/free").then(() => null), (await T("tabs.list")).find((t) => t.id === greedy.id));
110 check(!!moved, "⌘⇧] switches to the next tab even when the page has the focus and swallows every key", JSON.stringify({ active: (await active()).id, greedy: greedy.id, next, seen: !!seen }));
111 await T("tabs.activate", { tab: greedy.id });
112 await sleep(200);
113 const pageSaw = (await T("page.eval", { js: "window.__seen || []" })).value;
114 check(Array.isArray(pageSaw) && !pageSaw.some((k) => /\]|\}/.test(k)), "and the page never sees the key", JSON.stringify(pageSaw));
115 // ⌘R reloads even when the page has the keyboard (it used to need the toolbar in focus)
116 await T("page.eval", { js: "window.__mark = 1, document.getElementById('box').focus(), true" });
117 await q(`active.wv.focus(); active.wv.sendInputEvent({ type: "keyDown", keyCode: "r", modifiers: ["${mod}"] }); active.wv.sendInputEvent({ type: "keyUp", keyCode: "r", modifiers: ["${mod}"] }); return true`);
118 const reloaded = await waitFor(async () => (await T("page.eval", { js: "window.__mark === undefined" }).catch(() => ({ value: false }))).value, "the reload", 40).catch(() => false);
119 check(!!reloaded, "⌘R reloads the page even while the page has the keyboard and swallows every key");
120 await T("tabs.close", { tab: greedy.id }).catch(() => {});
121
122 // ⌘← / ⌘→ (Alt+← / Alt+→ elsewhere) go back and forward from the page, but never inside a text field
123 const navMod = process.platform === "darwin" ? "meta" : "alt";
124 const arrow = (k) => q(`active.wv.focus(); active.wv.sendInputEvent({ type: "keyDown", keyCode: "${k}", modifiers: ["${navMod}"] }); active.wv.sendInputEvent({ type: "keyUp", keyCode: "${k}", modifiers: ["${navMod}"] }); return true`);
125 const urlNow = async () => (await active()).url;
126 await T("tabs.new", { url: base + "/free" });
127 await sleep(300);
128 await T("page.goto", { url: base + "/forms" });
129 await T("page.eval", { js: "document.activeElement && document.activeElement.blur(), true" });
130 await arrow("Left");
131 const wentBack = await waitFor(async () => (await urlNow()).endsWith("/free"), "⌘← to go back", 30).catch(() => false);
132 check(!!wentBack, "⌘← on a page goes back a page", await urlNow());
133 await sleep(300);
134 await arrow("Right");
135 const wentFwd = await waitFor(async () => (await urlNow()).endsWith("/forms"), "⌘→ to go forward", 30).catch(() => false);
136 check(!!wentFwd, "⌘→ goes forward again", await urlNow());
137 await sleep(300);
138 await T("page.eval", { js: "(document.querySelector('input:not([type=hidden]):not([type=password])') || document.querySelector('input')).focus(), true" });
139 await arrow("Left");
140 await sleep(600);
141 check((await urlNow()).endsWith("/forms"), "but in a text field ⌘← moves the cursor and the page stays", await urlNow());
142
143 // "/" commands: the askrobots tools of the sign-in (a stand-in here); nothing without a sign-in
144 await T("tabs.new", { url: base + "/free" });
145 await sleep(300);
146 const notes = async () => (await fetch(base + "/o/_notes")).json();
147 const cards = () => q(`return [...document.querySelectorAll('#chatLog .cmd')].map((c) => ({ typed: c.querySelector('.typed').textContent, s: c.querySelector('.s').textContent, links: [...c.querySelectorAll('a')].map((a) => a.title), items: [...c.querySelectorAll('li')].map((l) => l.textContent), details: (c.querySelector('pre') || {}).textContent || '', open: !!(c.querySelector('details') || {}).open }))`);
148 const toasts = () => q(`return [...document.querySelectorAll('#toast .note')].map((n) => n.textContent)`);
149 const chatOpen = () => q(`return document.getElementById('chat').classList.contains('open')`);
150 await type("/no");
151 await waitFor(async () => (await rows()).length, "rows for a command before signing in");
152 check((await rows()).join("|") === "notice:Connect askrobots to use / commands", "before connecting, / offers to connect and lists nothing", JSON.stringify(await rows()));
153 await key("Enter");
154 check(await q(`return document.getElementById('chat').classList.contains('open')`), "and Enter opens the panel where you connect");
155 await cdp.ev(`setChat(false)`);
156 await T("mcp.connect", { id: "askrobots", wait: true });
157
158 await type("/");
159 await waitFor(async () => (await rows()).length > 3, "the command list");
160 const all = await rows();
161 check(all.includes("cmd:/lookup") && all.includes("cmd:/wipe") && all.includes("cmd:/create_note") && !all.some((r) => /upload_file/.test(r)), "once connected, / lists the service's tools (not the browser's own upload)", JSON.stringify(all));
162 await type("/no");
163 await waitFor(async () => (await rows())[0] === "cmd:/create_note", "the /note shortcut");
164 await key("Tab");
165 check((await bar()).v === "/create_note ", "the site's short names work (/note is create_note), and Tab completes the command", JSON.stringify(await bar()));
166
167 await type("/note buy milk");
168 await waitFor(async () => (await rows())[0] === "cmd:/note buy milk", "the command row");
169 check(/content: buy milk/.test(await q(`return document.querySelector('#omni .or').textContent`)), "the row shows what will be saved before Enter");
170 await key("Enter");
171 await waitFor(async () => (await toasts()).some((t) => /Saved note: buy milk \(private\)/.test(t)), "the saved note's toast");
172 check((await notes()).notes.some((n) => n.content === "buy milk"), "Enter runs it on the service, and a save says what was saved, in words", JSON.stringify(await toasts()));
173 check(!(await chatOpen()), "a save does not open the chat");
174 const pageUrl = `${base}/notes/n1/`;
175 await q(`[...document.querySelectorAll('#toast .note a')].find((a) => a.title === ${JSON.stringify(pageUrl)}).click(); return true`);
176 await waitFor(async () => (await active()).url === pageUrl, "the note's page to open");
177 check(true, "Open on the toast opens the note's own page on the service, in a new tab");
178
179 await type("/lookup marmalade");
180 await waitFor(async () => (await rows())[0] === "cmd:/lookup marmalade", "the lookup row");
181 await key("Enter");
182 await waitFor(chatOpen, "the chat to open for an answer to read");
183 const c1 = await waitFor(async () => (await cards()).length >= 2 && await cards(), "the commands in the chat");
184 check(c1[0].typed === "/note buy milk" && /Saved note: buy milk \(private\)Open/.test(c1[0].s) && c1[0].links[0] === pageUrl, "the save is in the chat too, with its page", JSON.stringify(c1[0]));
185 check(c1[1].typed === "/lookup marmalade" && /Found 2 notes about marmalade/.test(c1[1].details) && c1[1].open, "a lookup opens the chat, with the answer shown", JSON.stringify(c1[1]));
186 check(!(await q(`return !!document.querySelector('#chatLog .cmd script, #chatLog .cmd img')`)), "an answer is shown as text, whatever it says");
187 await cdp.ev(`setChat(false)`);
188
189 await type("/list_links");
190 await waitFor(async () => (await rows())[0] === "cmd:/list_links", "the list row");
191 await key("Enter");
192 await waitFor(async () => (await cards()).length >= 3, "the list in the chat");
193 const c2 = (await cards())[2];
194 // The stand-in holds l1 (its page on the service) and l2 (its page elsewhere), plus whatever earlier suites saved there
195 // when the suites share one demo server (one at a time, as in CI).
196 const found = Number((/(\d+) found/.exec(c2.s) || [])[1]);
197 check(found >= 2 && c2.items.length === found && c2.items.includes("Headers echo") && c2.items.includes("Free page") && c2.links.includes(`${base}/links/l1/`) && c2.links.every((l) => l.startsWith(base + "/")),
198 "a list shows each thing found, linked to its page only when that page is on the service's own site", JSON.stringify(c2));
199 await cdp.ev(`setChat(false)`);
200
201 await type("/wipe");
202 await waitFor(async () => (await rows())[0] === "cmd:/wipe", "the wipe row");
203 await key("Enter");
204 await waitFor(async () => (await rows()).some((r) => /Press Enter again/.test(r)), "the question");
205 check((await notes()).wiped === 0, "a command the service marks as destructive asks first, and does nothing yet", JSON.stringify(await rows()));
206 await key("Enter");
207 await waitFor(async () => (await notes()).wiped === 1, "the confirmed wipe");
208 check(true, "a second Enter runs it");
209
210 await type("/save hello");
211 await waitFor(async () => (await rows()).length, "the save row");
212 await key("Enter");
213 await sleep(300);
214 check(/needs project_id too/.test(await q(`return document.getElementById('omni').textContent`)), "a command needing more than one line can give says so instead of running", await q(`return document.getElementById('omni').textContent`));
215 await type("/zzz");
216 await waitFor(async () => /no command/.test((await rows()).join()), "the unknown command note");
217 check(true, "an unknown command says so");
218 check(!(await asked()).some((x) => x.startsWith("/")), "commands are never sent for search suggestions");
219 await key("Escape");
220}