Skip to content

An agentic browser harness: an AI side panel & orb in your own logged-in Chrome, Edge, Arc, Brave or even Firefox run by the agent/subscription you already use (Claude Code, Codex, Antigravity). Free, open source, no API key. Also an MCP server.

feat(monitor): announce a finished watch on the page the user is looking at

Shahriar8164719c3f40
diff --git a/src/extension/entrypoints/background.ts b/src/extension/entrypoints/background.ts
index 926e3fc..da7ecd6 100644
--- a/src/extension/entrypoints/background.ts
+++ b/src/extension/entrypoints/background.ts
@@ -14,6 +14,7 @@ import { appendEvents, recordingStateFor, serveRecorder } from '@/lib/bridge/rec
import { closePanels, onPanelPresence, serveRunPorts, serveTabSessions } from '@/lib/bridge/run-port';
import { clearStrandedRail, serveRail, setPanelCollapsed, syncRail } from '@/lib/bridge/rail';
import { serveTimers } from '@/lib/bridge/timer';
+import { serveToast } from '@/lib/bridge/toast';
import { closeSidebar, openSidePanel } from '@/lib/bridge/side-panel';
import { isAgentKind } from '@/lib/agents/catalog';
import {
@@ -208,6 +209,7 @@ export default defineBackground(() => {
serveRunPorts();
serveTabSessions();
serveRail();
+ serveToast();
void syncRail();
onWelcome(() => {
diff --git a/src/extension/entrypoints/content.ts b/src/extension/entrypoints/content.ts
index 04a5202..85fe0fc 100644
--- a/src/extension/entrypoints/content.ts
+++ b/src/extension/entrypoints/content.ts
@@ -3,6 +3,7 @@ import { exposeIndicator } from '@/lib/indicator/host';
import { exposeMonitor } from '@/lib/monitor/watch';
import { exposeRail } from '@/lib/rail/host';
import { exposeRecorder } from '@/lib/recordings/host';
+import { exposeToast } from '@/lib/toast/host';
export default defineContentScript({
matches: ['*://*/*'],
@@ -15,5 +16,6 @@ export default defineContentScript({
exposeMonitor();
exposeIndicator();
exposeRail();
+ exposeToast();
},
});
diff --git a/src/lib/bridge/monitor.ts b/src/lib/bridge/monitor.ts
index cd80d17..338dfe7 100644
--- a/src/lib/bridge/monitor.ts
+++ b/src/lib/bridge/monitor.ts
@@ -27,6 +27,7 @@ import {
type MonitorUntil,
} from '@/lib/monitor/events';
import { hostOf, originOf } from '@/lib/recordings/events';
+import { showToast } from '@/lib/bridge/toast';
const MONITORS_KEY = 'browsentic/monitors';
const DONE_KEY = 'browsentic/monitorsDone';
@@ -287,7 +288,7 @@ export async function finishMonitor(
}
for (const settle of waiters.get(monitorId) ?? []) settle(state);
waiters.delete(monitorId);
- if (phase !== 'stopped') await notify(state);
+ if (phase !== 'stopped') await announce(state);
fanOut(state);
return state;
}
@@ -495,16 +496,27 @@ function pathOf(url: string): string {
}
}
-async function notify(state: MonitorState): Promise<void> {
- const name = state.label ?? state.host;
- const copy: Partial<Record<MonitorPhase, { title: string; message: string }>> = {
- done: { title: `Done: ${name}`, message: `Finished after ${clock(state.elapsedMs)}.` },
- timeout: { title: `Still going: ${name}`, message: `Watched for ${clock(state.elapsedMs)} without completing.` },
- 'tab-closed': { title: 'Monitor ended — tab closed', message: `${name} was closed before finishing.` },
- 'tab-navigated': { title: 'Monitor ended — tab moved on', message: state.message ?? `The tab left ${state.host}.` },
- };
- const content = copy[state.phase];
+/**
+ * A card drawn onto the page the user is on, which is the only surface they are certainly
+ * looking at. The OS notification stays as the fallback for when no page will take one —
+ * every tab is a chrome:// page, or the window has nothing open.
+ */
+async function announce(state: MonitorState): Promise<void> {
+ const content = copyFor(state);
if (!content) return;
+
+ const shown = await showToast(
+ {
+ toastId: state.monitorId,
+ tone: state.phase === 'done' ? 'live' : 'warn',
+ title: content.title,
+ body: content.message,
+ tabId: state.phase === 'tab-closed' ? undefined : state.tabId,
+ },
+ state.tabId,
+ );
+ if (shown) return;
+
await browser.notifications
?.create(`${NOTIFICATION_PREFIX}${state.monitorId}`, {
type: 'basic',
@@ -515,6 +527,17 @@ async function notify(state: MonitorState): Promise<void> {
.catch(() => undefined);
}
+function copyFor(state: MonitorState): { title: string; message: string } | undefined {
+ const name = state.label ?? state.host;
+ const copy: Partial<Record<MonitorPhase, { title: string; message: string }>> = {
+ done: { title: `Done: ${name}`, message: `Finished after ${clock(state.elapsedMs)}.` },
+ timeout: { title: `Still going: ${name}`, message: `Watched for ${clock(state.elapsedMs)} without completing.` },
+ 'tab-closed': { title: 'Monitor ended — tab closed', message: `${name} was closed before finishing.` },
+ 'tab-navigated': { title: 'Monitor ended — tab moved on', message: state.message ?? `The tab left ${state.host}.` },
+ };
+ return copy[state.phase];
+}
+
function largestIcon(): string {
const icons = browser.runtime.getManifest().icons ?? {};
const sizes = Object.keys(icons)
diff --git a/src/lib/bridge/toast.ts b/src/lib/bridge/toast.ts
new file mode 100644
index 0000000..0bca43e
--- /dev/null
+++ b/src/lib/bridge/toast.ts
@@ -0,0 +1,80 @@
+import { browser } from 'wxt/browser';
+import { injectContentScript } from '@/lib/actions/client';
+import {
+ TOAST_CHANNEL,
+ TOAST_DURATION_MS,
+ isToastRequest,
+ type ToastCommand,
+ type ToastTone,
+ type ToastView,
+} from '@/lib/toast/events';
+import { readTheme } from './theme';
+
+const NO_CONTENT_SCRIPT = 'Receiving end does not exist';
+
+export interface ToastNotice {
+ toastId: string;
+ tone: ToastTone;
+ title: string;
+ body: string;
+ tabId?: number;
+}
+
+/**
+ * Draws a notice on the page the user is actually looking at, falling back to the window
+ * that owns `nearTabId` when the focused one cannot host it. Answers false when no page
+ * would take it — a chrome:// tab, the Web Store, a window with nothing open — which is
+ * the caller's cue to fall back to an OS notification.
+ */
+export async function showToast(notice: ToastNotice, nearTabId?: number): Promise<boolean> {
+ const view: ToastView = { ...notice, theme: await readTheme(), durationMs: TOAST_DURATION_MS };
+ const command: ToastCommand = { channel: TOAST_CHANNEL, op: 'show', view };
+ for (const tabId of await candidates(nearTabId)) {
+ if (await post(tabId, command)) return true;
+ }
+ return false;
+}
+
+async function candidates(nearTabId?: number): Promise<number[]> {
+ const ids: number[] = [];
+ const add = (id: number | undefined) => {
+ if (id != null && !ids.includes(id)) ids.push(id);
+ };
+
+ const [focused] = await browser.tabs.query({ active: true, lastFocusedWindow: true }).catch(() => []);
+ add(focused?.id);
+
+ if (nearTabId == null) return ids;
+ const home = await browser.tabs.get(nearTabId).catch(() => null);
+ if (home?.windowId == null) return ids;
+ const [sibling] = await browser.tabs.query({ active: true, windowId: home.windowId }).catch(() => []);
+ add(sibling?.id);
+ return ids;
+}
+
+async function post(tabId: number, command: ToastCommand): Promise<boolean> {
+ const send = async () =>
+ ((await browser.tabs.sendMessage(tabId, command)) as { ok?: boolean } | undefined)?.ok === true;
+ try {
+ return await send();
+ } catch (error) {
+ const message = error instanceof Error ? error.message : String(error);
+ if (!message.includes(NO_CONTENT_SCRIPT)) return false;
+ if (!(await injectContentScript(tabId))) return false;
+ return send().catch(() => false);
+ }
+}
+
+export function serveToast(): void {
+ browser.runtime.onMessage.addListener((message: unknown, _sender, sendResponse) => {
+ if (!isToastRequest(message)) return;
+ void goToTab(message.tabId);
+ sendResponse({ ok: true });
+ });
+}
+
+async function goToTab(tabId: number): Promise<void> {
+ const tab = await browser.tabs.update(tabId, { active: true }).catch(() => null);
+ if (tab?.windowId == null) return;
+ await browser.windows.update(tab.windowId, { focused: true }).catch(() => undefined);
+}
diff --git a/src/lib/toast/events.ts b/src/lib/toast/events.ts
new file mode 100644
index 0000000..d1da19e
--- /dev/null
+++ b/src/lib/toast/events.ts
@@ -0,0 +1,38 @@
+import type { ThemeId } from '@/lib/bridge/theme';
+
+export const TOAST_CHANNEL = 'browsentic/toast';
+
+export const TOAST_DURATION_MS = 10_000;
+
+export const MAX_TOASTS = 3;
+
+export type ToastTone = 'live' | 'warn';
+
+export interface ToastView {
+ toastId: string;
+ theme: ThemeId;
+ tone: ToastTone;
+ title: string;
+ body: string;
+ durationMs: number;
+ /** The tab a click should land on. Absent when the tab is gone — the card is then only dismissible. */
+ tabId?: number;
+}
+
+export type ToastCommand =
+ | { channel: typeof TOAST_CHANNEL; op: 'show'; view: ToastView }
+ | { channel: typeof TOAST_CHANNEL; op: 'hide'; toastId?: string };
+
+export type ToastRequest = { channel: typeof TOAST_CHANNEL; op: 'activate'; tabId: number };
+
+export function isToastCommand(message: unknown): message is ToastCommand {
+ if (typeof message !== 'object' || message === null) return false;
+ const frame = message as { channel?: unknown; op?: unknown };
+ return frame.channel === TOAST_CHANNEL && (frame.op === 'show' || frame.op === 'hide');
+}
+
+export function isToastRequest(message: unknown): message is ToastRequest {
+ if (typeof message !== 'object' || message === null) return false;
+ const frame = message as { channel?: unknown; op?: unknown; tabId?: unknown };
+ return frame.channel === TOAST_CHANNEL && frame.op === 'activate' && typeof frame.tabId === 'number';
+}
diff --git a/src/lib/toast/host.ts b/src/lib/toast/host.ts
new file mode 100644
index 0000000..06912f1
--- /dev/null
+++ b/src/lib/toast/host.ts
@@ -0,0 +1,235 @@
+import { browser } from 'wxt/browser';
+import { RAIL_PALETTES, RAIL_TONES } from '@/lib/rail/events';
+import { MAX_TOASTS, TOAST_CHANNEL, isToastCommand, type ToastView } from './events';
+
+export { TOAST_CHANNEL } from './events';
+
+const HOST_ID = 'browsentic-toast';
+const EXIT_MS = 180;
+
+/**
+ * Completion notices drawn into whatever page the user is looking at, because an OS
+ * notification is a banner they have already walked past. Same closed shadow root as the
+ * rail — the page can neither style it nor read it, and `extractText` never sees it.
+ */
+export function exposeToast(): void {
+ let host: HTMLElement | null = null;
+ let root: ShadowRoot | null = null;
+ let stack: HTMLElement | null = null;
+ const timers = new Map<string, () => void>();
+
+ /* Answered through sendResponse, not a returned promise: on Chrome `browser` is the raw
+ `chrome` object, which honours only a literal `true` return and would drop the verdict
+ the background needs to decide whether an OS notification is still owed. Both drawing
+ calls are synchronous, so the response goes out before this listener returns. */
+ browser.runtime.onMessage.addListener((message: unknown, _sender, sendResponse) => {
+ if (!isToastCommand(message)) return;
+ if (message.op === 'hide') {
+ if (message.toastId) dismiss(message.toastId);
+ else clear();
+ sendResponse({ ok: true });
+ return;
+ }
+ sendResponse({ ok: render(message.view) });
+ });
+
+ /* A card left by a previous extension life is dead markup — its click reaches nothing. */
+ document.getElementById(HOST_ID)?.remove();
+ window.addEventListener('pageshow', (event) => {
+ if (event.persisted) clear();
+ });
+
+ function clear(): void {
+ for (const cancel of timers.values()) cancel();
+ timers.clear();
+ host?.remove();
+ host = null;
+ root = null;
+ stack = null;
+ }
+
+ function dismiss(toastId: string): void {
+ timers.get(toastId)?.();
+ timers.delete(toastId);
+ const card = stack?.querySelector<HTMLElement>(`[data-toast="${CSS.escape(toastId)}"]`);
+ if (!card) return;
+ card.classList.add('out');
+ setTimeout(() => {
+ card.remove();
+ if (stack && !stack.childElementCount) clear();
+ }, EXIT_MS);
+ }
+
+ function mount(view: ToastView): boolean {
+ if (!document.documentElement) return false;
+ if (!host || !host.isConnected) {
+ clear();
+ host = document.createElement('div');
+ host.id = HOST_ID;
+ host.style.cssText = 'all: initial; position: static;';
+ root = host.attachShadow({ mode: 'closed' });
+ root.innerHTML = `<style>${STYLES}</style><div class="stack" role="region" aria-label="Browsentic"></div>`;
+ stack = root.querySelector('.stack');
+ document.documentElement.append(host);
+ }
+ if (!stack) return false;
+ const palette = RAIL_PALETTES[view.theme];
+ for (const [key, value] of Object.entries(palette)) stack.style.setProperty(`--${key}`, value);
+ return true;
+ }
+
+ function render(view: ToastView): boolean {
+ if (!mount(view)) return false;
+ dismissNow(view.toastId);
+
+ const card = document.createElement('div');
+ card.className = 'card';
+ card.dataset.toast = view.toastId;
+ card.setAttribute('role', 'status');
+ card.style.setProperty('--tone', RAIL_TONES[view.theme][view.tone]);
+ card.innerHTML = `
+ <button type="button" class="close" aria-label="Dismiss">
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" aria-hidden="true"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>
+ </button>
+ <div class="head"><span class="dot"></span><span class="title"></span></div>
+ <p class="body"></p>
+ <span class="cue"></span>
+ <span class="bar"><i></i></span>`;
+
+ card.querySelector<HTMLElement>('.title')!.textContent = view.title;
+ card.querySelector<HTMLElement>('.body')!.textContent = view.body;
+
+ const cue = card.querySelector<HTMLElement>('.cue')!;
+ if (view.tabId == null) cue.remove();
+ else {
+ cue.textContent = 'Click to open the tab';
+ card.classList.add('go');
+ card.addEventListener('click', () => {
+ void browser.runtime
+ .sendMessage({ channel: TOAST_CHANNEL, op: 'activate', tabId: view.tabId })
+ .catch(() => undefined);
+ dismiss(view.toastId);
+ });
+ }
+
+ card.querySelector<HTMLButtonElement>('.close')!.addEventListener('click', (event) => {
+ event.preventDefault();
+ event.stopPropagation();
+ dismiss(view.toastId);
+ });
+
+ card.querySelector<HTMLElement>('.bar i')!.style.animationDuration = `${view.durationMs}ms`;
+
+ stack!.append(card);
+ while (stack!.childElementCount > MAX_TOASTS) {
+ const oldest = (stack!.firstElementChild as HTMLElement | null)?.dataset.toast;
+ if (!oldest) break;
+ dismissNow(oldest);
+ }
+
+ arm(card, view.toastId, view.durationMs);
+ return true;
+ }
+
+ /* Ten seconds of a minimized window is ten seconds nobody saw. The countdown — and the
+ bar that draws it — only starts once the document is on screen. */
+ function arm(card: HTMLElement, toastId: string, durationMs: number): void {
+ if (document.visibilityState === 'visible') {
+ card.classList.remove('wait');
+ const handle = setTimeout(() => dismiss(toastId), durationMs);
+ timers.set(toastId, () => clearTimeout(handle));
+ return;
+ }
+ card.classList.add('wait');
+ const onVisible = () => {
+ if (document.visibilityState !== 'visible') return;
+ document.removeEventListener('visibilitychange', onVisible);
+ arm(card, toastId, durationMs);
+ };
+ document.addEventListener('visibilitychange', onVisible);
+ timers.set(toastId, () => document.removeEventListener('visibilitychange', onVisible));
+ }
+
+ function dismissNow(toastId: string): void {
+ timers.get(toastId)?.();
+ timers.delete(toastId);
+ stack?.querySelector(`[data-toast="${CSS.escape(toastId)}"]`)?.remove();
+ }
+}
+
+const STYLES = `
+ :host { all: initial; }
+ * { box-sizing: border-box; margin: 0; padding: 0; }
+ .stack {
+ position: fixed;
+ top: 16px;
+ right: 16px;
+ z-index: 2147483647;
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+ width: 320px;
+ max-width: calc(100vw - 32px);
+ font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
+ pointer-events: none;
+ }
+ .card {
+ position: relative;
+ overflow: hidden;
+ padding: 12px 34px 13px 13px;
+ border: 1px solid var(--line);
+ border-radius: 14px;
+ background: var(--ground2);
+ box-shadow: 0 14px 40px -14px rgb(0 0 0 / 70%), 0 0 0 1px rgb(0 0 0 / 25%);
+ pointer-events: auto;
+ animation: browsentic-toast-in 200ms cubic-bezier(0.2, 0.9, 0.3, 1);
+ }
+ .card.go { cursor: pointer; }
+ .card.go:hover { background: var(--surface); }
+ .card.out { animation: browsentic-toast-out ${EXIT_MS}ms ease-in forwards; }
+ .head { display: flex; align-items: center; gap: 7px; }
+ .dot {
+ flex: none;
+ width: 7px;
+ height: 7px;
+ border-radius: 999px;
+ background: var(--tone);
+ box-shadow: 0 0 8px 1px color-mix(in oklch, var(--tone) 70%, transparent);
+ }
+ .title {
+ font: 600 13px/1.35 inherit;
+ color: var(--ink);
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+ .body { margin-top: 4px; font: 400 12px/1.45 inherit; color: var(--inkDim); }
+ .cue { display: block; margin-top: 6px; font: 500 11px/1.4 inherit; color: var(--brand); }
+ .close {
+ position: absolute;
+ top: 8px;
+ right: 8px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: 20px;
+ height: 20px;
+ border: 0;
+ border-radius: 7px;
+ background: transparent;
+ color: var(--inkFaint);
+ cursor: pointer;
+ }
+ .close:hover { background: var(--surface); color: var(--ink); }
+ .close svg { width: 12px; height: 12px; display: block; }
+ .bar { position: absolute; left: 0; right: 0; bottom: 0; height: 2px; background: var(--line); }
+ .bar i { display: block; height: 100%; background: var(--tone); animation: browsentic-toast-drain linear forwards; }
+ .card.wait .bar i { animation-play-state: paused; }
+ @keyframes browsentic-toast-in { from { opacity: 0; transform: translateX(14px); } }
+ @keyframes browsentic-toast-out { to { opacity: 0; transform: translateX(14px); } }
+ @keyframes browsentic-toast-drain { from { width: 100%; } to { width: 0; } }
+ @media (prefers-reduced-motion: reduce) {
+ .card, .card.out { animation: none; }
+ .bar i { width: 100%; animation: none; }
+ }
+`;