Skip to main content

DOM API

@grant CAT.agent.dom

The DOM API provides complete browser page automation capabilities, including navigation, content reading, screenshots, form interaction, and DOM monitoring.

Tab Management

listTabs — List Tabs

const tabs = await CAT.agent.dom.listTabs();

Returns information about every open tab.

Return value, TabInfo[]:

FieldTypeDescription
tabIdnumberTab ID
urlstringCurrent URL
titlestringPage title
activebooleanWhether it's the currently active tab
windowIdnumberThe window ID it belongs to
discardedbooleanWhether it has been discarded (suspended)
const result = await CAT.agent.dom.navigate(url, options?);

Parameters:

ParameterTypeDefaultDescription
urlstringTarget URL (required)
options.tabIdnumbercurrently active tabSpecify a tab
options.waitUntilbooleantrueWhether to wait for the page to finish loading
options.timeoutnumber30000Timeout in milliseconds

Return value, NavigateResult:

{ tabId: number; url: string; title: string }

Content Reading

readPage — Read Page Content

const page = await CAT.agent.dom.readPage(options?);

Converts the page DOM into structured text, automatically removing irrelevant elements like <script>, <style>, <noscript>, <svg>, and <link[rel=stylesheet]>.

Parameters:

ParameterTypeDefaultDescription
options.tabIdnumbercurrently active tabSpecify a tab
options.selectorstringCSS selector; only returns content from matching elements
options.maxLengthnumberMaximum character count; truncated beyond this
options.removeTagsstring[]Additional tag names to remove

Return value, PageContent:

FieldTypeDescription
titlestringPage title
urlstringPage URL
htmlstringThe processed page text content
truncatedbooleanWhether the content was truncated
totalLengthnumberThe original total content length

screenshot — Take a Screenshot

const shot = await CAT.agent.dom.screenshot(options?);

Parameters:

ParameterTypeDefaultDescription
options.tabIdnumbercurrently active tabSpecify a tab
options.qualitynumber80JPEG quality (0-100)
options.fullPagebooleanfalseCapture the full page
options.selectorstringCSS selector; only captures the matching element's area
options.saveTostringPath to save to in the OPFS workspace

Return value, ScreenshotResult:

FieldTypeDescription
dataUrlstringA base64 data URL
pathstringThe OPFS save path (when saveTo is used)
sizenumberThe file size (when saveTo is used)

Screenshot mode selection:

ScenarioBehavior
selector is usedLocates the element's bounds via CDP and crops the screenshot
Background tabTries a CDP screenshot; if it fails, activates the tab and uses captureVisibleTab
Foreground tabUses captureVisibleTab directly
// Save a screenshot to OPFS
const shot = await CAT.agent.dom.screenshot({
saveTo: "screenshots/page.png",
quality: 90
});
console.log(`Saved to ${shot.path}, size ${shot.size} bytes`);

Page Interaction

click — Click an Element

const result = await CAT.agent.dom.click(selector, options?);

Parameters:

ParameterTypeDefaultDescription
selectorstringCSS selector (required)
options.tabIdnumbercurrently active tabSpecify a tab
options.trustedbooleanfalseUse CDP to fire a real mouse event

Return value, ActionResult:

FieldTypeDescription
successbooleanWhether it succeeded
navigatedbooleanWhether the click caused page navigation
urlstringThe new URL after navigation
newTabbooleanWhether a new tab was opened

trusted vs. regular click:

  • trusted: false (default) — simulates element.click() via injected JS; fast, but some sites may detect it as a non-genuine event
  • trusted: true — sends a real mouse event via the Chrome DevTools Protocol, behaving identically to a real user action, but requires the debugger permission

fill — Fill a Form

const result = await CAT.agent.dom.fill(selector, value, options?);

Parameters:

ParameterTypeDescription
selectorstringCSS selector (required)
valuestringThe value to fill in (required)
options.tabIdnumberSpecify a tab
options.trustedbooleanUse CDP to simulate keyboard input

Behavior:

  • Normal mode: sets element.value and dispatches an input event
  • trusted mode: CDP focuses the element → types character by character

scroll — Scroll the Page

const result = await CAT.agent.dom.scroll(direction, options?);

Parameters:

ParameterTypeDescription
direction"up" | "down" | "top" | "bottom"Scroll direction (required)
options.tabIdnumberSpecify a tab
options.selectorstringScroll a specific container instead of the whole page

Return value, ScrollResult:

FieldTypeDescription
scrollTopnumberThe scroll position after scrolling
scrollHeightnumberThe total content height
clientHeightnumberThe visible area height
atBottombooleanWhether it has scrolled to the bottom

waitFor — Wait for an Element

const result = await CAT.agent.dom.waitFor(selector, options?);

Polls for the specified element to appear on the page (checking every 500ms).

Parameters:

ParameterTypeDefaultDescription
selectorstringCSS selector (required)
options.tabIdnumbercurrently active tabSpecify a tab
options.timeoutnumber10000Timeout in milliseconds

Return value, WaitForResult:

FieldTypeDescription
foundbooleanWhether the element was found
elementobjectElement info (only present when found=true)
element.selectorstringThe matching selector
element.tagstringTag name
element.textstringText content
element.rolestringARIA role
element.typestringinput type
element.visiblebooleanWhether it's visible

Script Execution

executeScript — Execute JavaScript

const result = await CAT.agent.dom.executeScript(code, options?);

Parameters:

ParameterTypeDefaultDescription
codestringJavaScript code (required)
options.tabIdnumbercurrently active tabSpecify a tab
options.world"MAIN" | "ISOLATED""ISOLATED"Execution environment

Two execution environments:

EnvironmentDescriptionUse case
ISOLATEDThe extension's isolated environment, separate from the page's JSDOM manipulation, reading content, using the extension's blob URLs
MAINThe page's own environment, sharing the window objectCalling the page's JS functions, reading page variables
// ISOLATED — safely read the DOM
const title = await CAT.agent.dom.executeScript(
"return document.querySelector('h1')?.textContent",
{ world: "ISOLATED" }
);

// MAIN — call a JS function on the page
const data = await CAT.agent.dom.executeScript(
"return window.__APP_STATE__",
{ world: "MAIN" }
);

The code is wrapped in new Function() for execution, supporting a return value. The timeout is 30 seconds.

DOM Monitoring

Monitors DOM changes and dialog events on the page via the Chrome DevTools Protocol.

startMonitor — Start Monitoring

await CAT.agent.dom.startMonitor(tabId);

Starts monitoring DOM changes and dialogs (alert/confirm/prompt) on the given tab.

stopMonitor — Stop Monitoring

const result = await CAT.agent.dom.stopMonitor(tabId);

Stops monitoring and returns the changes collected.

Return value, MonitorResult:

FieldTypeDescription
dialogsArray<{ type, message }>The list of dialogs
addedNodesArray<{ tag, id?, class?, role?, text }>A summary of newly added DOM nodes

peekMonitor — Check Monitoring Status

const status = await CAT.agent.dom.peekMonitor(tabId);

Non-destructively checks the current monitoring status.

Return value, MonitorStatus:

FieldTypeDescription
hasChangesbooleanWhether there are changes
dialogCountnumberThe number of dialogs
nodeCountnumberThe number of newly added nodes

Full Example

// ==UserScript==
// @name Automatic Form Filling
// @match https://example.com/form
// @grant CAT.agent.dom
// ==/UserScript==

// Wait for the form to load
await CAT.agent.dom.waitFor("form#signup", { timeout: 5000 });

// Fill in the form
await CAT.agent.dom.fill("input[name=username]", "test_user");
await CAT.agent.dom.fill("input[name=email]", "[email protected]");

// Check the agreement checkbox
await CAT.agent.dom.click("input[type=checkbox]#agree");

// Screenshot the filled-in result
await CAT.agent.dom.screenshot({
selector: "form#signup",
saveTo: "screenshots/form-filled.png"
});

// Click submit
const result = await CAT.agent.dom.click("button[type=submit]", { trusted: true });
if (result.navigated) {
console.log("Form submitted successfully, navigated to:", result.url);
}