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
cards.mjs · 6.4 KB · 72 lines
| 1 | // Card view, as a plugin: a view rule installed from gitstable (the stand-in, DBB_GITSTABLE) adds a button to the |
| 2 | // status bar on its sites; the button reads the timeline into dense cards in a tab of the browser's own. Nothing is |
| 3 | // built in: with no view-rule plugin there is no button and no card view. The stand-in timeline is in X's older |
| 4 | // layout (data-testid on every part) and loads more as its end comes into view; the plugin is X's own rule |
| 5 | // (askrobots/x-cards) pointed at it. |
| 6 | export default async function (ctx) { |
| 7 | const { call: T, check, base, sleep } = ctx; |
| 8 | const cdp = await ctx.chrome(); |
| 9 | const q = (js) => cdp.ev(`(() => { ${js} })()`); |
| 10 | async function waitFor(fn, what, tries = 80) { |
| 11 | for (let i = 0; i < tries; i++) { const v = await fn(); if (v) return v; await sleep(150); } |
| 12 | throw new Error(`timed out waiting for ${what}`); |
| 13 | } |
| 14 | const fails = (p) => p.then(() => null, (e) => e); |
| 15 | const ev = async (js) => (await T("page.eval", { js })).value; |
| 16 | // The page carries one masonry layout per column count; the window's width shows one. Cards in the one shown: |
| 17 | const VIS = "[...document.querySelectorAll('main .m')].filter((m) => getComputedStyle(m).display !== 'none').flatMap((m) => [...m.querySelectorAll('.card')])"; |
| 18 | const viewBtns = () => q(`return [...document.querySelectorAll('#viewBtns .viewBtn')].map((b) => b.dataset.plugin + '|' + b.textContent.trim() + '|' + (b.querySelector('svg') ? 'icon' : 'no-icon'))`); |
| 19 | |
| 20 | // ---- nothing built in ---- |
| 21 | await T("page.goto", { url: base + "/timeline" }); |
| 22 | const src = (await T("tabs.list")).find((t) => t.active); |
| 23 | check((await viewBtns()).length === 0, "with no view-rule plugin installed there is no card view button"); |
| 24 | const none = await T("cards.open", {}); |
| 25 | check(!none.ok && none.reason === "no-rule", "and no card view: it says to find a plugin", none.message); |
| 26 | check((await T("cards.for", {})).views.length === 0, "and none is offered for this site"); |
| 27 | |
| 28 | // ---- install the plugin ---- |
| 29 | const pv = await T("plugins.preview", { source: "demo/cards" }); |
| 30 | check(/adds a Cards button to the status bar on 127\.0\.0\.1/.test(pv.says) && pv.powers.join() === "read-posts", "looking it up says it adds a button, reads posts and can't run code or send anything", pv.says); |
| 31 | const sneaky = await fails(T("plugins.preview", { source: "demo/cards-sneaky" })); |
| 32 | check(sneaky?.code === "not-view-only", "a view rule asking for a page's markup is refused", sneaky?.message); |
| 33 | await T("plugins.install", { token: pv.token }); |
| 34 | const btn = await waitFor(async () => { const b = await viewBtns(); return b.length ? b : null; }, "the plugin's button"); |
| 35 | check(btn.join() === "cards|Cards|icon", "installed, it puts its button in the status bar on its sites, with the icon it named", btn.join()); |
| 36 | check((await T("cards.for", {})).views.map((v) => v.id).join() === "cards", "and the card view is offered for this site"); |
| 37 | |
| 38 | // ---- the button ---- |
| 39 | await q(`document.querySelector('#viewBtns .viewBtn').click()`); |
| 40 | const tab = await waitFor(async () => { const t = (await T("tabs.list")).find((t) => t.active); return t && /^dbb-cards:\/\//.test(t.url ?? "") ? t : null; }, "the card tab", 200); |
| 41 | check(tab.id !== src.id, "clicking it reads the timeline and opens the cards in a new tab of their own", tab.url); |
| 42 | const shown = await ev(`${VIS}.length`); |
| 43 | check(shown >= 30, "it scrolled the timeline and gathered more posts than one screen holds", `${shown} cards`); |
| 44 | check((await ev(`${VIS}[0].querySelector('.who b').textContent`)) === "Person 1" && (await ev(`${VIS}[0].querySelector('.who span').textContent`)).startsWith("@person1"), "a card shows the author and their handle"); |
| 45 | check((await ev(`${VIS}[0].querySelector('.stats').textContent`)) === "1 replies · 2 reposts · 1.5K likes · 99K views", "and the replies, reposts, likes and views, shortened", await ev(`${VIS}[0].querySelector('.stats').textContent`)); |
| 46 | check((await ev(`${VIS}[0].querySelector('.text').textContent`)) === "Post number 1 <b>not bold</b>" && (await ev("document.querySelectorAll('.text b, .text script, .text img').length")) === 0, "the page's text is shown as text, never as markup"); |
| 47 | check((await ev("document.querySelectorAll('script').length")) === 0, "the card page has no scripts"); |
| 48 | check(/1 promoted post hidden/.test(await ev("document.querySelector('details summary').textContent")) && /Person 3/.test(await ev("document.querySelector('details .card').textContent")), "the post marked as an ad is set aside, behind a line that says so"); |
| 49 | check((await ev(`${VIS}.some((c) => /Post number 5\\b/.test(c.textContent))`)) === true, "and a video post is not mistaken for an ad"); |
| 50 | const link = await ev(`${VIS}[0].querySelector('a.when').href`); |
| 51 | check(link === base + "/person1/status/1001", "each card links to the post itself", link); |
| 52 | check((await viewBtns()).length === 0, "on the card tab itself there is no card view button"); |
| 53 | |
| 54 | // ---- More posts: read on from the same tab, shown while it scrolls ---- |
| 55 | await T("page.click", { selector: "header a[href$='/more']" }); |
| 56 | const after = await waitFor(async () => { const n = await ev(`${VIS}.length`).catch(() => 0); return n > shown ? n : null; }, "more cards"); |
| 57 | check(after > shown, "More posts reads further down the same timeline", `${shown} → ${after}`); |
| 58 | check((await ev("location.hash")) === "#new" && /more · read at/.test(await ev("document.getElementById('new').textContent")), "and goes to the new batch, under a line saying how many more"); |
| 59 | |
| 60 | // ---- switched off and removed ---- |
| 61 | await T("tabs.activate", { tab: src.id }); |
| 62 | await T("plugins.enable", { id: "cards", enabled: false }); |
| 63 | check(await waitFor(async () => (await viewBtns()).length === 0, "the button to go").then(() => true, () => false), "switching the plugin off takes its button away"); |
| 64 | await T("plugins.enable", { id: "cards", enabled: true }); |
| 65 | await waitFor(async () => (await viewBtns()).length === 1, "the button back"); |
| 66 | await T("plugins.remove", { id: "cards" }); |
| 67 | check(await waitFor(async () => (await viewBtns()).length === 0, "the button to go").then(() => true, () => false), "removing it takes the button away"); |
| 68 | const gone = await T("cards.open", {}); |
| 69 | check(!gone.ok && gone.reason === "no-rule", "and the card view with it"); |
| 70 | const bad = await T("cards.open", { rule: { v: 1, id: "x", name: "X", sites: ["127.0.0.1"], post: "article", fields: { text: { sel: "div", take: "innerHTML" } } } }); |
| 71 | check(!bad.ok && /refused/.test(bad.message), "a view rule passed directly is checked the same way", bad.message); |
| 72 | } |