Overview
Concrete function that creates a snapshot, formats it as text, searches with OR/glob patterns, and returns context-windowed results with match markers.
Description
The searchAndFormat function combines snapshot creation, formatting, and text search into a single call. It creates a snapshot for the tab, formats it to text, passes the text and query to `searchSnapshotText` (which supports `|` for OR terms and glob patterns like `*?[]{}`), and formats the results with configurable context lines around matches. Matched lines are marked with `✓` and results are grouped by proximity with `----` separators. If no matches are found, it returns a "No matches found" message.
Usage
- Search for elements: `const result = await searchAndFormat(tabId, "Submit|Save")`
- With context: `await searchAndFormat(tabId, "button*Cancel", 3)` for 3 lines of context
- With options: `await searchAndFormat(tabId, query, 1, { useGlob: true, caseSensitive: true })`
Code Reference
Source Location
- Repository: AIPex
- File: packages/browser-runtime/src/automation/snapshot-provider.ts
- Lines: L516-539 (searchAndFormat), L467-511 (formatSearchResults)
- Support: packages/browser-runtime/src/automation/query.ts:L99-147 (searchSnapshotText)
Signature
export async function searchAndFormat(
tabId: number,
query: string,
contextLevels?: number,
options?: Partial<SearchOptions>
): Promise<string | null>
interface SearchOptions {
contextLevels: number;
useGlob?: boolean;
caseSensitive?: boolean;
}
Import
import { searchAndFormat } from "@aipexstudio/browser-runtime";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| tabId |
number |
Yes |
Target browser tab ID
|
| query |
string |
Yes |
` for OR, glob patterns)
|
| contextLevels |
number |
No |
Lines of context around matches (default 1)
|
| options |
Partial<SearchOptions> |
No |
useGlob, caseSensitive
|
Outputs
| Name |
Type |
Description
|
| returns |
null |
Formatted results with `✓` markers, or "No matches found", or null if no snapshot
|
Usage Examples
Search for Submit Button
import { searchAndFormat } from "@aipexstudio/browser-runtime";
const result = await searchAndFormat(tabId, "Submit|Save|Confirm", 2);
if (result) {
console.log(result);
// ✓ button "Submit" uid=abc123 <button> focusable
// → div "form-container"
// → div "page-content"
// ----
// ✓ button "Save" uid=def456 <button> focusable
}
Glob Pattern Search
const result = await searchAndFormat(tabId, "button*Cancel", 1, {
useGlob: true,
});
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.