Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404// /find's renderer. Matching lives in panoptiblog-lookup.js; this file only// loads the dataset and draws results. Records are third-party data, so every// value reaches the DOM through textContent, and a link is only a link when// its URL parses as http or https.
import { DISPLAYED_TOP, displayHandle, explainExclusion, isOnLeaderboard, isRanked, languageName, leaderboardHref, rankChange, search, shareHref, sharePath,} from "./panoptiblog-lookup.js";import { LOOKUP_URL, LookupError, loadLookup } from "./panoptiblog-lookup-data.js";import { currentViewer } from "./panoptiblog-viewer.js";
const FEEDBACK_URL = "https://tangled.org/permadeath.com/substandard/issues";const OPT_OUT_URL = "/#included";const QUERY_PARAM = "q";
const exact = new Intl.NumberFormat("en");const day = new Intl.DateTimeFormat("en", { dateStyle: "medium", timeZone: "UTC" });const list = new Intl.ListFormat("en", { type: "conjunction" });
const dialog = document.getElementById("panoptiblog-find");const openButton = document.getElementById("panoptiblog-find-open");const closeButton = document.getElementById("panoptiblog-find-close");const form = document.getElementById("find-form");const input = document.getElementById("find-query");const status = document.getElementById("find-status");const results = document.getElementById("find-results");let opener;
let lookup;let pending;
function text(value) { return value === undefined || value === null ? "" : String(value);}
function make(tag, className, content) { const node = document.createElement(tag); if (className) node.className = className; if (content !== undefined) node.textContent = content; return node;}
function safeHref(value) { try { const url = new URL(text(value)); return url.protocol === "https:" || url.protocol === "http:" ? url.href : undefined; } catch { return undefined; }}
function link(href, label, external = false) { const anchor = make("a", undefined, label); anchor.href = href; if (external) anchor.rel = "noopener noreferrer"; return anchor;}
function dateOf(value) { const time = Date.parse(text(value)); return Number.isFinite(time) ? day.format(time) : undefined;}
function stat(label, value) { const item = make("div"); item.append(make("dt", undefined, label), make("dd", undefined, value)); return item;}
function changeText(pub, previousDate) { const change = rankChange(pub); const since = previousDate ? ` since ${previousDate}` : ""; if (change === undefined) return previousDate ? `new${since}` : undefined; if (change === 0) return `no change${since}`; const arrow = change > 0 ? "▲" : "▼"; return `${arrow} ${exact.format(Math.abs(change))}${since}`;}
function rankedBody(pub, data) { const nodes = []; const total = Number(data.totals?.rankedPublications); const stats = make("dl", "stats find-stats"); stats.append( stat("rank", Number.isFinite(total) && total > 0 ? `#${exact.format(pub.rank)} of ${exact.format(total)}` : `#${exact.format(pub.rank)}`), ); if (Number.isFinite(pub.percentile)) stats.append(stat("percentile", pub.percentile.toFixed(1))); if (Number.isFinite(pub.score)) stats.append(stat("score", pub.score.toFixed(2))); const change = changeText(pub, dateOf(data.previousGeneratedAt)); if (change) stats.append(stat("previous run", Number.isFinite(pub.previousRank) ? `#${exact.format(pub.previousRank)} · ${change}` : change)); nodes.push(stats); if (pub.stale) { nodes.push(make( "p", "find-next find-stale", "Not refreshed in the latest crawl; figures are from an earlier run.", )); }
const line = make("p", "find-next"); if (isOnLeaderboard(pub)) { line.append(link(leaderboardHref(pub), "view details")); } else { line.append(`Ranked, but outside the top ${DISPLAYED_TOP} shown on the leaderboard.`); } // Every ranked publication has a score page, inside the top 100 or not. const scorePage = sharePath(pub.uri); if (scorePage) line.append(" · ", link(scorePage, "score page")); nodes.push(line); return nodes;}
function excludedBody(pub) { const explained = explainExclusion(pub); const nodes = [ make("p", "find-filtered", "We found this publication in the crawl, but it was filtered out of the ranking."), make("p", "find-reason", explained.reason), ]; if (explained.reasons?.length) { const list = make("ul", "find-reasons"); for (const reason of explained.reasons) list.append(make("li", undefined, reason)); nodes.push(list); } const fix = make("p", "find-fix"); fix.append(make("strong", undefined, "to change it: "), explained.fix); nodes.push(fix);
const stats = make("dl", "stats find-stats"); if (Number.isFinite(pub.docCount)) stats.append(stat("posts", exact.format(pub.docCount))); if (Number.isFinite(pub.wordCount)) stats.append(stat("words", exact.format(pub.wordCount))); const language = languageName(pub.language); if (language) stats.append(stat("language", language)); if (pub.discoverable === false || pub.exclusion === "undiscoverable") { stats.append(stat("discoverable", pub.discoverable === false ? "no" : "yes")); } nodes.push(stats); return { label: explained.label, nodes };}
function resultCard(pub, data) { const card = make("li", "find-result"); const head = make("div", "find-head"); const title = make("h3", "find-name"); const handle = displayHandle(pub.handle); const name = text(pub.name) || handle || "untitled publication"; const href = safeHref(pub.url); title.append(href ? link(href, name, true) : name);
const meta = make("p", "find-meta"); if (handle) meta.append(make("span", undefined, `@${handle}`)); if (href) meta.append(make("span", undefined, new URL(href).host));
let body; let label; if (isRanked(pub)) { label = `#${exact.format(pub.rank)}`; body = rankedBody(pub, data); } else { ({ label, nodes: body } = excludedBody(pub)); } const badge = make("span", isRanked(pub) ? "find-badge find-badge-ranked" : "find-badge", label); head.append(title, badge); if (isRanked(pub)) { const compare = make("button", "find-compare", "compare"); compare.type = "button"; compare.setAttribute("aria-label", `Compare ${name}`); compare.addEventListener("click", () => { document.dispatchEvent(new CustomEvent("panoptiblog:compare", { detail: { uri: text(pub.uri) }, })); }); body.push(compare); } const shareHrefValue = shareHref(pub, data.totals?.rankedPublications); if (shareHrefValue) { const share = link(shareHrefValue, "post to bsky", true); share.className = "find-share"; share.setAttribute("aria-label", "Post this result to Bluesky"); body.push(share); } card.append(head, meta, ...body); return card;}
/** The latest crawl's repository counts, worded like the what's included chart. */function crawlSummary(data) { const crawl = data.totals?.crawl ?? {}; const read = Number(crawl.readRepositories); const all = Number(crawl.repositories); if (!Number.isFinite(read) || !Number.isFinite(all)) return undefined; const sentence = crawl.mode === "incremental" ? `That crawl updated ${exact.format(Number(crawl.changedRepositories ?? 0))} changed repositories; ${exact.format(read)} of ${exact.format(all)} are indexed.` : `That crawl read ${exact.format(read)} of ${exact.format(all)} repositories.`; const extra = []; if (Number(crawl.skipped) > 0) extra.push(`skipped ${exact.format(crawl.skipped)}`); if (Number(crawl.failed) > 0) extra.push(`could not read ${exact.format(crawl.failed)}`); // A truncated listing stopped at the crawl's page limit; that repository // still counts as read. if (Number(crawl.truncated) > 0) extra.push(`read ${exact.format(crawl.truncated)} only in part`); return extra.length ? `${sentence} It ${list.format(extra)}.` : sentence;}
function noMatch(query, data) { const panel = make("div", "find-empty"); const when = dateOf(data.generatedAt); panel.append(make( "p", "find-empty-title", `Nothing in the ${when ? `${when} ` : ""}crawl matches “${query}”.`, )); panel.append(make( "p", undefined, "Every crawled publication is listed here, ranked or not, so this one was not in the latest crawl.", )); const summary = crawlSummary(data); if (summary) panel.append(make("p", undefined, summary));
const checklist = make("ul", "find-checklist"); for (const item of [ "Try another way in: your handle, DID, website address or the publication's at-uri.", "The publication is a site.standard.publication record in your own repository.", "Posts are site.standard.document records whose site field points at that publication.", "Your PDS answered when the crawl ran. The crawl runs once a day, so a new publication shows up after the next one.", ]) { checklist.append(make("li", undefined, item)); } panel.append(checklist);
const actions = make("p", "find-actions"); actions.append( link(FEEDBACK_URL, "send feedback", true), make("span", undefined, " · "), link(OPT_OUT_URL, "how to opt out"), ); panel.append(actions); return panel;}
function failure(error) { const panel = make("div", "pano-failure"); panel.append(make( "p", "pano-failure-message", error instanceof LookupError && error.missing ? "the lookup data has not been published yet" : "the lookup data is unavailable right now", )); const actions = make("div", "pano-failure-actions"); const retry = make("button", "pano-retry", "try again"); retry.type = "button"; retry.addEventListener("click", () => run(input.value)); actions.append(retry, link(LOOKUP_URL, "view the dataset"), link(FEEDBACK_URL, "send feedback", true)); panel.append(actions); return panel;}
function render(query, data) { const found = search(data.publications, query); if (found.total === 0) { status.textContent = "no matches"; results.replaceChildren(noMatch(query.trim(), data)); return; } const shown = found.results.length; status.textContent = shown < found.total ? `showing ${exact.format(shown)} of ${exact.format(found.total)} matches; add more of the name, or search by handle or website` : `${exact.format(found.total)} ${found.total === 1 ? "match" : "matches"}`; const list = make("ol", "find-list"); for (const pub of found.results) list.append(resultCard(pub, data)); const when = dateOf(data.generatedAt); const note = make("p", "panoptiblog-note", when ? `From the crawl of ${when}.` : ""); results.replaceChildren(list, note);}
function remember(query) { const url = new URL(location.href); if (query) url.searchParams.set(QUERY_PARAM, query); else url.searchParams.delete(QUERY_PARAM); history.replaceState(history.state, "", url.href);}
let sequence = 0;
async function run(value) { const query = text(value).trim(); remember(query); const mine = ++sequence; if (!query) { status.textContent = ""; results.replaceChildren(); return; } if (!lookup) status.textContent = "loading the latest crawl…"; try { const data = await loadLookup(); if (mine === sequence) render(query, data); } catch (error) { console.error("[panoptiblog] could not load the lookup dataset", error); if (mine !== sequence) return; status.textContent = ""; results.replaceChildren(failure(error)); }}
function openFind() { if (!dialog || dialog.open) return; opener = document.activeElement; dialog.showModal(); openButton?.setAttribute("aria-expanded", "true"); const me = currentViewer(); const query = text(me?.handle || me?.did); if (input && !input.value.trim() && query) { input.value = query; run(query); } input?.focus();}
function closeFind() { if (!dialog?.open) return; dialog.close();}
function setupDialog() { if (!dialog || !openButton) return; openButton.addEventListener("click", openFind); closeButton?.addEventListener("click", closeFind); dialog.addEventListener("close", () => { openButton.setAttribute("aria-expanded", "false"); if (opener?.isConnected) opener.focus(); opener = undefined; }); dialog.addEventListener("click", (event) => { if (event.target !== dialog) return; const rect = dialog.getBoundingClientRect(); if ( event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom ) { closeFind(); } }); results?.addEventListener("click", (event) => { const anchor = event.target.closest("a"); const href = anchor?.getAttribute("href") ?? ""; if (href.startsWith("/#") || href.startsWith("#")) closeFind(); });}
if (form && input && status && results) { form.addEventListener("submit", (event) => { event.preventDefault(); run(input.value); }); // Start the download as soon as the reader shows intent, so the first // search rarely waits for it. A failure here is reported by that search. input.addEventListener("focus", () => loadLookup().catch(() => {}), { once: true }); let typing; input.addEventListener("input", () => { if (!lookup) return; clearTimeout(typing); typing = setTimeout(() => run(input.value), 150); });
setupDialog(); document.addEventListener("panoptiblog:find", (event) => { const query = text(event.detail?.query).trim(); if (!query) return; input.value = query; openFind(); run(query); }); document.addEventListener("panoptiblog:viewer", (event) => { const query = text(event.detail?.handle || event.detail?.did).trim(); if (!query || !dialog?.open || input.value.trim()) return; input.value = query; run(query); }); const initial = new URL(location.href).searchParams.get(QUERY_PARAM); if (initial) { input.value = initial; openFind(); run(initial); }}