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
extensions.mjs · 2.9 KB · 41 lines
| 1 | // Local extensions: extra automation methods from a file named in DBB_EXTENSIONS (see src/extensions.ts). |
| 2 | export default async function (ctx) { |
| 3 | const { call: T, check, base } = ctx; |
| 4 | const rejects = (p) => p.then(() => null, (e) => e); |
| 5 | |
| 6 | const st = await T("status"); |
| 7 | check(Array.isArray(st.extensions) && st.extensions.length === 1 && st.extensions[0] === "ext-echo.mjs", "status lists the extension by file name only (no folder layout)", JSON.stringify(st.extensions)); |
| 8 | |
| 9 | await T("page.goto", { url: base + "/free" }); |
| 10 | const r = await T("ext.echo", { say: "hello" }); |
| 11 | check(r.said === "hello" && r.url === base + "/free", "an extension method runs with its parameters and can reach the page of the active tab", JSON.stringify(r)); |
| 12 | |
| 13 | const t2 = await T("tabs.new", { url: base + "/headers", activate: false }); |
| 14 | const r2 = await T("ext.echo", { tab: t2.id ?? (await T("tabs.list")).at(-1).id }); |
| 15 | check(/\/headers$/.test(r2.url), "…and of the tab it names", r2.url); |
| 16 | |
| 17 | check((await rejects(T("ext.boom")))?.code === "extension-error", "an extension that throws is an API error, not a crash"); |
| 18 | check((await rejects(T("ext.nothing")))?.code === "no-method", "an unknown extension method is 'no-method'"); |
| 19 | check((await rejects(T("echo")))?.code === "no-method", "an extension method is only reachable as ext.<name>"); |
| 20 | |
| 21 | // What extensions are allowed to rely on, pinned here so a change to the browser cannot break them silently. |
| 22 | const host = await T("ext.host"); |
| 23 | check(host.found && host.type === "webview" && host.contentSize[0] > 0 && host.contentSize[1] > 0, "the page's host window is reachable from its web contents, and the page is a webview", JSON.stringify(host)); |
| 24 | const paint = await T("ext.paintable"); |
| 25 | check(!paint.empty && paint.width > 100 && paint.height > 100, "the active page can be painted to an image", `${paint.width}x${paint.height}`); |
| 26 | |
| 27 | // Asking for a page never raises, focuses, moves or resizes the window, including when the tab has to be made the visible one. |
| 28 | const w0 = await T("ext.window"); |
| 29 | await T("ext.echo", {}); |
| 30 | const other = (await T("tabs.list")).find((t) => !t.active); |
| 31 | if (other) await T("ext.echo", { tab: other.id }); |
| 32 | const w1 = await T("ext.window"); |
| 33 | check(JSON.stringify(w1) === JSON.stringify(w0), "asking for a page (even another tab) does not raise, focus, move or resize the window", JSON.stringify({ before: w0, after: w1 })); |
| 34 | |
| 35 | // In background mode (E2E_BACKGROUND=1) the window is off-screen and never takes focus, however much automation runs. |
| 36 | if (process.env.E2E_BACKGROUND === "1") { |
| 37 | await T("page.click", { selector: "h1" }); // real input, which used to need the window raised |
| 38 | const w = await T("ext.window"); |
| 39 | check(w.focused === false && w.position[0] < -1000, "background mode: the window is off-screen and not focused after real input", JSON.stringify({ focused: w.focused, position: w.position })); |
| 40 | } |
| 41 | } |