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(actions): add page.searchSite to search a site with its own search

Shahriar82613be57cc8
diff --git a/lib/actions/page/search-site.ts b/lib/actions/page/search-site.ts
new file mode 100644
index 0000000..6e42ef4
--- /dev/null
+++ b/lib/actions/page/search-site.ts
@@ -0,0 +1,99 @@
+import { z } from 'zod';
+import { ActionError, defineAction } from '../core';
+import { cssPath, resolveTarget, targetSchema, type Target } from './dom';
+import { typeInto } from './fill-input';
+import {
+ bestSearchField,
+ describeField,
+ fieldFor,
+ searchFields,
+ searchToggles,
+ searchUrl,
+ type SearchField,
+} from './search';
+
+const MAX_QUERY = 200;
+
+export const searchSite = defineAction({
+ name: 'page.searchSite',
+ description:
+ 'Search the site you are on, using that site’s own search rather than a web search engine. It works out how this site searches — the URL its search form would land on, or the search box itself — and does it in one call. ' +
+ 'Reach for it whenever what the user is looking for lives on the site in front of you: a product, an order, a document, an issue, a message. A web search is for things this site does not know about. ' +
+ 'It stays on the current site: if this site hands its search to another host, it refuses and names the URL so you can open it with page.navigate. ' +
+ 'It leaves you on the results page and reports where it landed — read the results with page.getPageInfo or page.extractText afterwards, and act on them with the ordinary tools.',
+ input: z.object({
+ query: z.string().min(1).max(MAX_QUERY).describe('What to look for on this site'),
+ strategy: z
+ .enum(['auto', 'url', 'field'])
+ .default('auto')
+ .describe(
+ 'How to search: "auto" goes straight to the site’s search URL when one can be worked out and types into the box otherwise, ' +
+ '"url" insists on the URL, "field" insists on typing — which is what a search box that filters as you type needs',
+ ),
+ target: targetSchema
+ .optional()
+ .describe('The search box to use, when the page has several or the one picked was wrong'),
+ }),
+ execute({ query, strategy, target }) {
+ const field = chooseField(target);
+ const built = strategy === 'field' ? null : searchUrl(field, query);
+
+ if (built) {
+ if (built.url.origin !== location.origin) throw offSite(built.url.href);
+ location.assign(built.url.href);
+ return {
+ searchedFor: query,
+ via: 'url',
+ templateFrom: built.from,
+ navigatingTo: built.url.href,
+ field: field ? describeField(field) : undefined,
+ };
+ }
+
+ if (strategy === 'url') {
+ throw new ActionError(
+ 'No search URL could be worked out here — this site’s search is not a GET form and the address carries no query parameter. Try strategy "field".',
+ 'TARGET_NOT_FOUND',
+ );
+ }
+ return typeIntoField(field, query);
+ },
+});
+
+function chooseField(target?: Target): SearchField | null {
+ if (target) return fieldFor(resolveTarget(target, { includeHidden: true }));
+ return bestSearchField(searchFields());
+}
+
+function typeIntoField(field: SearchField | null, query: string) {
+ if (!field) {
+ throw new ActionError(
+ 'No search box on this page. Call page.findSearch to see what search this site does have, if any.',
+ 'TARGET_NOT_FOUND',
+ );
+ }
+ if (field.hidden) {
+ throw new ActionError(`The search box on this page is hidden${revealHint()}.`, 'TARGET_NOT_FOUND');
+ }
+ const action = field.form?.getAttribute('action');
+ const posted = action && new URL(action, location.href);
+ if (posted && posted.origin !== location.origin) throw offSite(posted.href);
+
+ field.element.focus();
+ const { submitted } = typeInto(field.element, query, { clear: true, pressEnter: true });
+ return { searchedFor: query, via: 'field', submitted, field: describeField(field) };
+}
+
+function revealHint(): string {
+ const toggle = searchToggles(searchFields())[0];
+ return toggle
+ ? ` — click ${cssPath(toggle)} to reveal it, then search again`
+ : ', and nothing on the page reveals it';
+}
+
+function offSite(href: string): ActionError {
+ return new ActionError(
+ `This site hands its search to another host. Open ${href} with page.navigate instead, so it goes through the usual checks.`,
+ 'UNSUPPORTED',
+ );
+}
diff --git a/lib/actions/registry.ts b/lib/actions/registry.ts
index 9b5c9b2..b70c76e 100644
--- a/lib/actions/registry.ts
+++ b/lib/actions/registry.ts
@@ -27,6 +27,7 @@ import { readRecording } from './page/read-recording';
import { readTheme } from './page/read-theme';
import { screenshot } from './page/screenshot';
import { scrollTo } from './page/scroll-to';
+import { searchSite } from './page/search-site';
import { selectOption } from './page/select-option';
import { selectText } from './page/select-text';
import { solveCaptcha } from './page/solve-captcha';
@@ -68,6 +69,7 @@ export const actions: ReadonlyMap<string, AnyAction> = new Map(
awaitMonitor,
stopMonitor,
highlightElement,
+ searchSite,
navigate,
openTab,
switchTab,
diff --git a/lib/bridge/invoke.ts b/lib/bridge/invoke.ts
index e24fbab..131ddd8 100644
--- a/lib/bridge/invoke.ts
+++ b/lib/bridge/invoke.ts
@@ -14,6 +14,7 @@ import { navigate, resolveNavigation, type NavigateInput } from '@/lib/actions/p
import { openTab } from '@/lib/actions/page/open-tab';
import { readRecording } from '@/lib/actions/page/read-recording';
import { screenshot } from '@/lib/actions/page/screenshot';
+import { searchSite } from '@/lib/actions/page/search-site';
import { solveCaptcha } from '@/lib/actions/page/solve-captcha';
import { startMonitor } from '@/lib/actions/page/start-monitor';
import { stopMonitor } from '@/lib/actions/page/stop-monitor';
@@ -56,6 +57,7 @@ export async function invokeForHarness(
: failure('MAPPING_TAB_CHANGED', 'The tab this run started in has been closed.');
}
if (action === navigate.name) return navigateTab(tab.id, input);
+ if (action === searchSite.name) return searchTab(tab.id, input);
if (action === switchTab.name) return switchSessionTab({ id: tab.id, windowId: tab.windowId }, input, owner);
if (action === closeTab.name) return closeOpenTab({ id: tab.id, windowId: tab.windowId }, input);
if (action === screenshot.name) return screenshotTab({ id: tab.id, windowId: tab.windowId }, input, runId);
@@ -276,3 +278,24 @@ async function navigateViaTabsApi(tabId: number, input: unknown): Promise<Action
: { performed: plan.action, loaded: completed },
);
}
+
+const FIELD_SETTLE_MS = 2_000;
+
+async function searchTab(tabId: number, input: unknown): Promise<ActionResult> {
+ const inPage = await invokeInTab(tabId, searchSite.name, input);
+ if (!inPage.ok) return inPage;
+ const searched = inPage.data as { via?: unknown; submitted?: unknown };
+ const loaded = await settleAfterSearch(tabId, searched.via === 'url' || searched.submitted === true);
+ return success({ ...(inPage.data as object), loaded, landedOn: (await pinnedTab(tabId))?.url });
+}
+
+async function settleAfterSearch(tabId: number, navigating: boolean): Promise<boolean> {
+ const watcher = watchForLoad(tabId);
+ if (navigating) return watcher.settled;
+ const raced = await Promise.race([
+ watcher.settled,
+ new Promise<null>((resolve) => setTimeout(() => resolve(null), FIELD_SETTLE_MS)),
+ ]);
+ if (raced === null) watcher.cancel();
+ return raced ?? false;
+}