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(extension): add the background worker and content script

Shahriarf6d5fe0305fd
diff --git a/entrypoints/background.ts b/entrypoints/background.ts
new file mode 100644
index 0000000..bb6c682
--- /dev/null
+++ b/entrypoints/background.ts
@@ -0,0 +1,79 @@
+import { failure, isBridgeRequest, success } from '@/lib/actions/protocol';
+import { describeActions } from '@/lib/actions/registry';
+import { injectContentScript } from '@/lib/actions/client';
+import { invokeForHarness } from '@/lib/bridge/invoke';
+import { analyzeStoredFile } from '@/lib/bridge/file-store';
+import { serveRunPorts } from '@/lib/bridge/run-port';
+import { RECONNECT_ALARM, connectDaemon, disconnectDaemon, pairDaemon } from '@/lib/bridge/socket';
+
+export default defineBackground(() => {
+ // Action bridge for extension pages (the side panel).
+ // 'describe' lists every action as an MCP-ready tool; 'invoke' routes one to the active tab.
+ browser.runtime.onMessage.addListener((message, _sender, sendResponse) => {
+ if (!isBridgeRequest(message)) return;
+ if (message.op === 'describe') {
+ sendResponse(success(describeActions()));
+ return;
+ }
+ if (message.op === 'invoke' && typeof message.action === 'string') {
+ invokeForHarness(message.action, message.input)
+ .then(sendResponse)
+ .catch((error) => sendResponse(failure('BRIDGE_ERROR', String(error))));
+ return true;
+ }
+ // Summarize a stored file. Fire-and-forget: the helper reads the bytes, runs the daemon
+ // round-trip, and writes the summary/status back to the file index, which the side panel
+ // observes via storage.onChanged — so it lands even if the panel that asked has closed.
+ // The daemon's own keepalive pings hold the worker alive across the round-trip.
+ if (message.op === 'analyzeFile' && typeof message.fileId === 'string') {
+ void analyzeStoredFile(message.fileId);
+ sendResponse(success(true));
+ return;
+ }
+ // Pairing is driven from the popup: the worker owns the socket, the UI just asks.
+ if (message.op === 'pair' && typeof message.token === 'string') {
+ pairDaemon(message.token)
+ .then((result) =>
+ sendResponse(result.ok ? success(true) : failure('PAIRING_FAILED', result.error ?? 'Pairing failed')),
+ )
+ .catch((error) => sendResponse(failure('BRIDGE_ERROR', String(error))));
+ return true;
+ }
+ if (message.op === 'disconnect') {
+ disconnectDaemon()
+ .then(() => sendResponse(success(true)))
+ .catch((error) => sendResponse(failure('BRIDGE_ERROR', String(error))));
+ return true;
+ }
+ sendResponse(failure('INVALID_REQUEST', 'Expected {op:"describe"|"invoke"|"pair"|"disconnect"}'));
+ return;
+ });
+
+ // Declared content scripts only load with a page, so tabs that were open before this
+ // extension version loaded have no listener in them and every action would fail with
+ // TAB_UNREACHABLE. Re-inject on install and update; pages that refuse (chrome://, the Web
+ // Store) fail injection quietly and stay unreachable, which is correct.
+ browser.runtime.onInstalled.addListener(() => {
+ void browser.tabs.query({}).then((tabs) => {
+ for (const tab of tabs) {
+ if (tab.id != null && !tab.discarded) void injectContentScript(tab.id);
+ }
+ });
+ });
+
+ // Free-text instructions from the popup and side panel, and the run events that come back.
+ // A port rather than sendMessage, because assistant text streams in token by token.
+ serveRunPorts();
+
+ // The same actions, reached from outside the browser by the local MCP daemon — but only
+ // once the user has paired this browser. An unpaired extension never dials out.
+ void connectDaemon();
+ browser.runtime.onStartup.addListener(() => void connectDaemon());
+
+ // Chrome stops an idle worker after 30s. Daemon pings keep it alive while connected;
+ // this alarm is the only thing that can revive it once it has been torn down.
+ browser.alarms.create(RECONNECT_ALARM, { periodInMinutes: 1 });
+ browser.alarms.onAlarm.addListener((alarm) => {
+ if (alarm.name === RECONNECT_ALARM) void connectDaemon();
+ });
+});
diff --git a/entrypoints/content.ts b/entrypoints/content.ts
new file mode 100644
index 0000000..037b7bb
--- /dev/null
+++ b/entrypoints/content.ts
@@ -0,0 +1,14 @@
+import { exposeActions } from '@/lib/actions/host';
+
+export default defineContentScript({
+ matches: ['*://*/*'],
+ main() {
+ // This script arrives two ways: declared in the manifest (fresh page loads) and injected
+ // programmatically (tabs that were open before the extension loaded). A tab can get both,
+ // and a second exposeActions() would answer every action twice.
+ const world = globalThis as { __voicelinkActions?: boolean };
+ if (world.__voicelinkActions) return;
+ world.__voicelinkActions = true;
+ exposeActions();
+ },
+});