1+ /**
2+ * Chrome DevTools Protocol browser operations.
3+ *
4+ * Provides screenshots, target creation, JavaScript evaluation, ARIA/role
5+ * snapshots, DOM text, and selector lookup on top of the CDP socket helpers.
6+ */
17import { resolveIntegerOption } from "openclaw/plugin-sdk/number-runtime" ;
28import type { SsrFPolicy } from "../infra/net/ssrf.js" ;
39import {
@@ -22,6 +28,7 @@ export {
2228 isWebSocketUrl ,
2329} from "./cdp.helpers.js" ;
2430
31+ /** Normalize a reported CDP WebSocket URL against the configured CDP base URL. */
2532export function normalizeCdpWsUrl ( wsUrl : string , cdpUrl : string ) : string {
2633 const ws = new URL ( wsUrl ) ;
2734 const cdp = new URL ( cdpUrl ) ;
@@ -58,6 +65,7 @@ export function normalizeCdpWsUrl(wsUrl: string, cdpUrl: string): string {
5865 return ws . toString ( ) ;
5966}
6067
68+ /** Capture a PNG screenshot through CDP. */
6169export async function captureScreenshotPng ( opts : {
6270 wsUrl : string ;
6371 fullPage ?: boolean ;
@@ -71,6 +79,7 @@ export async function captureScreenshotPng(opts: {
7179 } ) ;
7280}
7381
82+ /** Capture a PNG or JPEG screenshot through CDP, optionally full-page. */
7483export async function captureScreenshot ( opts : {
7584 wsUrl : string ;
7685 fullPage ?: boolean ;
@@ -183,11 +192,13 @@ export async function captureScreenshot(opts: {
183192 ) ;
184193}
185194
195+ /** HTTP and WebSocket timeout options for CDP actions that need discovery. */
186196export type CdpActionTimeouts = {
187197 httpTimeoutMs ?: number ;
188198 handshakeTimeoutMs ?: number ;
189199} ;
190200
201+ /** Create a new browser target after applying navigation and CDP SSRF policy. */
191202export async function createTargetViaCdp ( opts : {
192203 cdpUrl : string ;
193204 url : string ;
@@ -302,6 +313,7 @@ async function prepareCdpPageSession(send: CdpSendFn, sessionId?: string): Promi
302313 await send ( "Runtime.runIfWaitingForDebugger" , undefined , sessionId ) . catch ( ( ) => { } ) ;
303314}
304315
316+ /** Runtime.evaluate remote-object subset used by CDP helpers. */
305317export type CdpRemoteObject = {
306318 type : string ;
307319 subtype ?: string ;
@@ -311,6 +323,7 @@ export type CdpRemoteObject = {
311323 preview ?: unknown ;
312324} ;
313325
326+ /** Exception details surfaced from CDP Runtime.evaluate. */
314327export type CdpExceptionDetails = {
315328 text ?: string ;
316329 lineNumber ?: number ;
@@ -319,6 +332,7 @@ export type CdpExceptionDetails = {
319332 stackTrace ?: unknown ;
320333} ;
321334
335+ /** Evaluate JavaScript in a CDP target and return by value when possible. */
322336export async function evaluateJavaScript ( opts : {
323337 wsUrl : string ;
324338 expression : string ;
@@ -349,6 +363,7 @@ export async function evaluateJavaScript(opts: {
349363 } ) ;
350364}
351365
366+ /** Normalized accessibility tree node returned by ARIA snapshots. */
352367export type AriaSnapshotNode = {
353368 ref : string ;
354369 role : string ;
@@ -359,9 +374,11 @@ export type AriaSnapshotNode = {
359374 depth : number ;
360375} ;
361376
377+ /** Prefix assigned to generated accessibility-node refs. */
362378export const AX_REF_PREFIX = "ax" ;
363379export const AX_REF_PATTERN = new RegExp ( `^${ AX_REF_PREFIX } \\d+$` ) ;
364380
381+ /** Raw accessibility node subset read from CDP Accessibility.getFullAXTree. */
365382export type RawAXNode = {
366383 nodeId ?: string ;
367384 role ?: { value ?: string } ;
@@ -386,6 +403,7 @@ function axValue(v: unknown): string {
386403 return "" ;
387404}
388405
406+ /** Format raw AX nodes into bounded ARIA snapshot nodes. */
389407export function formatAriaSnapshot ( nodes : RawAXNode [ ] , limit : number ) : AriaSnapshotNode [ ] {
390408 const byId = new Map < string , RawAXNode > ( ) ;
391409 for ( const n of nodes ) {
@@ -454,6 +472,7 @@ export function formatAriaSnapshot(nodes: RawAXNode[], limit: number): AriaSnaps
454472 return out ;
455473}
456474
475+ /** Capture an accessibility-tree snapshot through CDP. */
457476export async function snapshotAria ( opts : {
458477 wsUrl : string ;
459478 limit ?: number ;
@@ -474,6 +493,7 @@ export async function snapshotAria(opts: {
474493 ) ;
475494}
476495
496+ /** Role snapshot ref metadata used by agent-facing snapshots. */
477497export type CdpRoleRef = {
478498 role : string ;
479499 name ?: string ;
@@ -482,6 +502,7 @@ export type CdpRoleRef = {
482502 frameId ?: string ;
483503} ;
484504
505+ /** Options for CDP role snapshot extraction and compaction. */
485506export type CdpRoleSnapshotOptions = {
486507 interactive ?: boolean ;
487508 compact ?: boolean ;
@@ -919,6 +940,7 @@ async function buildCdpRoleSnapshot(params: {
919940 } ;
920941}
921942
943+ /** Build a role/name text snapshot with stable refs from CDP DOM and AX data. */
922944export async function snapshotRoleViaCdp ( opts : {
923945 wsUrl : string ;
924946 options ?: CdpRoleSnapshotOptions ;
@@ -958,6 +980,7 @@ export async function snapshotRoleViaCdp(opts: {
958980 ) ;
959981}
960982
983+ /** Capture a raw DOM snapshot through CDP. */
961984export async function snapshotDom ( opts : {
962985 wsUrl : string ;
963986 limit ?: number ;
@@ -1028,6 +1051,7 @@ export async function snapshotDom(opts: {
10281051 return { nodes : Array . isArray ( nodes ) ? ( nodes as DomSnapshotNode [ ] ) : [ ] } ;
10291052}
10301053
1054+ /** Simplified DOM node returned by DOM snapshot helpers. */
10311055export type DomSnapshotNode = {
10321056 ref : string ;
10331057 parentRef : string | null ;
@@ -1043,6 +1067,7 @@ export type DomSnapshotNode = {
10431067 value ?: string ;
10441068} ;
10451069
1070+ /** Extract visible DOM text from a CDP target. */
10461071export async function getDomText ( opts : {
10471072 wsUrl : string ;
10481073 format : "html" | "text" ;
@@ -1084,6 +1109,7 @@ export async function getDomText(opts: {
10841109 return { text } ;
10851110}
10861111
1112+ /** Query a selector in a CDP target and return matching node metadata. */
10871113export async function querySelector ( opts : {
10881114 wsUrl : string ;
10891115 selector : string ;
@@ -1139,6 +1165,7 @@ export async function querySelector(opts: {
11391165 return { matches : Array . isArray ( matches ) ? ( matches as QueryMatch [ ] ) : [ ] } ;
11401166}
11411167
1168+ /** Selector match metadata returned by querySelector. */
11421169export type QueryMatch = {
11431170 index : number ;
11441171 tag : string ;
0 commit comments