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
keep.mjs · 5.1 KB · 68 lines
| 1 | // Keep: one click keeps a copy of the page (what was on screen and its text) and closes the tab; kept copies are searched and |
| 2 | // opened from the browser itself. |
| 3 | export 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 = 100) { |
| 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 | |
| 12 | const t = await T("tabs.new", { url: base + "/keep-me" }); |
| 13 | await sleep(300); |
| 14 | const before = (await T("tabs.list")).length; |
| 15 | const r = await T("keep.tab", {}); |
| 16 | check(r.ok && r.record.title === "Marmalade notes" && r.record.url === base + "/keep-me" && r.record.imageBytes > 1000 && r.record.textChars > 50, "keeping a page saves a picture of it and its text, with its address and title", JSON.stringify({ ...r.record, id: undefined })); |
| 17 | check(r.closed && (await T("tabs.list")).length === before - 1 && !(await T("tabs.list")).some((x) => x.id === t.id), "and closes the tab"); |
| 18 | check(r.bookmarked === false, "with no connection it is not saved as a bookmark (and that is fine)"); |
| 19 | |
| 20 | const all = await T("keep.list", {}); |
| 21 | check(all.length === 1 && all[0].id === r.record.id, "the kept page is listed"); |
| 22 | const hit = await T("keep.list", { q: "zebra marmalade" }); |
| 23 | check(hit.length === 1 && /zebra keeps a secret recipe/.test(hit[0].snippet), "searching the text of kept pages finds it, with the words around the match", hit[0]?.snippet); |
| 24 | check((await T("keep.list", { q: "zebra pineapple" })).length === 0, "every word has to match"); |
| 25 | |
| 26 | // the copy opens in a tab, served by the browser itself: text and picture, nothing that runs |
| 27 | await T("tabs.new", { url: r.view }); |
| 28 | await sleep(500); |
| 29 | const txt = (await T("page.text")).text; |
| 30 | check(/Kept copy/.test(txt) && /zebra keeps a secret recipe/.test(txt) && /bold words & an ampersand/.test(txt), "the kept copy opens with its text (markup in the page is shown as text, not run)", txt.slice(0, 120)); |
| 31 | check((await T("page.eval", { js: "(() => { const i = document.querySelector('img'); return i && i.complete && i.naturalWidth > 100 })()" })).value === true, "and with the picture of the page"); |
| 32 | check((await T("page.eval", { js: "document.scripts.length" })).value === 0, "the copy has no scripts"); |
| 33 | |
| 34 | // a page with a sign-in form is not kept |
| 35 | await T("page.goto", { url: base + "/login" }); |
| 36 | const login = await T("keep.tab", {}); |
| 37 | check(login.ok === false && login.reason === "login" && (await T("tabs.list")).some((x) => x.url === base + "/login"), "a page with a sign-in form is not kept, and its tab stays open", login.message); |
| 38 | |
| 39 | // the toolbar button and the list in the bookmarks panel |
| 40 | await T("tabs.new", { url: base + "/keep-me?second=1" }); |
| 41 | await sleep(300); |
| 42 | await q(`document.getElementById('keepBtn').click()`); |
| 43 | await waitFor(async () => /Kept “Marmalade notes”/.test(await q(`return document.getElementById('toast').textContent`)), "the kept note"); |
| 44 | check(!(await T("tabs.list")).some((x) => /second=1/.test(x.url)), "📥 keeps the page and closes the tab, with a short note and no dialog"); |
| 45 | await cdp.ev(`setBm(true)`); |
| 46 | await q(`document.getElementById('bmView-kept').click()`); |
| 47 | await waitFor(async () => q(`return document.querySelectorAll('#bmPanel .bm').length === 2`), "two kept copies in the list"); |
| 48 | await q(`const i = document.getElementById('keptSearch'); i.value = 'seville'; i.dispatchEvent(new Event('input'));`); |
| 49 | await waitFor(async () => q(`return document.querySelectorAll('#bmPanel .bm').length === 2 && /Seville/.test(document.querySelector('#bmPanel .bm .s').textContent)`), "search in the kept list"); |
| 50 | check(true, "the bookmarks panel lists kept copies and searches their text"); |
| 51 | await q(`document.querySelector('#bmPanel .bm').click()`); |
| 52 | await waitFor(async () => /^dbb-kept:\/\//.test((await T("tabs.list")).find((x) => x.active).url), "the kept copy to open"); |
| 53 | check(true, "clicking a kept copy opens it in a new tab"); |
| 54 | |
| 55 | // the assistant can keep a tab |
| 56 | await T("tabs.new", { url: base + "/keep-me?third=1" }); |
| 57 | await T("chat.mode", { mode: "auto" }); |
| 58 | const res = await T("chat.send", { text: "pocket that tab" }); |
| 59 | await T("chat.mode", { mode: "ask" }); |
| 60 | check(res.reply === "Result: kept and closed" && !res.events.some((e) => e.kind === "confirm") && !(await T("tabs.list")).some((x) => /third=1/.test(x.url)), "the assistant keeps a tab and closes it, unasked in Auto mode", JSON.stringify({ reply: res.reply, ended: res.ended, ev: res.events.filter((e) => e.kind !== "usage").map((e) => [e.kind, e.name, e.note || e.reason || e.summary || e.message]).slice(0, 8) })); |
| 61 | check((await T("keep.list", {}))[0].reason === "recipe for later", "and records why, when it was told"); |
| 62 | |
| 63 | // off while the caller network is on |
| 64 | await T("net.provider", { enabled: true, userAgent: "E2E-Keep/1.0 (+https://example.invalid/about)" }); |
| 65 | const off = await T("keep.tab", {}); |
| 66 | await T("net.provider", { enabled: false }); |
| 67 | check(off.ok === false && off.reason === "caller-network-on", "keeping is switched off while the caller network is on"); |
| 68 | } |