Something went wrong. Try again.
View your browser history as a graph
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208/** * Ephemeral capture state, kept in chrome.storage.session. * * THE most important detail of an MV3 capture layer: the service worker is * killed between events, so we CANNOT hold "which visit is current in each tab" * in a plain JS variable — it would vanish. chrome.storage.session survives * worker restarts (and is wiped when the browser closes), which is exactly the * lifetime we want for in-flight navigation bookkeeping. * * We track three things: * - currentVisit[tabId]: the latest committed visit in that tab (id + url), * used as the parent for same-tab link navigations. We keep the url too so * the title handler can verify a title belongs to this visit before applying * it (titles arrive on a different, racy event stream — see background). * - pendingOpener[newTabId]: when a link opens a NEW tab, the source visit id, * so the new tab's first commit can attach to the link that opened it. * - pendingTitle[tabId]: a title/favicon that arrived before its visit row was * written, stashed by url so the visit can pick it up once created. */
import { stripHash } from "../model/url";import { emptyStack, type NavStack } from "../background/navEntries";
/** storage.session key for the per-tab HEAD pointer; exported so the viewer can * watch it via chrome.storage.onChanged and follow the active tab live. */export const CURRENT_KEY = "currentVisitByTab";const OPENER_KEY = "pendingOpenerByTab";const TITLE_KEY = "pendingTitleByTab";// A reopened/restored tab's request to continue an existing subtree: the node to// seed as `current` on the new tab's first commit (see hg:attach in background).const ATTACH_KEY = "pendingAttachByTab";// A reopened tab's logical group key (the original tab's tabGroup), stamped onto// the reopened tab's visits so it rejoins the old subtree's group. Only set for// reopened tabs; normal tabs have no entry and group by tabId.const TABKEY_KEY = "tabKeyByTab";// Mirror of each tab's native browser session-history stack (see navEntries),// so a forward_back event resolves to the exact entry by cursor, not a URL guess.const NAVSTACK_KEY = "navStackByTab";
export interface CurrentVisit { id: string; url: string; /** webNavigation timeStamp when this visit committed (for redirect timing). */ committedAt: number;}
export interface PendingTitle { url: string; title?: string; faviconUrl?: string;}
export interface PendingAttach { /** Visit id to seed as the new tab's `current` pointer. */ id: string; /** Its url, for the same-url skip + the current pointer. */ url: string;}
async function read<T>(key: string): Promise<Record<number, T>> { const obj = await chrome.storage.session.get(key); return (obj[key] as Record<number, T> | undefined) ?? {};}
async function write<T>(key: string, value: Record<number, T>): Promise<void> { await chrome.storage.session.set({ [key]: value });}
export async function getCurrentVisit(tabId: number): Promise<CurrentVisit | undefined> { return (await read<CurrentVisit>(CURRENT_KEY))[tabId];}
export async function setCurrentVisit(tabId: number, visit: CurrentVisit): Promise<void> { const map = await read<CurrentVisit>(CURRENT_KEY); map[tabId] = visit; await write(CURRENT_KEY, map);}
export async function clearTab(tabId: number): Promise<void> { const current = await read<CurrentVisit>(CURRENT_KEY); delete current[tabId]; await write(CURRENT_KEY, current); const titles = await read<PendingTitleMap>(TITLE_KEY); delete titles[tabId]; await write(TITLE_KEY, titles); const stacks = await read<NavStack>(NAVSTACK_KEY); delete stacks[tabId]; await write(NAVSTACK_KEY, stacks); const attaches = await read<PendingAttach>(ATTACH_KEY); delete attaches[tabId]; await write(ATTACH_KEY, attaches); const tabKeys = await read<string>(TABKEY_KEY); delete tabKeys[tabId]; await write(TABKEY_KEY, tabKeys);}
/** storage.session key for the per-tab logical group key; exported so the viewer * can watch it and re-solo a reopened tab onto its original subtree. */export { TABKEY_KEY };
/** A reopened tab's logical group key (undefined for normal tabs). */export async function getTabKey(tabId: number): Promise<string | undefined> { return (await read<string>(TABKEY_KEY))[tabId];}
export async function setTabKey(tabId: number, tabKey: string): Promise<void> { const map = await read<string>(TABKEY_KEY); map[tabId] = tabKey; await write(TABKEY_KEY, map);}
/** Record that a freshly opened `tabId` should continue an existing subtree: * its first commit seeds `current` to this node (single-use). */export async function setPendingAttach(tabId: number, attach: PendingAttach): Promise<void> { const map = await read<PendingAttach>(ATTACH_KEY); map[tabId] = attach; await write(ATTACH_KEY, map);}
/** Consume and clear the pending attach for a tab (single-use). */export async function takePendingAttach(tabId: number): Promise<PendingAttach | undefined> { const map = await read<PendingAttach>(ATTACH_KEY); const attach = map[tabId]; if (attach !== undefined) { delete map[tabId]; await write(ATTACH_KEY, map); } return attach;}
/** The tab's mirrored browser session-history stack (empty if unseen). */export async function getNavStack(tabId: number): Promise<NavStack> { return (await read<NavStack>(NAVSTACK_KEY))[tabId] ?? emptyStack();}
export async function setNavStack(tabId: number, stack: NavStack): Promise<void> { const map = await read<NavStack>(NAVSTACK_KEY); map[tabId] = stack; await write(NAVSTACK_KEY, map);}
/** Record that `newTabId` was opened from `sourceVisitId`. */export async function setPendingOpener(newTabId: number, sourceVisitId: string): Promise<void> { const map = await read<string>(OPENER_KEY); map[newTabId] = sourceVisitId; await write(OPENER_KEY, map);}
/** Peek at a tab's stashed opener WITHOUT consuming it. Used to avoid one signal * (tabs.onCreated) clobbering an opener another (onCreatedNavigationTarget) * already stashed — the two race in Firefox where only one of them fires. */export async function getPendingOpener(tabId: number): Promise<string | undefined> { return (await read<string>(OPENER_KEY))[tabId];}
/** Consume and clear the opener for a tab (single-use). */export async function takePendingOpener(tabId: number): Promise<string | undefined> { const map = await read<string>(OPENER_KEY); const id = map[tabId]; if (id !== undefined) { delete map[tabId]; await write(OPENER_KEY, map); } return id;}
/** * Pending titles are keyed by document (stripHash) WITHIN a tab. A single slot * per tab used to lose data two ways: a title and a favicon for the same page * arrive as separate events (the second clobbered the first's other field), and * two pages in flight at once clobbered each other entirely. Keying by url and * merging fields fixes both — important since titles are the racy stream. */type PendingTitleMap = Record<string, PendingTitle>;
/** Stash (merging) a title/favicon that arrived before its visit row existed. */export async function setPendingTitle(tabId: number, pending: PendingTitle): Promise<void> { const map = await read<PendingTitleMap>(TITLE_KEY); const perTab = map[tabId] ?? {}; const key = stripHash(pending.url); const prev = perTab[key]; perTab[key] = { url: pending.url, title: pending.title ?? prev?.title, faviconUrl: pending.faviconUrl ?? prev?.faviconUrl, }; map[tabId] = perTab; await write(TITLE_KEY, map);}
/** Consume a stashed title for `url` (single-use). */export async function takePendingTitle( tabId: number, url: string,): Promise<PendingTitle | undefined> { const map = await read<PendingTitleMap>(TITLE_KEY); const perTab = map[tabId]; if (!perTab) return undefined; const key = stripHash(url); const pending = perTab[key]; if (!pending) return undefined; delete perTab[key]; if (Object.keys(perTab).length === 0) delete map[tabId]; else map[tabId] = perTab; await write(TITLE_KEY, map); return pending;}