viewpoint_cdp/protocol/dom_snapshot/
mod.rs

1//! `DOMSnapshot` domain types.
2//!
3//! This domain facilitates obtaining document snapshots with DOM, layout, and style information.
4
5use serde::{Deserialize, Serialize};
6
7// ============================================================================
8// DOMSnapshot.captureSnapshot
9// ============================================================================
10
11/// Parameters for DOMSnapshot.captureSnapshot.
12#[derive(Debug, Clone, Serialize, Default)]
13#[serde(rename_all = "camelCase")]
14pub struct CaptureSnapshotParams {
15    /// Whitelist of computed styles to return.
16    pub computed_styles: Vec<String>,
17    /// Whether to include DOM rectangles (for layout/CSS painting).
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub include_dom_rects: Option<bool>,
20    /// Whether to include blended background colors.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub include_blended_background_colors: Option<bool>,
23    /// Whether to include text color opacities.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub include_text_color_opacities: Option<bool>,
26    /// Whether to include paint orders.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub include_paint_order: Option<bool>,
29}
30
31impl CaptureSnapshotParams {
32    /// Create new capture snapshot params with default styles.
33    pub fn new() -> Self {
34        Self {
35            computed_styles: vec![
36                "display".to_string(),
37                "visibility".to_string(),
38                "opacity".to_string(),
39            ],
40            include_dom_rects: Some(true),
41            ..Default::default()
42        }
43    }
44
45    /// Create params with custom computed styles.
46    pub fn with_styles(styles: Vec<String>) -> Self {
47        Self {
48            computed_styles: styles,
49            ..Default::default()
50        }
51    }
52
53    /// Set whether to include DOM rectangles.
54    #[must_use]
55    pub fn include_dom_rects(mut self, include: bool) -> Self {
56        self.include_dom_rects = Some(include);
57        self
58    }
59
60    /// Set whether to include blended background colors.
61    #[must_use]
62    pub fn include_blended_background_colors(mut self, include: bool) -> Self {
63        self.include_blended_background_colors = Some(include);
64        self
65    }
66}
67
68/// Result of DOMSnapshot.captureSnapshot.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70#[serde(rename_all = "camelCase")]
71pub struct CaptureSnapshotResult {
72    /// The document snapshots for all documents in the page.
73    pub documents: Vec<DocumentSnapshot>,
74    /// Shared strings.
75    pub strings: Vec<String>,
76}
77
78/// A document snapshot.
79#[derive(Debug, Clone, Serialize, Deserialize)]
80#[serde(rename_all = "camelCase")]
81pub struct DocumentSnapshot {
82    /// Document URL index into the `strings` array.
83    pub document_url: i32,
84    /// Document title index into the `strings` array.
85    pub title: i32,
86    /// Base URL index into the `strings` array.
87    pub base_url: i32,
88    /// Content language index into the `strings` array.
89    pub content_language: i32,
90    /// Encoding name index into the `strings` array.
91    pub encoding_name: i32,
92    /// Public ID index into the `strings` array.
93    pub public_id: i32,
94    /// System ID index into the `strings` array.
95    pub system_id: i32,
96    /// Frame ID index into the `strings` array.
97    pub frame_id: i32,
98    /// Node tree snapshot.
99    pub nodes: NodeTreeSnapshot,
100    /// Layout tree snapshot.
101    pub layout: LayoutTreeSnapshot,
102    /// Text box snapshot.
103    pub text_boxes: TextBoxSnapshot,
104    /// Scroll offset X.
105    #[serde(default)]
106    pub scroll_offset_x: Option<f64>,
107    /// Scroll offset Y.
108    #[serde(default)]
109    pub scroll_offset_y: Option<f64>,
110    /// Document content width.
111    #[serde(default)]
112    pub content_width: Option<f64>,
113    /// Document content height.
114    #[serde(default)]
115    pub content_height: Option<f64>,
116}
117
118/// Node tree snapshot.
119#[derive(Debug, Clone, Serialize, Deserialize)]
120#[serde(rename_all = "camelCase")]
121pub struct NodeTreeSnapshot {
122    /// Parent node index.
123    #[serde(default)]
124    pub parent_index: Option<Vec<i32>>,
125    /// Node type.
126    #[serde(default)]
127    pub node_type: Option<Vec<i32>>,
128    /// Shadow root type (null, open, closed).
129    #[serde(default)]
130    pub shadow_root_type: Option<RareStringData>,
131    /// Node name index into the `strings` array.
132    #[serde(default)]
133    pub node_name: Option<Vec<i32>>,
134    /// Node value index into the `strings` array.
135    #[serde(default)]
136    pub node_value: Option<Vec<i32>>,
137    /// Backend node ID.
138    #[serde(default)]
139    pub backend_node_id: Option<Vec<i32>>,
140    /// Attributes of Element nodes.
141    #[serde(default)]
142    pub attributes: Option<Vec<ArrayOfStrings>>,
143    /// Text value index (for text nodes).
144    #[serde(default)]
145    pub text_value: Option<RareStringData>,
146    /// Input value index (for input elements).
147    #[serde(default)]
148    pub input_value: Option<RareStringData>,
149    /// Input checked state (for checkbox/radio).
150    #[serde(default)]
151    pub input_checked: Option<RareBooleanData>,
152    /// Option selected state.
153    #[serde(default)]
154    pub option_selected: Option<RareBooleanData>,
155    /// Document content document index.
156    #[serde(default)]
157    pub content_document_index: Option<RareIntegerData>,
158    /// Type of a pseudo element node (before, after, backdrop).
159    #[serde(default)]
160    pub pseudo_type: Option<RareStringData>,
161    /// Pseudo element identifier for this node (CSS `::marker`).
162    #[serde(default)]
163    pub pseudo_identifier: Option<RareStringData>,
164    /// Whether this DOM node responds to mouse clicks.
165    #[serde(default)]
166    pub is_clickable: Option<RareBooleanData>,
167    /// The URL of the script (if any) that generates this node.
168    #[serde(default)]
169    pub current_source_url: Option<RareStringData>,
170    /// Origin URL of the script (if any) that generates this node.
171    #[serde(default)]
172    pub origin_url: Option<RareStringData>,
173}
174
175/// Layout tree snapshot.
176#[derive(Debug, Clone, Serialize, Deserialize)]
177#[serde(rename_all = "camelCase")]
178pub struct LayoutTreeSnapshot {
179    /// Index of the corresponding node in the `NodeTreeSnapshot`.
180    pub node_index: Vec<i32>,
181    /// Style index array into `computedStyles` array.
182    pub styles: Vec<ArrayOfStrings>,
183    /// CSS box model bounds (x, y, width, height).
184    pub bounds: Vec<Rectangle>,
185    /// Text content of text nodes.
186    pub text: Vec<i32>,
187    /// Stacking contexts.
188    #[serde(default)]
189    pub stacking_contexts: Option<RareBooleanData>,
190    /// Paint orders.
191    #[serde(default)]
192    pub paint_orders: Option<Vec<i32>>,
193    /// Offset rects.
194    #[serde(default)]
195    pub offset_rects: Option<Vec<Rectangle>>,
196    /// Scroll rects.
197    #[serde(default)]
198    pub scroll_rects: Option<Vec<Rectangle>>,
199    /// Client rects.
200    #[serde(default)]
201    pub client_rects: Option<Vec<Rectangle>>,
202    /// Blended background colors.
203    #[serde(default)]
204    pub blended_background_colors: Option<Vec<i32>>,
205    /// Text color opacities.
206    #[serde(default)]
207    pub text_color_opacities: Option<Vec<f64>>,
208}
209
210/// Text box snapshot.
211#[derive(Debug, Clone, Serialize, Deserialize)]
212#[serde(rename_all = "camelCase")]
213pub struct TextBoxSnapshot {
214    /// Index of the layout tree node that owns this box.
215    pub layout_index: Vec<i32>,
216    /// Text box bounds (x, y, width, height).
217    pub bounds: Vec<Rectangle>,
218    /// Start offset of text in the text value.
219    pub start: Vec<i32>,
220    /// Length of the text box substring in the text value.
221    pub length: Vec<i32>,
222}
223
224/// Rectangle coordinates (x, y, width, height).
225pub type Rectangle = Vec<f64>;
226
227/// Array of string indices.
228pub type ArrayOfStrings = Vec<i32>;
229
230/// Data for rare string attributes.
231#[derive(Debug, Clone, Serialize, Deserialize)]
232pub struct RareStringData {
233    /// Index of the data item.
234    pub index: Vec<i32>,
235    /// String value.
236    pub value: Vec<i32>,
237}
238
239/// Data for rare boolean attributes.
240#[derive(Debug, Clone, Serialize, Deserialize)]
241pub struct RareBooleanData {
242    /// Index of the data item.
243    pub index: Vec<i32>,
244}
245
246/// Data for rare integer attributes.
247#[derive(Debug, Clone, Serialize, Deserialize)]
248pub struct RareIntegerData {
249    /// Index of the data item.
250    pub index: Vec<i32>,
251    /// Integer value.
252    pub value: Vec<i32>,
253}
254
255// ============================================================================
256// DOMSnapshot.disable
257// ============================================================================
258
259/// Parameters for DOMSnapshot.disable (empty).
260#[derive(Debug, Clone, Serialize, Default)]
261pub struct DisableParams {}
262
263// ============================================================================
264// DOMSnapshot.enable
265// ============================================================================
266
267/// Parameters for DOMSnapshot.enable (empty).
268#[derive(Debug, Clone, Serialize, Default)]
269pub struct EnableParams {}