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

0 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 site icons (favicons) in tabs, address bar suggestions and the bookmarks panel 7249579 15 hours, 56 minutes ago History
omnibox.mjs · 15.3 KB · 215 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 await T("tabs.close", { tab: greedy.id }).catch(() => {});
116
117 // ⌘← / ⌘→ (Alt+← / Alt+→ elsewhere) go back and forward from the page, but never inside a text field
118 const navMod = process.platform === "darwin" ? "meta" : "alt";
119 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`);
120 const urlNow = async () => (await active()).url;
121 await T("tabs.new", { url: base + "/free" });
122 await sleep(300);
123 await T("page.goto", { url: base + "/forms" });
124 await T("page.eval", { js: "document.activeElement && document.activeElement.blur(), true" });
125 await arrow("Left");
126 const wentBack = await waitFor(async () => (await urlNow()).endsWith("/free"), "⌘← to go back", 30).catch(() => false);
127 check(!!wentBack, "⌘← on a page goes back a page", await urlNow());
128 await sleep(300);
129 await arrow("Right");
130 const wentFwd = await waitFor(async () => (await urlNow()).endsWith("/forms"), "⌘→ to go forward", 30).catch(() => false);
131 check(!!wentFwd, "⌘→ goes forward again", await urlNow());
132 await sleep(300);
133 await T("page.eval", { js: "(document.querySelector('input:not([type=hidden]):not([type=password])') || document.querySelector('input')).focus(), true" });
134 await arrow("Left");
135 await sleep(600);
136 check((await urlNow()).endsWith("/forms"), "but in a text field ⌘← moves the cursor and the page stays", await urlNow());
137
138 // "/" commands: the askrobots tools of the sign-in (a stand-in here); nothing without a sign-in
139 await T("tabs.new", { url: base + "/free" });
140 await sleep(300);
141 const notes = async () => (await fetch(base + "/o/_notes")).json();
142 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 }))`);
143 const toasts = () => q(`return [...document.querySelectorAll('#toast .note')].map((n) => n.textContent)`);
144 const chatOpen = () => q(`return document.getElementById('chat').classList.contains('open')`);
145 await type("/no");
146 await waitFor(async () => (await rows()).length, "rows for a command before signing in");
147 check((await rows()).join("|") === "notice:Connect askrobots to use / commands", "before connecting, / offers to connect and lists nothing", JSON.stringify(await rows()));
148 await key("Enter");
149 check(await q(`return document.getElementById('chat').classList.contains('open')`), "and Enter opens the panel where you connect");
150 await cdp.ev(`setChat(false)`);
151 await T("mcp.connect", { id: "askrobots", wait: true });
152
153 await type("/");
154 await waitFor(async () => (await rows()).length > 3, "the command list");
155 const all = await rows();
156 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));
157 await type("/no");
158 await waitFor(async () => (await rows())[0] === "cmd:/create_note", "the /note shortcut");
159 await key("Tab");
160 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()));
161
162 await type("/note buy milk");
163 await waitFor(async () => (await rows())[0] === "cmd:/note buy milk", "the command row");
164 check(/content: buy milk/.test(await q(`return document.querySelector('#omni .or').textContent`)), "the row shows what will be saved before Enter");
165 await key("Enter");
166 await waitFor(async () => (await toasts()).some((t) => /Saved note: buy milk \(private\)/.test(t)), "the saved note's toast");
167 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()));
168 check(!(await chatOpen()), "a save does not open the chat");
169 const pageUrl = `${base}/notes/n1/`;
170 await q(`[...document.querySelectorAll('#toast .note a')].find((a) => a.title === ${JSON.stringify(pageUrl)}).click(); return true`);
171 await waitFor(async () => (await active()).url === pageUrl, "the note's page to open");
172 check(true, "Open on the toast opens the note's own page on the service, in a new tab");
173
174 await type("/lookup marmalade");
175 await waitFor(async () => (await rows())[0] === "cmd:/lookup marmalade", "the lookup row");
176 await key("Enter");
177 await waitFor(chatOpen, "the chat to open for an answer to read");
178 const c1 = await waitFor(async () => (await cards()).length >= 2 && await cards(), "the commands in the chat");
179 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]));
180 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]));
181 check(!(await q(`return !!document.querySelector('#chatLog .cmd script, #chatLog .cmd img')`)), "an answer is shown as text, whatever it says");
182 await cdp.ev(`setChat(false)`);
183
184 await type("/list_links");
185 await waitFor(async () => (await rows())[0] === "cmd:/list_links", "the list row");
186 await key("Enter");
187 await waitFor(async () => (await cards()).length >= 3, "the list in the chat");
188 const c2 = (await cards())[2];
189 // The stand-in holds l1 (its page on the service) and l2 (its page elsewhere), plus whatever earlier suites saved there
190 // when the suites share one demo server (one at a time, as in CI).
191 const found = Number((/(\d+) found/.exec(c2.s) || [])[1]);
192 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 + "/")),
193 "a list shows each thing found, linked to its page only when that page is on the service's own site", JSON.stringify(c2));
194 await cdp.ev(`setChat(false)`);
195
196 await type("/wipe");
197 await waitFor(async () => (await rows())[0] === "cmd:/wipe", "the wipe row");
198 await key("Enter");
199 await waitFor(async () => (await rows()).some((r) => /Press Enter again/.test(r)), "the question");
200 check((await notes()).wiped === 0, "a command the service marks as destructive asks first, and does nothing yet", JSON.stringify(await rows()));
201 await key("Enter");
202 await waitFor(async () => (await notes()).wiped === 1, "the confirmed wipe");
203 check(true, "a second Enter runs it");
204
205 await type("/save hello");
206 await waitFor(async () => (await rows()).length, "the save row");
207 await key("Enter");
208 await sleep(300);
209 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`));
210 await type("/zzz");
211 await waitFor(async () => /no command/.test((await rows()).join()), "the unknown command note");
212 check(true, "an unknown command says so");
213 check(!(await asked()).some((x) => x.startsWith("/")), "commands are never sent for search suggestions");
214 await key("Escape");
215}