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
bookmarks.mjs · 11.8 KB · 123 lines
| 1 | // Bookmarks: the person's saved links on askrobots (a stand-in here), read when the list is opened, saved with one click on ☆, |
| 2 | // and usable by the assistant to save and close tabs. |
| 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 | const server = async () => (await fetch(base + "/o/_links")).json(); |
| 8 | async function waitFor(fn, what, tries = 100) { |
| 9 | for (let i = 0; i < tries; i++) { const v = await fn(); if (v) return v; await sleep(100); } |
| 10 | throw new Error(`timed out waiting for ${what}`); |
| 11 | } |
| 12 | const star = () => q(`const b = document.getElementById('starBtn'); return b.textContent.trim() + (b.classList.contains('saved') ? '|saved' : '') + (b.disabled ? '|disabled' : '')`); |
| 13 | |
| 14 | // before connecting: nothing to show, and the star says to connect instead of failing silently |
| 15 | const s0 = await T("bookmarks.list"); |
| 16 | check(s0.items.length === 0 && s0.source === "none" && s0.connected === false, "before connecting there are no bookmarks and nothing was asked of the service", JSON.stringify(s0)); |
| 17 | await T("page.goto", { url: base + "/forms" }); |
| 18 | try { await waitFor(async () => (await star()) === "Save", "the empty star on a web page", 30); } catch (e) { throw new Error(e.message + " — star is " + (await star()) + ", url bar " + (await q(`return document.getElementById('url').value`)) + ", errors " + (await q(`return String(window.__lastErr || '')`))); } |
| 19 | await q(`document.getElementById('starBtn').click()`); |
| 20 | await waitFor(async () => /Connect askrobots/.test(await q(`return document.getElementById('toast').textContent`)), "the 'connect first' note"); |
| 21 | check((await star()) === "Save", "clicking ☆ before connecting says to connect first, and saves nothing"); |
| 22 | |
| 23 | // connect (in a tab of the browser): the bookmarks come in by themselves |
| 24 | await T("mcp.connect", { id: "askrobots", wait: true }); |
| 25 | const s1 = await waitFor(async () => { const s = await T("bookmarks.list"); return s.items.length === 2 ? s : null; }, "the bookmarks after signing in"); |
| 26 | check(s1.source === "service" && s1.connected && s1.items.map((b) => b.title).join("|") === "Headers echo|Free page", "after signing in, the bookmarks arrive from the service, newest first", JSON.stringify(s1.items.map((b) => b.title))); |
| 27 | |
| 28 | // the list |
| 29 | await cdp.ev(`setBm(true)`); |
| 30 | await waitFor(async () => q(`return document.querySelectorAll('#bmPanel .bm').length === 2`), "two rows in the list"); |
| 31 | check(await q(`return /Your links on askrobots.com/.test(document.getElementById('bmPanel').textContent)`), "the list says where the bookmarks come from and when they were updated"); |
| 32 | await q(`const i = document.getElementById('bmSearch'); i.value = 'free'; i.dispatchEvent(new Event('input'));`); |
| 33 | check(await q(`const r = document.querySelectorAll('#bmPanel .bm'); return r.length === 1 && /Free page/.test(r[0].textContent)`), "searching the list narrows it"); |
| 34 | await q(`document.querySelector('#bmPanel .bm').click()`); |
| 35 | await waitFor(async () => (await T("tabs.list")).find((t) => t.active).url === base + "/free", "the bookmark to open"); |
| 36 | check(!(await q(`return document.getElementById('bmPanel').classList.contains('open')`)), "clicking a bookmark opens it in the tab and closes the list"); |
| 37 | check((await star()) === "Saved|saved", "the star is filled on a page that is already a bookmark", await star()); |
| 38 | |
| 39 | // one click saves, with no dialog |
| 40 | await T("page.goto", { url: base + "/forms" }); |
| 41 | await waitFor(async () => (await star()) === "Save", "the empty star"); |
| 42 | await q(`document.getElementById('starBtn').click()`); |
| 43 | await waitFor(async () => (await star()) === "Saved|saved", "the star to fill in"); |
| 44 | const saved = (await server())[0]; |
| 45 | check(saved.url === base + "/forms" && saved.title === "forms" && saved.project_id === "P-links" && saved.is_public === false, "one click saves the page with its title, private, in the project the server's entry names", JSON.stringify(saved)); |
| 46 | check(await q(`return !document.querySelector('dialog[open], .modal.show')`) && (await T("bookmarks.has", { url: base + "/forms" })).saved, "there is no dialog after it, and the browser knows the page is saved"); |
| 47 | await q(`document.getElementById('starBtn').click()`); // already saved: shows the list instead of saving twice |
| 48 | await waitFor(async () => q(`return document.getElementById('bmPanel').classList.contains('open')`), "the list to open"); |
| 49 | check((await server()).filter((l) => l.url === base + "/forms").length === 1, "clicking a filled star never saves the page twice (it opens the list)"); |
| 50 | await cdp.ev(`setBm(false)`); |
| 51 | |
| 52 | // a page that is not saved, on a site that has a saved page: the star says so |
| 53 | await T("page.goto", { url: base + "/hits" }); |
| 54 | await waitFor(async () => /site/.test(await q(`return document.getElementById('starBtn').className`)), "the same-site hint"); |
| 55 | const hint = await q(`const b = document.getElementById('starBtn'); return b.textContent.trim() + '|' + b.title`); |
| 56 | check(/^Save\|You have \d+ bookmarks on this site/.test(hint), "a page that is not saved, on a site with saved pages, shows that you have bookmarks there (and ☆ still saves this page)", hint); |
| 57 | |
| 58 | // a link saved somewhere else shows up the next time the list is opened |
| 59 | await fetch(base + "/o/_addlink?url=" + encodeURIComponent(base + "/headers?elsewhere=1") + "&title=" + encodeURIComponent("Saved on the website")); |
| 60 | await cdp.ev(`setBm(true)`); |
| 61 | await waitFor(async () => q(`return /Saved on the website/.test(document.getElementById('bmPanel').textContent)`), "the link saved elsewhere"); |
| 62 | check(true, "a link saved on the website shows up when the list is opened"); |
| 63 | await cdp.ev(`setBm(false)`); |
| 64 | |
| 65 | // searching looks through all the links on the service, not just the newest 100 the list holds |
| 66 | await fetch(base + "/o/_seed"); |
| 67 | await cdp.ev(`setBm(true)`); |
| 68 | await waitFor(async () => q(`return /Showing your newest 100/.test(document.getElementById('bmPanel').textContent)`), "the full newest-100 list"); |
| 69 | check(!(await T("bookmarks.list")).items.some((b) => /Ancient/.test(b.title)), "an old link is not among the newest 100 the list holds"); |
| 70 | await q(`const i = document.getElementById('bmSearch'); i.value = 'ancient'; i.dispatchEvent(new Event('input'));`); |
| 71 | await waitFor(async () => q(`return [...document.querySelectorAll('#bmPanel .bm')].some((r) => /Ancient Yahoo archive/.test(r.textContent))`), "the old link found by searching askrobots"); |
| 72 | check(true, "searching the bookmarks finds an old link by asking askrobots, beyond the newest 100"); |
| 73 | const direct = await T("bookmarks.search", { q: "ancient" }); |
| 74 | check(direct.items.length === 1 && direct.items[0].title === "Ancient Yahoo archive", "and scripts can search the same way", JSON.stringify(direct.items.map((b) => b.title))); |
| 75 | await cdp.ev(`setBm(false)`); |
| 76 | await T("page.goto", { url: base + "/headers?ancient=1&utm_source=newsletter" }); |
| 77 | await waitFor(async () => (await star()) === "Saved|saved", "the star on the old bookmark"); |
| 78 | check(true, "an old bookmark found by a search is then known: opening it shows ★ (a tracking parameter on the address does not matter)"); |
| 79 | |
| 80 | // an old bookmark nobody searched for: the browser asks about the site when you open the page, and ☆ fills in |
| 81 | await T("page.goto", { url: base.replace("127.0.0.1", "localhost") + "/free?old=103" }); |
| 82 | try { await waitFor(async () => (await star()) === "Saved|saved", "the star on an old bookmark found by checking the site", 60); } catch (e) { throw new Error(e.message + " — star " + (await star()) + ", direct check " + JSON.stringify(await T("bookmarks.check", { url: base.replace("127.0.0.1", "localhost") + "/free?old=103" })) + ", state " + JSON.stringify((({ items, known, ...r }) => ({ ...r, items: items.length, known: known.length }))(await T("bookmarks.list")))); } |
| 83 | check(true, "opening a page saved long ago (beyond the newest 100) fills ☆ after the browser checks the site with askrobots"); |
| 84 | await cdp.ev(`setBm(true)`); |
| 85 | await waitFor(async () => q(`return !!document.getElementById('bmCheck')`), "the setting in the list"); |
| 86 | check(await q(`return document.getElementById('bmCheck').checked && /sends the site's name/.test(document.getElementById('bmPanel').textContent)`), "the list shows the setting, on, and says what it sends"); |
| 87 | await q(`document.getElementById('bmCheck').click()`); |
| 88 | await waitFor(async () => (await T("bookmarks.list")).checkPages === false, "the setting to turn off"); |
| 89 | const offCheck = await T("bookmarks.check", { url: base.replace("127.0.0.1", "localhost") + "/free?old=104" }); |
| 90 | check(offCheck.checked === false, "with it off, pages are not checked", JSON.stringify(offCheck)); |
| 91 | await q(`document.getElementById('bmCheck').click()`); |
| 92 | await cdp.ev(`setBm(false)`); |
| 93 | |
| 94 | // a window that thinks it is not connected learns it is after a load, even when the list did not change |
| 95 | await cdp.ev(`bmApply(Object.assign({}, bm, { connected: false }))`); |
| 96 | await T("bookmarks.list", { refresh: true }); |
| 97 | await waitFor(async () => cdp.ev(`bm.connected === true`), "the window to learn it is connected"); |
| 98 | check(true, "after a load the window always hears it is connected, even when the list is the same as its copy"); |
| 99 | |
| 100 | // the assistant can save a tab and close it (Auto mode: both are on the safe list, so no questions) |
| 101 | const tidyTab = await T("tabs.new", { url: base + "/free?tidy=1" }); |
| 102 | const tabsBefore = (await T("tabs.list")).length; |
| 103 | await T("chat.mode", { mode: "auto" }); |
| 104 | const tidy = await T("chat.send", { text: "tidy up the tidy tab" }); |
| 105 | await T("chat.mode", { mode: "ask" }); |
| 106 | check(tidy.reply === "Result: saved and closed" && !tidy.events.some((e) => e.kind === "confirm"), "the assistant saves a tab as a bookmark and closes it, unasked in Auto mode", tidy.reply); |
| 107 | check((await server()).some((l) => l.url === base + "/free?tidy=1") && (await T("tabs.list")).length === tabsBefore - 1 && !(await T("tabs.list")).some((t) => t.id === tidyTab.id), "the bookmark is on the service and the tab is gone"); |
| 108 | |
| 109 | // keeping a page while connected also saves it as a bookmark |
| 110 | await T("tabs.new", { url: base + "/keep-me" }); |
| 111 | await sleep(300); |
| 112 | const kept = await T("keep.tab", {}); |
| 113 | check(kept.ok && kept.bookmarked === true && (await server()).some((l) => l.url === base + "/keep-me"), "keeping a page while connected also saves it as a bookmark on askrobots", JSON.stringify({ ok: kept.ok, bookmarked: kept.bookmarked })); |
| 114 | const files = await (await fetch(base + "/o/_files")).json(); |
| 115 | const png = files.find((f) => /\.png$/.test(f.filename)), txt = files.find((f) => /\.txt$/.test(f.filename)); |
| 116 | const today = new Date().toISOString().slice(0, 10); |
| 117 | check(png && png.filename === `Marmalade notes (127.0.0.1, ${today}).png` && png.head === "89504e470d0a1a0a" && png.bytes > 1000, "and its picture goes to the account's Files as a real PNG with a readable name: title, site and date", JSON.stringify(png && { filename: png.filename, bytes: png.bytes })); |
| 118 | check(txt && txt.filename === `Marmalade notes (127.0.0.1, ${today}).txt` && /^Marmalade notes\nhttp:\/\/127\.0\.0\.1:\d+\/keep-me\nKept /.test(txt.text) && /zebra keeps a secret recipe/.test(txt.text), "and its text too, headed by the title, address and time", txt && txt.text.slice(0, 80)); |
| 119 | check(files.every((f) => f.is_public === false && f.category === "browser/kept" && f.project_id === "P-links" && /^Kept from http/.test(f.description)), "both private, in the category and project the server's entry names, described with where they came from", JSON.stringify(files.map((f) => [f.category, f.project_id, f.is_public]))); |
| 120 | check(kept.record.remote && kept.record.remote.files.length === 2 && !kept.record.remote.error, "the kept copy records what was saved to the account", JSON.stringify(kept.record.remote)); |
| 121 | const offered = await T("mcp.tools"); |
| 122 | check(!offered.some((x) => /upload_file/.test(x.name)), "the assistant is never offered the upload tool (only the browser itself uses it)"); |
| 123 | } |