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

Function navigate

src/commands/navigate.rs:9–69  ·  view source on GitHub ↗

Navigate to a URL, go back/forward in history, or reload the page.

(
    client: &mut CdpClient,
    session_id: &str,
    url: Option<&str>,
    back: bool,
    forward: bool,
    reload: bool,
    extra_headers: Option<&str>,
    output: Option<&str>,
)

Source from the content-addressed store, hash-verified

7
8/// Navigate to a URL, go back/forward in history, or reload the page.
9pub async fn navigate(
10 client: &mut CdpClient,
11 session_id: &str,
12 url: Option<&str>,
13 back: bool,
14 forward: bool,
15 reload: bool,
16 extra_headers: Option<&str>,
17 output: Option<&str>,
18) -> Result<CommandResult> {
19 // Validate navigation intent before mutating session state
20 let intent_count = [back, forward, reload, url.is_some()]
21 .iter()
22 .filter(|&&b| b)
23 .count();
24 if intent_count == 0 {
25 bail!("URL required (or use --back, --forward, --reload)");
26 }
27 if intent_count > 1 {
28 bail!("Conflicting navigation intents: only one of URL, --back, --forward, or --reload can be specified");
29 }
30
31 super::pages::apply_extra_headers(client, session_id, extra_headers).await?;
32
33 let navigate_result = async {
34 if back {
35 return go_back(client, session_id, output).await;
36 }
37 if forward {
38 return go_forward(client, session_id, output).await;
39 }
40 if reload {
41 return do_reload(client, session_id, output).await;
42 }
43
44 let url = url.unwrap(); // safe: validated above
45
46 let result = client
47 .send_to_target(session_id, "Page.navigate", json!({"url": url}))
48 .await?;
49
50 if let Some(err) = result.get("errorText").and_then(|v| v.as_str()) {
51 bail!("Navigation error: {err}");
52 }
53
54 wait_for_load(client, session_id, NAVIGATION_TIMEOUT_MS).await?;
55 let result =
56 CommandResult::output(format!("Navigated to {url}")).with_navigated_to(url.to_string());
57 Ok(result.save_output(output).await?)
58 }
59 .await;
60
61 // Only clear headers that this invocation applied.
62 if extra_headers.is_some() {
63 if let Err(e) = super::pages::clear_extra_headers(client, session_id).await {
64 eprintln!("Warning: failed to clear extra headers: {e}");
65 }
66 }

Callers 4

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

Calls 9

apply_extra_headersFunction · 0.85
go_backFunction · 0.85
go_forwardFunction · 0.85
do_reloadFunction · 0.85
wait_for_loadFunction · 0.85
clear_extra_headersFunction · 0.85
send_to_targetMethod · 0.80
with_navigated_toMethod · 0.80
save_outputMethod · 0.80

Tested by

no test coverage detected