cdp-helper-contracts.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. export const DEFAULT_CDP_TIMEOUT_MS = 15_000;
  2. export const DRAG_PATH_STEPS = 8;
  3. export interface DOMNode {
  4. nodeId: number;
  5. backendNodeId: number;
  6. nodeName: string;
  7. nodeType: number;
  8. nodeValue?: string;
  9. attributes?: string[];
  10. children?: DOMNode[];
  11. shadowRoots?: DOMNode[];
  12. contentDocument?: DOMNode;
  13. frameId?: string;
  14. }
  15. export interface AXNode {
  16. nodeId: string;
  17. backendDOMNodeId?: number;
  18. role?: { value?: string };
  19. name?: { value?: string };
  20. description?: { value?: string };
  21. properties?: Array<{
  22. name: string;
  23. value?: { value?: unknown };
  24. }>;
  25. childIds?: string[];
  26. ignored?: boolean;
  27. }
  28. export interface DOMSnapshotResult {
  29. documents: Array<{
  30. documentURL?: number;
  31. frameId?: number;
  32. scrollOffsetX?: number;
  33. scrollOffsetY?: number;
  34. nodes: {
  35. backendNodeId?: number[];
  36. nodeName?: number[];
  37. nodeType?: number[];
  38. parentIndex?: number[];
  39. attributes?: Array<{ name: number; value: number }[]>;
  40. isClickable?: { index: number[] };
  41. };
  42. layout?: {
  43. nodeIndex?: number[];
  44. bounds?: number[][];
  45. styles?: number[][];
  46. paintOrders?: number[];
  47. clientRects?: number[][];
  48. scrollRects?: number[][];
  49. };
  50. }>;
  51. strings?: string[];
  52. }
  53. export interface CDPDragData {
  54. items: Array<{
  55. mimeType: string;
  56. data: string;
  57. title?: string;
  58. baseURL?: string;
  59. }>;
  60. files?: string[];
  61. dragOperationsMask: number;
  62. }
  63. export const DRAG_CANCEL_DATA: CDPDragData = {
  64. items: [],
  65. dragOperationsMask: 0xffff,
  66. };
  67. export interface CDPDragGestureResult {
  68. mode: 'pointer' | 'html5';
  69. dragStarted: boolean;
  70. dropDispatched: boolean;
  71. }
  72. export interface CDPDragGestureOptions {
  73. /** Equivalent to Playwright's Progress timeout for awaiting Input.dragIntercepted. */
  74. interceptTimeoutMs?: number;
  75. /** Bound each CDP command issued by the gesture. */
  76. commandTimeoutMs?: number;
  77. /** Cancel the gesture while still allowing bounded pointer cleanup. */
  78. signal?: AbortSignal;
  79. }
  80. export interface CDPFileUploadOptions {
  81. timeoutMs?: number;
  82. signal?: AbortSignal;
  83. }
  84. export interface CDPFileUploadResult {
  85. filesAttached: number;
  86. chooserOpened: boolean;
  87. }
  88. /** Scope a backend-node read to the CDP session that owns the node. */
  89. export interface CDPBackendNodeInspectionOptions {
  90. timeoutMs?: number;
  91. signal?: AbortSignal;
  92. commandSessionId?: string;
  93. }
  94. export interface CDPEditableState {
  95. tag: string;
  96. role: string;
  97. contentEditable: boolean;
  98. editable: boolean;
  99. focused: boolean;
  100. textLength: number;
  101. fingerprint: string;
  102. /** Computed in-page only when an expected value is supplied; editable content is never returned. */
  103. matchesExpected?: boolean;
  104. /** Computed in-page only when expected text is supplied; editable content is never returned. */
  105. containsExpected?: boolean;
  106. }
  107. export interface CDPFocusedEditableState extends CDPEditableState {
  108. /** Whether the deep active element is the activation host itself or one of its descendants. */
  109. withinTarget: boolean;
  110. }
  111. export interface CDPRenderedState {
  112. textLength: number;
  113. fingerprint: string;
  114. /** Computed in-page only when an expected value is supplied; rendered text is never returned. */
  115. matchesExpected?: boolean;
  116. /** Computed in-page only when expected text is supplied; rendered text is never returned. */
  117. containsExpected?: boolean;
  118. }
  119. export interface CDPConsoleDiagnosticEntry {
  120. sequence: number;
  121. source: 'console' | 'exception';
  122. level: string;
  123. timestamp: number;
  124. message: string;
  125. url?: string;
  126. lineNumber?: number;
  127. columnNumber?: number;
  128. stack?: string[];
  129. }
  130. export type CDPConsoleDiagnosticLevelFilter =
  131. | 'debug'
  132. | 'info'
  133. | 'log'
  134. | 'warn'
  135. | 'warning'
  136. | 'error';
  137. export interface CDPConsoleDiagnosticsOptions {
  138. limit?: number;
  139. levels?: readonly CDPConsoleDiagnosticLevelFilter[];
  140. filter?: string;
  141. }
  142. export interface CDPConsoleDiagnosticsPage {
  143. entries: CDPConsoleDiagnosticEntry[];
  144. totalEntries: number;
  145. returnedEntries: number;
  146. truncated: boolean;
  147. }
  148. export type CDPNetworkDiagnosticOutcome =
  149. | 'pending'
  150. | 'success'
  151. | 'redirect'
  152. | 'http-error'
  153. | 'failed';
  154. export type CDPNetworkDiagnosticStatusFilter =
  155. | 'pending'
  156. | 'success'
  157. | 'failed'
  158. | '2xx'
  159. | '3xx'
  160. | '4xx'
  161. | '5xx';
  162. export type CDPNetworkDiagnosticResourceType =
  163. | 'document'
  164. | 'stylesheet'
  165. | 'image'
  166. | 'media'
  167. | 'font'
  168. | 'script'
  169. | 'xhr'
  170. | 'fetch'
  171. | 'eventsource'
  172. | 'manifest'
  173. | 'preflight'
  174. | 'other';
  175. export interface CDPNetworkDiagnosticEntry {
  176. sequence: number;
  177. method: string;
  178. url: string;
  179. resourceType: CDPNetworkDiagnosticResourceType;
  180. outcome: CDPNetworkDiagnosticOutcome;
  181. timestamp: number;
  182. status?: number;
  183. statusText?: string;
  184. mimeType?: string;
  185. failureReason?: string;
  186. durationMs?: number;
  187. }
  188. export interface CDPNetworkDiagnosticsOptions {
  189. limit?: number;
  190. status?: readonly CDPNetworkDiagnosticStatusFilter[];
  191. resourceTypes?: readonly CDPNetworkDiagnosticResourceType[];
  192. filter?: string;
  193. afterSequence?: number;
  194. }
  195. export interface CDPNetworkDiagnosticsPage {
  196. entries: CDPNetworkDiagnosticEntry[];
  197. lastSequence: number;
  198. totalEntries: number;
  199. returnedEntries: number;
  200. truncated: boolean;
  201. }
  202. export type CDPHoverTargetNameSource =
  203. | 'aria-label'
  204. | 'aria-labelledby'
  205. | 'title'
  206. | 'svg-title'
  207. | 'alt'
  208. | 'text'
  209. | 'none';
  210. export interface CDPHoverSemanticSnapshot {
  211. target: {
  212. name: string;
  213. source: CDPHoverTargetNameSource;
  214. supportingText: string;
  215. };
  216. nearby: Array<{
  217. fingerprint: string;
  218. text: string;
  219. role: string;
  220. linked: boolean;
  221. distance: number;
  222. }>;
  223. }
  224. export type CDPTextInputErrorCode = 'INPUT_FOCUS_REJECTED';
  225. export class CDPTextInputError extends Error {
  226. readonly code: CDPTextInputErrorCode;
  227. constructor(code: CDPTextInputErrorCode, message: string) {
  228. super(message);
  229. this.name = 'CDPTextInputError';
  230. this.code = code;
  231. }
  232. }
  233. export type CDPDragErrorCode = 'DRAG_DATA_UNAVAILABLE' | 'DROP_DISPATCH_FAILED';
  234. export class CDPDragError extends Error {
  235. readonly code: CDPDragErrorCode;
  236. constructor(code: CDPDragErrorCode, message: string) {
  237. super(message);
  238. this.name = 'CDPDragError';
  239. this.code = code;
  240. }
  241. }
  242. /** Required computed styles, kept deliberately small for complex pages. */
  243. export const REQUIRED_COMPUTED_STYLES = [
  244. 'display',
  245. 'visibility',
  246. 'opacity',
  247. 'overflow',
  248. 'overflow-x',
  249. 'overflow-y',
  250. 'cursor',
  251. 'pointer-events',
  252. 'position',
  253. ];
  254. export const INTERACTIVE_ROLES = new Set([
  255. 'button',
  256. 'link',
  257. 'textbox',
  258. 'searchbox',
  259. 'checkbox',
  260. 'radio',
  261. 'switch',
  262. 'combobox',
  263. 'listbox',
  264. 'spinbutton',
  265. 'slider',
  266. 'option',
  267. 'menuitem',
  268. 'menuitemcheckbox',
  269. 'menuitemradio',
  270. 'tab',
  271. 'treeitem',
  272. 'gridcell',
  273. 'row',
  274. 'cell',
  275. ]);
  276. export const INTERACTIVE_TAGS = new Set([
  277. 'button',
  278. 'input',
  279. 'select',
  280. 'textarea',
  281. 'a',
  282. 'details',
  283. 'summary',
  284. 'option',
  285. 'optgroup',
  286. 'video',
  287. 'audio',
  288. ]);
  289. export const SKIP_TAGS = new Set(['script', 'style', 'noscript', 'meta', 'link', 'head']);
  290. export const CONTAINER_TAGS = new Set(['html', 'body', '#document']);
  291. export const SVG_ELEMENTS = new Set([
  292. 'path',
  293. 'rect',
  294. 'g',
  295. 'circle',
  296. 'ellipse',
  297. 'line',
  298. 'polyline',
  299. 'polygon',
  300. 'use',
  301. 'defs',
  302. 'clipPath',
  303. 'mask',
  304. 'pattern',
  305. 'image',
  306. 'text',
  307. 'tspan',
  308. ]);
  309. export interface SnapshotNode {
  310. bounds: { x: number; y: number; width: number; height: number } | null;
  311. coordinateSpace: 'main-frame-viewport' | 'frame-local' | 'unavailable';
  312. computedStyles: Record<string, string>;
  313. isClickable: boolean;
  314. paintOrder: number | null;
  315. /** Owning CDP frame, retained for canonical semantic identity. */
  316. frameId?: string;
  317. }