MCPcopy Create free account
hub / github.com/aeroxy/chrome-devtools-cli / evaluate

Function evaluate

src/commands/evaluate.rs:10–85  ·  view source on GitHub ↗

Evaluate a JavaScript expression in the page and return the result.

(
    client: &mut CdpClient,
    session_id: &str,
    expression: &str,
    format: OutputFormat,
    output: Option<&str>,
    track_navigation: bool,
)

Source from the content-addressed store, hash-verified

8
9/// Evaluate a JavaScript expression in the page and return the result.
10pub async fn evaluate(
11 client: &mut CdpClient,
12 session_id: &str,
13 expression: &str,
14 format: OutputFormat,
15 output: Option<&str>,
16 track_navigation: bool,
17) -> Result<CommandResult> {
18 // Handle JavaScript dialogs (alert, confirm, prompt) during evaluation.
19 // `client.dialog_action` must be set to "accept", "dismiss", or a prompt
20 // response string before calling this function. The underlying
21 // `send_to_target` call will then automatically handle any
22 // `Page.javascriptDialogOpening` events that occur.
23
24 let initial_url = if track_navigation {
25 Some(client.current_url(session_id).await?)
26 } else {
27 None
28 };
29
30 let result = client
31 .send_to_target(
32 session_id,
33 "Runtime.evaluate",
34 json!({
35 "expression": expression,
36 "returnByValue": true,
37 "awaitPromise": true,
38 }),
39 )
40 .await?;
41
42 if let Some(exception) = result.get("exceptionDetails") {
43 let text = exception["text"].as_str().unwrap_or("Unknown error");
44 let desc = exception["exception"]["description"]
45 .as_str()
46 .unwrap_or(text);
47 anyhow::bail!("{desc}");
48 }
49
50 let value = &result["result"];
51 let val_type = value["type"].as_str().unwrap_or("undefined");
52
53 let output_hint = if format.is_text() {
54 let text = match val_type {
55 "undefined" => "undefined".to_string(),
56 "string" => value["value"].as_str().unwrap_or("").to_string(),
57 _ => {
58 if let Some(v) = value.get("value") {
59 serde_json::to_string_pretty(v)?
60 } else {
61 value["description"].as_str().unwrap_or("").to_string()
62 }
63 }
64 };
65
66 text
67 } else {

Callers 4

run_directFunction · 0.85
inner_executeFunction · 0.85
run_scriptFunction · 0.85
run_adapterFunction · 0.85

Calls 6

format_structuredFunction · 0.85
current_urlMethod · 0.80
send_to_targetMethod · 0.80
is_textMethod · 0.80
save_outputMethod · 0.80

Tested by

no test coverage detected