Repository navigation
fix(cua-driver): target page JavaScript exactly #2166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
68a42c0
5c2b402
0e9a829
35fa565
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,18 @@ impl CdpClient { | |
| /// "Allow remote debugging?" confirmation the Chrome-toggle path does, | ||
| /// so there's no reason to route it through `CdpSessionCache`. | ||
| pub async fn evaluate(javascript: &str, port: u16) -> anyhow::Result<String> { | ||
| let mut session = CdpSession::connect(port, None).await?; | ||
| Self::evaluate_targeted(javascript, port, None).await | ||
| } | ||
|
|
||
| /// Evaluate JavaScript against the unique page target selected by | ||
| /// `target_url_contains`. An explicit hint never falls back to another | ||
| /// page when it is absent or ambiguous. | ||
| pub async fn evaluate_targeted( | ||
| javascript: &str, | ||
| port: u16, | ||
| target_url_contains: Option<&str>, | ||
| ) -> anyhow::Result<String> { | ||
| let mut session = CdpSession::connect(port, target_url_contains).await?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in I added |
||
| let obj = tokio::time::timeout( | ||
| Duration::from_secs(10), | ||
| session.call("Runtime.evaluate", serde_json::json!({ | ||
|
|
@@ -419,24 +430,77 @@ async fn ws_url_for_page_target(port: u16, target_url_contains: Option<&str>) -> | |
| Ok((ws_url, target_url)) | ||
| } | ||
|
|
||
| /// Pick the page target whose `url` contains `hint` (case-insensitive), or | ||
| /// fall back to the first page in `pages` if no hint is given or nothing | ||
| /// matches. Shared by both discovery paths (classic `/json` and | ||
| /// Pick the unique page target whose `url` contains `hint` | ||
| /// (case-insensitive), or the first page when no hint is given. Explicit | ||
| /// hints fail closed when zero or multiple pages match. Shared by both | ||
| /// discovery paths (classic `/json` and | ||
| /// `Target.getTargets`) since a browser with more than one tab open is | ||
| /// otherwise picked non-deterministically — CDP target ids carry no | ||
| /// relationship to the caller's `window_id`. | ||
| fn pick_target<'a>(pages: &[&'a serde_json::Value], hint: Option<&str>) -> Option<&'a serde_json::Value> { | ||
| if let Some(hint) = hint { | ||
| let hint_lower = hint.to_ascii_lowercase(); | ||
| if let Some(t) = pages.iter().find(|t| { | ||
| t.get("url") | ||
| .and_then(|v| v.as_str()) | ||
| .is_some_and(|url| url.to_ascii_lowercase().contains(&hint_lower)) | ||
| }) { | ||
| return Some(t); | ||
| match hint { | ||
| None => pages.first().copied(), | ||
| Some(hint) => { | ||
| let hint_lower = hint.to_ascii_lowercase(); | ||
| let mut matches = pages.iter().copied().filter(|target| { | ||
| target | ||
| .get("url") | ||
| .and_then(|value| value.as_str()) | ||
| .is_some_and(|url| url.to_ascii_lowercase().contains(&hint_lower)) | ||
| }); | ||
| let target = matches.next()?; | ||
| if matches.next().is_some() { | ||
| return None; | ||
| } | ||
| Some(target) | ||
| } | ||
| } | ||
| pages.first().copied() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::pick_target; | ||
|
|
||
| fn pages() -> Vec<serde_json::Value> { | ||
| vec![ | ||
| serde_json::json!({ "type": "page", "url": "app://fixture/#window-a" }), | ||
| serde_json::json!({ "type": "page", "url": "app://fixture/#window-b" }), | ||
| ] | ||
| } | ||
|
|
||
| #[test] | ||
| fn explicit_target_hint_selects_one_page() { | ||
| let pages = pages(); | ||
| let refs = pages.iter().collect::<Vec<_>>(); | ||
| assert_eq!( | ||
| pick_target(&refs, Some("#WINDOW-B")).and_then(|target| target["url"].as_str()), | ||
| Some("app://fixture/#window-b") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn explicit_target_hint_never_falls_back() { | ||
| let pages = pages(); | ||
| let refs = pages.iter().collect::<Vec<_>>(); | ||
| assert!(pick_target(&refs, Some("#missing")).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ambiguous_target_hint_fails_closed() { | ||
| let pages = pages(); | ||
| let refs = pages.iter().collect::<Vec<_>>(); | ||
| assert!(pick_target(&refs, Some("app://fixture/")).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn omitted_target_hint_keeps_legacy_first_page_behavior() { | ||
| let pages = pages(); | ||
| let refs = pages.iter().collect::<Vec<_>>(); | ||
| assert_eq!( | ||
| pick_target(&refs, None).and_then(|target| target["url"].as_str()), | ||
| Some("app://fixture/#window-a") | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| fn parse_cdp_result(obj: &serde_json::Value) -> anyhow::Result<String> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Windows and Linux,
PageToolis registered withWindowsPageBackend/LinuxPageBackend, and those impls still only implementexecute_javascriptwhile their CDP paths readCUA_DRIVER_CDP_PORT. Because the new dispatcher always callsexecute_javascript_targetedand this default bails whenevercdp_portis present, a call using the newly advertisedexecute_javascriptcdp_portnever reaches those CDP fallbacks; it fails with “not implemented” even though the platform can evaluate JS via CDP. Please either override this method in those backends to consumecdp_portor avoid advertising/accepting the option there.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in
35fa5658. The sharedcua_driver_core::cdphelper now supports unique case-insensitive URL targeting, and both Windows and Linux overrideexecute_javascript_targetedto consume explicitcdp_port/target_url_contains. Calls without explicit targeting still use the existing bookmark/env behavior.Validation:
cargo check -p platform-windows --target x86_64-pc-windows-gnu: passedcargo check -p platform-linux: passed in a clean Debian bookworm / Rust 1.92 container with X11/Wayland build dependencies.The local macOS-to-Linux cross-check was initially blocked before our code by missing pkg-config sysroot; the native Linux container check above covers the actual backend.