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.
TypeScript agentic-browseragentic-browsingagy-cliaiai-agentai-browser-extensionai-toolsbrave-extensionbrowser-agentbrowser-automationbrowser-extensionbrowser-usebrowserskillchrome-extensionclaude-codecodexfirefox-addonmcp-servernanobrowserweb-automation
refactor(tools): switching a tool's every-visit run replaces its entry instead of mutating it, missing user scripts register side by side, and the comments keep only Chrome's quirks
Shahriar8ee8faab93ec
diff --git a/src/extension/components/saved-tools.tsx b/src/extension/components/saved-tools.tsxindex 19116e1..8a8de94 100644--- a/src/extension/components/saved-tools.tsx+++ b/src/extension/components/saved-tools.tsx@@ -13,8 +13,6 @@ import { displayName, slugify } from '@/lib/skills/saved-tool';* The offer, a second after an approved toolkit lands. It shows the name it would take* rather than asking for one, because the answer is usually yes and the name is usually* right — but the last segment stays editable, since that is the part the user will type.- * Running on every visit starts off: it is a second decision, about code that will then- * run without being asked for.*/export function KeepToolPrompt({offer,@@ -82,7 +80,6 @@ export function KeepToolPrompt({);}-/** Chrome keeps user scripts behind a per-extension switch that no API can flip, so point at it. */function AllowUserScripts() {return (<p className="mt-1.5 text-[10px] leading-relaxed text-amber">@@ -95,7 +92,7 @@ function AllowUserScripts() {);}-/** `/remove-tools`: everything saved, with a cross on each and whether it runs on every visit. */+/** `/remove-tools`: everything saved, with a cross on each. */export function SavedToolList({tools,autoRunReady,diff --git a/src/lib/bridge/auto-run.test.ts b/src/lib/bridge/auto-run.test.tsindex ff9ceaf..80dbf06 100644--- a/src/lib/bridge/auto-run.test.ts+++ b/src/lib/bridge/auto-run.test.ts@@ -32,7 +32,6 @@ interface FakePage {load: () => void;}-/** Just enough of a page for the injected source: a location, the Navigation API, a DOM that can change and load. */function openPage(url: string, saved: SavedTool = tool(), readyState = 'complete'): FakePage {const location = new URL(url);const document = { readyState, documentElement: {} };diff --git a/src/lib/bridge/auto-run.ts b/src/lib/bridge/auto-run.tsindex 7501450..91dee64 100644--- a/src/lib/bridge/auto-run.ts+++ b/src/lib/bridge/auto-run.ts@@ -1,18 +1,3 @@-/**- * Saved tools that run by themselves on every visit.- *- * Chrome's `userScripts` API is the mechanism, not the debugger the `/` path uses: a user- * script is injected by the browser on each matching load, needs no debugging bar, works- * with DevTools open, is exempt from the page's CSP and outlives a restart. It only runs- * once the user has turned on **Allow User Scripts** for the extension, which is why the- * flag on the saved tool is the truth and the registration is a mirror of it, brought back- * into line whenever the list changes, the worker starts, the alarm fires, or the panel asks.- *- * The match pattern is the whole host on purpose. Scope is decided in the page by the same- * rule `/` applies — exact origin, then the first path segment's slug — so it holds for- * single-page sites too, where arriving at `/watch` is a history entry, not a load.- */-import { browser, type Browser } from 'wxt/browser';import { MAX_SLUG, ROOT_SEGMENT } from '@/lib/skills/saved-tool';import { listSavedTools, onSavedToolsChange, type SavedTool } from './saved-tools';@@ -21,17 +6,15 @@ const SCRIPT_PREFIX = 'browsentic-tool-';const SYNC_ALARM = 'browsentic/autoRun';-/** How long the page must go without a DOM change before the tool runs. */export const QUIET_MS = 400;-/** The longest a tool waits for quiet, since some pages never stop changing. */export const SETTLE_CAP_MS = 3_000;type UserScript = Browser.userScripts.RegisteredUserScript;type AutoRunTool = Pick<SavedTool, 'name' | 'origin' | 'scope' | 'fn' | 'code'>;-/** Whether Chrome will accept a registration right now: false on Firefox and until Allow User Scripts is on. */+/** Chrome throws on any `userScripts` call until Allow User Scripts is switched on. */export function autoRunReady(): boolean {if (import.meta.env.FIREFOX) return false;try {@@ -42,16 +25,11 @@ export function autoRunReady(): boolean {}}-/** The extension's own details page, where Chrome keeps the Allow User Scripts switch. */export function openUserScriptSettings(): void {void browser.tabs.create({ url: `chrome://extensions/?id=${browser.runtime.id}` });}-/**- * Chrome raises no event when Allow User Scripts is switched on, and the API simply appears- * in the running worker, so a tool waiting on it is picked up by the panel asking while it- * shows the wait, or by this alarm once the panel is gone.- */+/** Chrome raises no event when Allow User Scripts is switched on, so the alarm picks it up. */export function serveAutoRuns(): void {if (import.meta.env.FIREFOX) return;onSavedToolsChange(() => void syncAutoRuns());@@ -78,10 +56,10 @@ async function reconcile(): Promise<void> {const current = new Set(ours.filter((script) => sameScript(script, wanted.get(script.id))).map((script) => script.id));const stale = ours.filter((script) => !current.has(script.id)).map((script) => script.id);+ const missing = [...wanted.values()].filter((script) => !current.has(script.id));+if (stale.length) await browser.userScripts.unregister({ ids: stale });- for (const [id, script] of wanted) {- if (!current.has(id)) await browser.userScripts.register([script]).catch(() => undefined);- }+ await Promise.all(missing.map((script) => browser.userScripts.register([script]).catch(() => undefined)));}function scriptIdOf(tool: SavedTool): string {@@ -98,7 +76,7 @@ function scriptFor(tool: SavedTool): UserScript {};}-/** Every path on the tool's host, any port; the page narrows it to the exact origin and segment. */+/** The whole host, so a single-page arrival is caught too; the page itself checks origin and segment. */function hostPattern(origin: string): string {const { protocol, hostname } = new URL(origin);return `${protocol}//${hostname}/*`;@@ -113,13 +91,6 @@ function sameScript(registered: UserScript, wanted: UserScript | undefined): boo);}-/**- * What Chrome injects. The approved code is wrapped exactly as the installer wraps it, so- * it parses the same way it did when the user read it, and it is evaluated once per- * document. The entry point runs each time the document arrives in scope — on the load,- * and again after a single-page site navigates away and back — once the page has loaded- * and its DOM has gone quiet, which is the state the user watched the tool work in.- */export function autoRunSource(tool: AutoRunTool): string {return `(() => {const origin = ${JSON.stringify(tool.origin)};diff --git a/src/lib/bridge/saved-tools.ts b/src/lib/bridge/saved-tools.tsindex dddd7de..5b5ddcb 100644--- a/src/lib/bridge/saved-tools.ts+++ b/src/lib/bridge/saved-tools.ts@@ -35,7 +35,6 @@ export interface SavedTool {code: string;/** The zero-argument entry point `/` invokes. */fn: string;- /** Also run it by itself on every visit to its scope, as a user script Chrome injects. */autoRun?: boolean;createdAt: number;}@@ -69,15 +68,12 @@ export async function saveTool(tool: SavedTool): Promise<void> {}export async function setAutoRun(id: string, autoRun: boolean): Promise<SavedTool | null> {- const tools = await listSavedTools();+ const tools = (await listSavedTools()).map((tool) => (tool.id === id ? { ...tool, autoRun } : tool));const changed = tools.find((tool) => tool.id === id);- if (!changed) return null;- changed.autoRun = autoRun;- await browser.storage.local.set({ [TOOLS_KEY]: tools });- return changed;+ if (changed) await browser.storage.local.set({ [TOOLS_KEY]: tools });+ return changed ?? null;}-/** Fires with the whole list whenever it changes, from any context. */export function onSavedToolsChange(listener: (tools: SavedTool[]) => void): void {browser.storage.local.onChanged.addListener((changes) => {const change = changes[TOOLS_KEY];diff --git a/src/lib/bridge/tool-registry.ts b/src/lib/bridge/tool-registry.tsindex d87f7e8..d44f17c 100644--- a/src/lib/bridge/tool-registry.ts+++ b/src/lib/bridge/tool-registry.ts@@ -33,7 +33,6 @@ export interface KeepRequest {code: string;/** User-editable last name segment; falls back to the suggestion on the offer. */slug?: string;- /** Run it by itself on every visit to its scope as well. */autoRun?: boolean;}@@ -62,7 +61,6 @@ export async function keepTool(request: KeepRequest): Promise<SavedToolMeta> {return withoutCode(tool);}-/** The flag is the truth; the user script follows it from the store's change listener. */export async function switchAutoRun(id: string, autoRun: boolean): Promise<void> {const tool = await setAutoRun(id, autoRun);if (tool) await saveSkill(skillFor(tool)).catch(() => undefined);diff --git a/src/lib/bridge/use-run.ts b/src/lib/bridge/use-run.tsindex e7189e8..b8e223f 100644--- a/src/lib/bridge/use-run.ts+++ b/src/lib/bridge/use-run.ts@@ -15,7 +15,6 @@ import type { TabSession } from './tab-sessions';const RECONNECT_MS = 1_000;-/** How often the panel asks again while a tool waits on Chrome's Allow User Scripts switch. */const USER_SCRIPTS_RECHECK_MS = 2_000;const EXTERNAL_VIEW = 'external';@@ -53,7 +52,6 @@ export interface Run {tools: SavedToolMeta[];/** Raised a second after an approved toolkit lands, asking whether to keep it. */toolOffer: ToolOffer | null;- /** Whether Chrome will run a tool on every visit yet — false until Allow User Scripts is on. */autoRunReady: boolean;keepTool: (slug?: string, autoRun?: boolean) => void;dismissTool: () => void;