Overview
Concrete singleton service that captures user clicks on page elements, collecting element metadata, selectors, and screenshots for the monitor-operation intervention type.
Description
The ElementCaptureService class enables the monitor-operation intervention by listening for user clicks on a target page. It validates the tab (rejects restricted pages like chrome://), sends a "start-capture" message to the content script, listens for click events via `chrome.runtime.onMessage`, and collects element metadata including selector, tagName, text content, and attributes. It also captures a cropped screenshot of the clicked element using `captureVisibleTabWithElementCrop`. The service is a singleton that enforces one capture session at a time.
Usage
- Start capture: `await ElementCaptureService.getInstance().startCapture({ tabId }, callback)`
- Callback receives `ElementCaptureEvent` with element details and screenshot.
Code Reference
Source Location
- Repository: AIPex
- File: packages/browser-runtime/src/intervention/element-capture.ts
- Lines: L17-80 (class, startCapture), L309 (file end)
- Support: packages/browser-runtime/src/tools/screenshot-helpers.ts (captureVisibleTabWithElementCrop)
Signature
export class ElementCaptureService {
static getInstance(): ElementCaptureService
async startCapture(
options: ElementCaptureOptions,
callback: (event: ElementCaptureEvent) => void
): Promise<void>
stopCapture(): void
}
interface ElementCaptureOptions {
tabId: number;
}
interface ElementCaptureEvent {
selector: string;
tagName: string;
text?: string;
id?: string;
classes?: string[];
attributes?: Record<string, string>;
screenshot?: string; // base64 data URL
}
Import
import { ElementCaptureService } from "@aipexstudio/browser-runtime";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| options.tabId |
number |
Yes |
Target tab to capture clicks from
|
| callback |
(event) => void |
Yes |
Called when user clicks an element
|
Outputs
| Name |
Type |
Description
|
| callback event |
ElementCaptureEvent |
Element selector, tagName, text, attributes, screenshot
|
Usage Examples
Monitor User Clicks
import { ElementCaptureService } from "@aipexstudio/browser-runtime";
const captureService = ElementCaptureService.getInstance();
await captureService.startCapture({ tabId: activeTabId }, (event) => {
console.log("User clicked:", event.tagName, event.text);
console.log("Selector:", event.selector);
if (event.screenshot) {
showElementPreview(event.screenshot);
}
});
// Stop capturing when done:
captureService.stopCapture();
Related Pages
Implements Principle
Uses Heuristics
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.