// Reads standard.site link-tag hints from the page head and reports them to // the background worker. Built as an IIFE, so the only import is the shared // extension-API namespace. import { ext } from './lib/platform' import { profileHandleFromUrl } from './lib/profile-url' // No exports — this only marks the file as a module, so src/content.test.ts // can import it and so the declaration below stays out of the global scope. export {} // The Navigation API is not in TypeScript's DOM lib yet (5.9 ships only // NavigationHistoryEntry), so declare the sliver we use. Access is guarded by // `typeof`, which is safe for an identifier the browser does not define. declare const navigation: EventTarget | undefined function hint(rel: string): string | undefined { const el = document.querySelector(`link[rel="${rel}"]`) const href = el?.getAttribute('href')?.trim() return href && href.startsWith('at://') ? href : undefined } // After the extension reloads, orphaned copies of this script keep running in // open tabs but their runtime binding is dead: runtime.id is undefined and // sendMessage throws synchronously ("Extension context invalidated"). function orphaned(): boolean { return !ext.runtime?.id } function readHints(): { pubHint?: string; docHint?: string; profileHint?: string } { return { pubHint: hint('site.standard.publication'), docHint: hint('site.standard.document'), // bsky.app serves no standard.site well-known of its own; a profile URL is // the only thing that says whose publications to look for. profileHint: profileHandleFromUrl(location.href), } } // Detection is keyed on the page URL and the two hints; re-sending an // unchanged triple would only make the worker redo the same lookups. Chrome // fires a navigatesuccess for the initial page load too — measured, it lands // just after the load event — so without this every page would report twice. let lastReported: string | undefined function report() { if (orphaned()) return const hints = readHints() const key = `${location.href}\n${hints.pubHint ?? ''}\n${hints.docHint ?? ''}\n${hints.profileHint ?? ''}` if (key === lastReported) return lastReported = key ext.runtime.sendMessage({ type: 'page-hints', ...hints }).catch(() => { // background worker not ready; it will probe on demand instead }) } report() // The background worker asks for hints again on manual refresh. ext.runtime.onMessage.addListener((msg, _sender, sendResponse) => { if (msg?.type === 'read-hints') { sendResponse(readHints()) } }) // SPA navigations swap the URL and often the head tags without a page load. // // The Navigation API reports each navigation it sees once — measured on // bsky.app (headless Chrome 149, ~20s of scrolling and one in-app link click), // the mutation watch below ran 66 times over 448 mutation records to notice // one URL change, while this listener ran twice. `navigatesuccess` fires once // the navigation has committed, so location.href is already the new one. // Routers often swap the head tags a tick later, so read again after the page // settles; report() drops that second read unless something actually changed. // // It does not see history.pushState, though, which is how bsky.app's router // changes routes. The profile hint comes from location.href, so a pushState // navigation from one profile to another would otherwise go unnoticed. A URL // comparison on mutation callbacks — plus the two history events that name a // URL change — is the backstop. It reads no DOM, just location.href once per // mutation batch, and only calls report() when the URL is different. const SETTLE_MS = 400 let settleTimer: ReturnType | undefined if (typeof navigation !== 'undefined') { const onNavigated = () => { if (orphaned()) { navigation.removeEventListener('navigatesuccess', onNavigated) clearTimeout(settleTimer) return } console.debug('[substandard] spa navigation', location.href) report() clearTimeout(settleTimer) settleTimer = setTimeout(report, SETTLE_MS) } navigation.addEventListener('navigatesuccess', onNavigated) } let lastUrl = location.href let urlObserver: MutationObserver const checkUrl = () => { if (orphaned()) { urlObserver.disconnect() window.removeEventListener('popstate', checkUrl) window.removeEventListener('hashchange', checkUrl) return } if (location.href === lastUrl) return lastUrl = location.href report() } urlObserver = new MutationObserver(checkUrl) urlObserver.observe(document.documentElement, { childList: true, subtree: true }) window.addEventListener('popstate', checkUrl) window.addEventListener('hashchange', checkUrl)