chrome-devtools 1.5.1

Profile-aware CLI for running Chrome DevTools MCP with isolated Chrome user data directories
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use crate::cdp::CdpClient;
use serde_json::Value;
use std::thread;
use std::time::Duration;

const FRAME_SEPARATOR: &str = ">>>";

#[derive(Clone, Debug)]
pub(crate) struct FrameScope {
    session_id: Option<String>,
    context_id: Option<i64>,
    label: String,
}

impl FrameScope {
    fn top() -> Self {
        Self {
            session_id: None,
            context_id: None,
            label: "top frame".to_string(),
        }
    }

    fn child(&self, selector: &str, session_id: Option<String>, context_id: Option<i64>) -> Self {
        let path = match self.label.strip_prefix("frame ") {
            Some(path) => format!("{path} {FRAME_SEPARATOR} {selector}"),
            None => selector.to_string(),
        };
        Self {
            session_id,
            context_id,
            label: format!("frame {path}"),
        }
    }
}

struct Hop {
    scope: FrameScope,
    object_id: String,
}

pub(crate) struct Located {
    scope: FrameScope,
    object_id: String,
    hops: Vec<Hop>,
}

impl Located {
    pub(crate) fn release(&self, client: &mut CdpClient) {
        for hop in &self.hops {
            release_object(client, &hop.scope, &hop.object_id);
        }
        release_object(client, &self.scope, &self.object_id);
    }
}

fn release_object(client: &mut CdpClient, scope: &FrameScope, object_id: &str) {
    let _ = client.call_in(
        scope.session_id.as_deref(),
        serde_json::json!({
            "method": "Runtime.releaseObject",
            "params": { "objectId": object_id }
        }),
    );
}

fn exception_text(result: &Value) -> Option<String> {
    let details = result.get("exceptionDetails")?;
    let text = details
        .get("exception")
        .and_then(|exception| exception.get("description"))
        .and_then(|description| description.as_str())
        .or_else(|| details.get("text").and_then(|text| text.as_str()))
        .unwrap_or("unknown evaluation error");
    Some(format!("expression threw: {text}"))
}

fn evaluate_in(
    client: &mut CdpClient,
    scope: &FrameScope,
    expression: &str,
    by_value: bool,
) -> Result<Value, String> {
    let mut params = serde_json::json!({
        "expression": expression,
        "returnByValue": by_value
    });
    if let Some(context_id) = scope.context_id {
        params["contextId"] = serde_json::json!(context_id);
    }
    let response = client.call_in(
        scope.session_id.as_deref(),
        serde_json::json!({ "method": "Runtime.evaluate", "params": params }),
    )?;
    let result = response
        .get("result")
        .ok_or_else(|| "Runtime.evaluate returned no result".to_string())?;
    if let Some(error) = exception_text(result) {
        return Err(error);
    }
    Ok(result.get("result").cloned().unwrap_or(Value::Null))
}

pub(crate) fn call_on(
    client: &mut CdpClient,
    scope: &FrameScope,
    object_id: &str,
    function: &str,
) -> Result<Value, String> {
    let response = client.call_in(
        scope.session_id.as_deref(),
        serde_json::json!({
            "method": "Runtime.callFunctionOn",
            "params": {
                "objectId": object_id,
                "functionDeclaration": function,
                "returnByValue": true
            }
        }),
    )?;
    let result = response
        .get("result")
        .ok_or_else(|| "Runtime.callFunctionOn returned no result".to_string())?;
    if let Some(error) = exception_text(result) {
        return Err(error);
    }
    Ok(result
        .get("result")
        .and_then(|inner| inner.get("value"))
        .cloned()
        .unwrap_or(Value::Null))
}

fn query_object_id(
    client: &mut CdpClient,
    scope: &FrameScope,
    selector: &str,
) -> Result<Option<String>, String> {
    let selector_json = serde_json::json!(selector).to_string();
    let result = evaluate_in(
        client,
        scope,
        &format!("document.querySelector({selector_json})"),
        false,
    )?;
    Ok(result
        .get("objectId")
        .and_then(|object_id| object_id.as_str())
        .map(str::to_string))
}

fn frame_summary(client: &mut CdpClient, scope: &FrameScope) -> String {
    let value = evaluate_in(
        client,
        scope,
        "(() => ({ url: location.href, iframes: Array.from(document.querySelectorAll('iframe,frame')).slice(0, 8).map((f) => f.tagName.toLowerCase() + (f.id ? '#' + f.id : f.getAttribute('name') ? '[name=' + f.getAttribute('name') + ']' : '[src=' + (f.getAttribute('src') || '') + ']')) }))()",
        true,
    )
    .ok()
    .and_then(|result| result.get("value").cloned());
    let Some(value) = value else {
        return scope.label.clone();
    };
    let url = value
        .get("url")
        .and_then(|url| url.as_str())
        .unwrap_or("unknown url");
    let iframes = value
        .get("iframes")
        .and_then(|iframes| iframes.as_array())
        .map(|iframes| {
            iframes
                .iter()
                .filter_map(|iframe| iframe.as_str())
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();
    if iframes.is_empty() {
        format!("{} (url {url}, no iframes)", scope.label)
    } else {
        format!(
            "{} (url {url}, iframes here: {})",
            scope.label,
            iframes.join(", ")
        )
    }
}

fn missing_element(client: &mut CdpClient, scope: &FrameScope, selector: &str) -> String {
    format!(
        "no element matches selector: {selector} in {}",
        frame_summary(client, scope)
    )
}

pub(crate) fn describe_element(
    client: &mut CdpClient,
    scope: &FrameScope,
    object_id: &str,
) -> String {
    let state = call_on(
        client,
        scope,
        object_id,
        "function() { const style = getComputedStyle(this); const rect = this.getBoundingClientRect(); const hidden = style.display === 'none' || style.visibility === 'hidden' || (rect.width === 0 && rect.height === 0); return { tag: this.tagName.toLowerCase(), hidden, disabled: !!this.disabled, readOnly: !!this.readOnly }; }",
    )
    .unwrap_or(Value::Null);
    let tag = state
        .get("tag")
        .and_then(|tag| tag.as_str())
        .unwrap_or("element");
    let flag = |name: &str| state.get(name).and_then(|value| value.as_bool()) == Some(true);
    let reason = if flag("hidden") {
        ", hidden (display:none, visibility:hidden or zero size)"
    } else if flag("disabled") {
        ", disabled"
    } else if flag("readOnly") {
        ", read-only"
    } else {
        ""
    };
    format!("matched <{tag}>{reason}")
}

pub(crate) fn failure_context(
    client: &mut CdpClient,
    located: &Located,
    selector: &str,
    reason: &str,
) -> String {
    let element = describe_element(client, &located.scope, &located.object_id);
    format!(
        "{reason}: {selector} ({element}; in {})",
        frame_summary(client, &located.scope)
    )
}

fn enter_frame(
    client: &mut CdpClient,
    parent: &FrameScope,
    frame_id: &str,
    selector: &str,
) -> Result<FrameScope, String> {
    client.call_in(
        None,
        serde_json::json!({ "method": "Target.getTargets", "params": {} }),
    )?;
    let attached = client.call_in(
        None,
        serde_json::json!({
            "method": "Target.attachToTarget",
            "params": { "targetId": frame_id, "flatten": true }
        }),
    );
    match attached {
        Ok(response) => {
            let session_id = response
                .get("result")
                .and_then(|result| result.get("sessionId"))
                .and_then(|session_id| session_id.as_str())
                .ok_or_else(|| "Target.attachToTarget returned no sessionId".to_string())?;
            Ok(parent.child(selector, Some(session_id.to_string()), None))
        }
        Err(error) if error.contains("No target with given id found") => {
            let world = client.call_in(
                parent.session_id.as_deref(),
                serde_json::json!({
                    "method": "Page.createIsolatedWorld",
                    "params": {
                        "frameId": frame_id,
                        "worldName": "chrome-devtools",
                        "grantUniveralAccess": true
                    }
                }),
            )?;
            let context_id = world
                .get("result")
                .and_then(|result| result.get("executionContextId"))
                .and_then(|context_id| context_id.as_i64())
                .ok_or_else(|| "Page.createIsolatedWorld returned no context id".to_string())?;
            Ok(parent.child(selector, parent.session_id.clone(), Some(context_id)))
        }
        Err(error) => Err(error),
    }
}

pub(crate) fn locate(client: &mut CdpClient, selector: &str) -> Result<Located, String> {
    let parts = selector
        .split(FRAME_SEPARATOR)
        .map(str::trim)
        .collect::<Vec<_>>();
    if parts.iter().any(|part| part.is_empty()) {
        return Err(format!("empty selector segment in: {selector}"));
    }
    let (target_selector, frame_selectors) = parts
        .split_last()
        .ok_or_else(|| "selector is empty".to_string())?;
    let mut scope = FrameScope::top();
    let mut hops = Vec::new();
    for frame_selector in frame_selectors {
        let object_id = match query_object_id(client, &scope, frame_selector)? {
            Some(object_id) => object_id,
            None => {
                let message = format!(
                    "no iframe matches selector: {frame_selector} in {}",
                    frame_summary(client, &scope)
                );
                release_hops(client, &hops);
                return Err(message);
            }
        };
        let described = client.call_in(
            scope.session_id.as_deref(),
            serde_json::json!({
                "method": "DOM.describeNode",
                "params": { "objectId": object_id }
            }),
        );
        let node = described
            .as_ref()
            .ok()
            .and_then(|response| response.get("result").and_then(|result| result.get("node")));
        let frame_id = node
            .and_then(|node| node.get("frameId"))
            .and_then(|frame_id| frame_id.as_str());
        let Some(frame_id) = frame_id else {
            let tag = node
                .and_then(|node| node.get("nodeName"))
                .and_then(|name| name.as_str())
                .unwrap_or("element");
            release_object(client, &scope, &object_id);
            release_hops(client, &hops);
            return Err(format!(
                "selector {frame_selector} matches <{}>, not an iframe, in {}",
                tag.to_lowercase(),
                scope.label
            ));
        };
        let child = match enter_frame(client, &scope, frame_id, frame_selector) {
            Ok(child) => child,
            Err(error) => {
                release_object(client, &scope, &object_id);
                release_hops(client, &hops);
                return Err(format!(
                    "could not enter iframe {frame_selector} in {}: {error}",
                    scope.label
                ));
            }
        };
        hops.push(Hop {
            scope: scope.clone(),
            object_id,
        });
        scope = child;
    }
    match query_object_id(client, &scope, target_selector)? {
        Some(object_id) => Ok(Located {
            scope,
            object_id,
            hops,
        }),
        None => {
            let message = missing_element(client, &scope, target_selector);
            release_hops(client, &hops);
            Err(message)
        }
    }
}

fn release_hops(client: &mut CdpClient, hops: &[Hop]) {
    for hop in hops {
        release_object(client, &hop.scope, &hop.object_id);
    }
}

pub(crate) fn click_point(client: &mut CdpClient, located: &Located) -> Result<(f64, f64), String> {
    for hop in &located.hops {
        call_on(
            client,
            &hop.scope,
            &hop.object_id,
            "function() { this.scrollIntoView({ block: 'center', inline: 'center' }); }",
        )?;
    }
    call_on(
        client,
        &located.scope,
        &located.object_id,
        "function() { this.scrollIntoView({ block: 'center', inline: 'center' }); }",
    )?;
    let mut point = measure_point(client, located)?;
    if located.hops.is_empty() {
        return Ok(point);
    }
    for _ in 0..40 {
        thread::sleep(Duration::from_millis(50));
        let next = measure_point(client, located)?;
        let settled = (next.0 - point.0).abs() < 0.5 && (next.1 - point.1).abs() < 0.5;
        point = next;
        if settled {
            return Ok(point);
        }
    }
    Err("the element kept moving while the frames scrolled".to_string())
}

fn measure_point(client: &mut CdpClient, located: &Located) -> Result<(f64, f64), String> {
    let center = call_on(
        client,
        &located.scope,
        &located.object_id,
        "function() { const r = this.getBoundingClientRect(); return { x: r.x + r.width / 2, y: r.y + r.height / 2 }; }",
    )?;
    let mut x = center
        .get("x")
        .and_then(|x| x.as_f64())
        .ok_or_else(|| "selector center has no x".to_string())?;
    let mut y = center
        .get("y")
        .and_then(|y| y.as_f64())
        .ok_or_else(|| "selector center has no y".to_string())?;
    for hop in located.hops.iter().rev() {
        let origin = call_on(
            client,
            &hop.scope,
            &hop.object_id,
            "function() { const r = this.getBoundingClientRect(); const s = getComputedStyle(this); return { x: r.x + this.clientLeft + parseFloat(s.paddingLeft || '0'), y: r.y + this.clientTop + parseFloat(s.paddingTop || '0') }; }",
        )?;
        x += origin.get("x").and_then(|x| x.as_f64()).unwrap_or(0.0);
        y += origin.get("y").and_then(|y| y.as_f64()).unwrap_or(0.0);
    }
    Ok((x, y))
}

pub(crate) fn focus(client: &mut CdpClient, located: &Located) -> Result<bool, String> {
    let focused = call_on(
        client,
        &located.scope,
        &located.object_id,
        "function() { this.scrollIntoView({ block: 'center', inline: 'center' }); this.focus(); return this.ownerDocument.activeElement === this; }",
    )?;
    Ok(focused.as_bool() == Some(true))
}

pub(crate) fn is_file_input(client: &mut CdpClient, located: &Located) -> Result<bool, String> {
    let kind = call_on(
        client,
        &located.scope,
        &located.object_id,
        "function() { return this.tagName === 'INPUT' && this.type === 'file'; }",
    )?;
    Ok(kind.as_bool() == Some(true))
}

pub(crate) fn set_files(
    client: &mut CdpClient,
    located: &Located,
    files: &[String],
) -> Result<(), String> {
    client.call_in(
        located.scope.session_id.as_deref(),
        serde_json::json!({
            "method": "DOM.setFileInputFiles",
            "params": {
                "objectId": located.object_id,
                "files": files
            }
        }),
    )?;
    Ok(())
}

pub(crate) fn not_file_input(client: &mut CdpClient, located: &Located, selector: &str) -> String {
    let element = describe_element(client, &located.scope, &located.object_id);
    format!(
        "selector is not an input[type=file]: {selector} ({element}; in {})",
        frame_summary(client, &located.scope)
    )
}