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
menu.mjs · 8.7 KB · 107 lines
1// The right-click menu, and ⌘-click / middle-click opening a link behind the page. Menus are recorded, not shown
2// (DBB_RECORD_MENUS), so nothing appears on anyone's screen; the automation API reads the menu and picks an item, which
3// runs exactly what a click on that item runs. Nothing here touches the clipboard or a real site.
4export default async function (ctx) {
5 const { call: T, check, base, sleep } = ctx;
6 const c = await ctx.chrome();
7 async function waitFor(fn, what, tries = 60) {
8 for (let i = 0; i < tries; i++) { const v = await fn().catch(() => null); if (v) return v; await sleep(100); }
9 throw new Error("timed out waiting for " + what);
10 }
11 const active = async () => (await T("tabs.list")).find((t) => t.active);
12 const mod = process.platform === "darwin" ? "meta" : "control";
13
14 const page = await T("tabs.new", { url: base + "/free" });
15 await T("tabs.activate", { tab: page.id });
16 await waitFor(async () => (await active()).id === page.id && !(await active()).loading, "the page");
17 await T("page.eval", { js: `(() => {
18 const at = (el, x, y) => { el.style.cssText = "position:fixed;left:" + x + "px;top:" + y + "px;margin:0;font:16px sans-serif"; document.body.appendChild(el); return el; };
19 const a = at(document.createElement("a"), 20, 20); a.id = "l"; a.href = "/headers?from=menu"; a.textContent = "a link to open";
20 const p = at(document.createElement("p"), 20, 80); p.id = "s"; p.textContent = "marmalade oranges and sugar";
21 const i = at(document.createElement("input"), 20, 140); i.id = "i"; i.value = "typed words";
22 const box = at(document.createElement("div"), 20, 200); box.style.width = "300px"; box.style.height = "120px"; box.style.background = "#fff";
23 window.__marker = "before";
24 return true;
25 })()` });
26
27 // A mouse event on the page itself, at (x, y) in the page.
28 const mouse = (x, y, button, modifiers = []) => c.ev(`(() => {
29 const o = { x: ${x}, y: ${y}, button: "${button}", clickCount: 1, modifiers: ${JSON.stringify(modifiers)} };
30 active.wv.sendInputEvent({ type: "mouseMove", x: o.x, y: o.y }); // as a real pointer would: it is over the element first
31 active.wv.sendInputEvent({ ...o, type: "mouseDown" }); active.wv.sendInputEvent({ ...o, type: "mouseUp" }); return true;
32 })()`);
33 let since = (await T("x402.events", { since: 0 })).next;
34 async function rightClick(x, y) {
35 await mouse(x, y, "right");
36 const ev = await waitFor(async () => {
37 const r = await T("x402.events", { since });
38 const e = r.events.find((x) => x.kind === "context-menu");
39 if (e) since = r.next;
40 return e;
41 }, "a right-click menu");
42 return ev.items;
43 }
44
45 // on a link
46 let items = await rightClick(40, 28);
47 check(items.join(",") === "open-link-tab,copy-link,inspect", "right-click on a link offers Open Link in New Tab and Copy Link Address", items.join(","));
48 let before = (await T("tabs.list")).length;
49 check((await T("menu.pick", { id: "open-link-tab" })).ok, "picking Open Link in New Tab runs it");
50 const opened = await waitFor(async () => { const l = await T("tabs.list"); return l.length === before + 1 ? l.find((t) => /\/headers\?from=menu/.test(t.url)) || null : null; }, "the new tab").catch(() => null);
51 check(!!opened, "it opens the link in a new tab");
52 check((await active()).id === page.id, "behind the page you are on (you stay where you were)");
53 check(!(await T("menu.pick", { id: "paste" })).ok, "an item that was not in the menu cannot be picked");
54
55 // on selected text
56 await T("page.eval", { js: `(() => { const r = document.createRange(); r.selectNodeContents(document.getElementById("s")); getSelection().removeAllRanges(); getSelection().addRange(r); return true; })()` });
57 items = await rightClick(60, 90);
58 check(items.join(",") === "copy,search-selection,ask-selection,inspect", "right-click on selected text offers Copy, Search askrobots and Ask the Assistant", items.join(","));
59 const label = (await T("menu.last")).menu.items.find((i) => i.id === "search-selection")?.label;
60 check(/^Search askrobots for “marmalade oranges and s…”$/.test(label || ""), "the search item names the text, clipped", label);
61 await T("menu.pick", { id: "ask-selection" });
62 const chat = await waitFor(async () => c.ev(`document.getElementById("chatIn").value`), "the chat box").catch(() => "");
63 check(/marmalade oranges and sugar/.test(chat), "Ask the Assistant puts the text in the chat box for you to finish", JSON.stringify(chat));
64 check(!(await c.ev(`[...document.querySelectorAll("#chatLog .m.u")].some((m) => /marmalade/.test(m.textContent))`)), "and sends nothing by itself");
65 await c.ev(`(document.getElementById("chatIn").value = "", setChat(false), true)`);
66
67 // in a text field
68 items = await rightClick(40, 150);
69 check(items.includes("select-all") && !items.includes("reload"), "right-click in a text field offers the edit items", items.join(","));
70 const full = (await T("menu.last")).menu.items.filter((i) => i.id).map((i) => i.id);
71 check(["undo", "redo", "cut", "copy", "paste", "select-all"].every((i) => full.includes(i)), "all six are in the menu (greyed out when they can't be used)", full.join(","));
72 await T("menu.pick", { id: "select-all" });
73 const sel = await waitFor(async () => { const v = (await T("page.eval", { js: `(() => { const i = document.getElementById("i"); return i.selectionStart === 0 && i.selectionEnd === i.value.length && document.activeElement === i })()` })).value; return v || null; }, "the field's text selected").catch(() => false);
74 check(sel === true, "Select All selects the field's text");
75
76 // anywhere else
77 await T("page.eval", { js: "getSelection().removeAllRanges(), document.activeElement && document.activeElement.blur(), true" });
78 items = await rightClick(150, 260);
79 check(/^(back,)?(forward,)?reload,print,save-pdf,inspect$/.test(items.join(",")), "right-click on the page offers Back, Forward and Reload, then Print and Save as PDF", items.join(","));
80 await T("menu.pick", { id: "reload" });
81 const reloaded = await waitFor(async () => { const v = await T("page.eval", { js: "window.__marker || 'gone'" }).catch(() => null); return v && v.value === "gone" ? true : null; }, "the reload").catch(() => false);
82 check(reloaded === true, "Reload reloads the page");
83
84 // ⌘-click (Ctrl+click off a Mac) and middle-click open the link behind the page
85 await waitFor(async () => !(await active()).loading, "the reloaded page");
86 await T("page.eval", { js: `(() => { const a = document.createElement("a"); a.href = "/headers?from=click"; a.textContent = "click me"; a.style.cssText = "position:fixed;left:20px;top:20px;font:16px sans-serif"; document.body.appendChild(a); return true; })()` });
87 before = (await T("tabs.list")).length;
88 await mouse(35, 28, "left", [mod]);
89 const viaMod = await waitFor(async () => { const l = await T("tabs.list"); return l.length === before + 1 ? l : null; }, "a tab from the modified click").catch(() => null);
90 check(!!viaMod && (await active()).id === page.id, `${mod === "meta" ? "⌘" : "Ctrl+"}click on a link opens it in a new tab behind the page`, JSON.stringify(viaMod && viaMod.map((t) => t.url)));
91 before = (await T("tabs.list")).length;
92 await mouse(35, 28, "middle");
93 const viaMiddle = await waitFor(async () => { const l = await T("tabs.list"); return l.length === before + 1 ? l : null; }, "a tab from the middle click").catch(() => null);
94 check(!!viaMiddle && (await active()).id === page.id, "middle-click on a link does the same");
95
96 // the browser's own fields: the address bar gets the text items only
97 const own = await c.ev(`(() => { const u = document.getElementById("url"); u.focus(); u.select(); const r = u.getBoundingClientRect(); return [Math.round(r.left + 20), Math.round(r.top + r.height / 2)]; })()`);
98 await c.send("Input.dispatchMouseEvent", { type: "mousePressed", x: own[0], y: own[1], button: "right", clickCount: 1 });
99 await c.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: own[0], y: own[1], button: "right", clickCount: 1 });
100 const ownItems = await waitFor(async () => { const r = await T("x402.events", { since }); const e = r.events.find((x) => x.kind === "context-menu"); if (e) since = r.next; return e && e.items; }, "the address bar's menu").catch(() => null);
101 // Paste is greyed out (so not in the recorded list) when the clipboard is empty, as on a fresh CI machine: read the whole menu.
102 const ownAll = ownItems ? (await T("menu.last")).menu.items.filter((i) => i.id).map((i) => i.id) : [];
103 check(ownAll.join(",") === "undo,redo,cut,copy,paste,select-all", "right-click in the address bar offers the text items (Undo, Redo, Cut, Copy, Paste, Select All) and nothing else", ownAll.join(","));
104
105 for (const t of await T("tabs.list")) if (/from=(menu|click)/.test(t.url)) await T("tabs.close", { tab: t.id }).catch(() => {});
106 await T("tabs.close", { tab: page.id }).catch(() => {});
107}