Brings Obscura's CDP server in line with what Playwright's `chromium.connectOverCDP()` expects. Each fix is small and surgical; together they make `connect → newContext → newPage → goto → page.title()` work end-to-end against `https://example.com`. 1. **server.rs — emit pending events before command response.** Chromium CDP semantics require side-effect events (e.g. `Target.targetCreated` + `Target.attachedToTarget` from `Target.createTarget`) to arrive *before* the originating command's response. Playwright's wire reader assumes this ordering — it resolves the response promise on the response and then synchronously looks up the freshly-created session. Reversing the order makes that lookup miss and the page hang at `newPage()`. 2. **domains/browser.rs — accept Browser.setWindowBounds as a no-op.** Playwright calls `Browser.setWindowBounds` during teardown. Returning "Unknown Browser method" causes Playwright to treat the target as broken and tear it down mid-test, surfacing as "Frame has been detached" on the next `page.title()`. 3. **obscura-browser/page.rs — frame_id == target_id for main frame.** Was `format!("{id}.1", id)`. Playwright's frame manager indexes frames by frame id and looks up the main frame by target id, so the `.1` suffix made every main-frame lookup miss. 4. **domains/page.rs + dispatch.rs — track isolated worlds and re-emit them post-navigation.** Playwright registers a utility world via `Page.createIsolatedWorld` and expects `Runtime.executionContextCreated` for each registered world after every navigation. Without re-emission, the utility-world handle goes stale post-`goto` and `page.title()` hangs waiting for the world's execution context. Falls back to the legacy `__puppeteer_utility_world__24.40.0` name when no worlds have been registered, preserving prior Puppeteer support. 5. **obscura-js/runtime.rs — strip trailing semicolons before wrapping.** Playwright's utility-script expression is an IIFE ending in `})();`. Both `wrap_expression` (CDP `Runtime.evaluate`) and `evaluate_for_cdp` (the `meta_code` IIFE) embed the user expression inside `({EXPR})` / `return ({EXPR});`. With the trailing `;` left in, this produces `({...;})` — a SyntaxError. Parse errors are not catchable, so the surrounding try/catch never fires and the call silently returns `undefined`, surfacing as `page.title() === ""`. Verified end-to-end against `https://example.com`: [probe] connect OK [probe] newContext OK [probe] newPage OK [probe] goto OK [probe] title = Example Domain
249 lines
13 KiB
Rust
249 lines
13 KiB
Rust
use obscura_browser::lifecycle::WaitUntil;
|
|
use serde_json::{json, Value};
|
|
|
|
use crate::dispatch::CdpContext;
|
|
use crate::types::CdpEvent;
|
|
|
|
pub async fn handle(
|
|
method: &str,
|
|
params: &Value,
|
|
ctx: &mut CdpContext,
|
|
session_id: &Option<String>,
|
|
) -> Result<Value, String> {
|
|
match method {
|
|
"enable" => Ok(json!({})),
|
|
"navigate" => {
|
|
let url = params.get("url").and_then(|v| v.as_str())
|
|
.ok_or("url required")?;
|
|
|
|
let wait_until = params.get("waitUntil")
|
|
.and_then(|v| {
|
|
if let Some(s) = v.as_str() {
|
|
Some(WaitUntil::from_str(s))
|
|
} else if let Some(arr) = v.as_array() {
|
|
arr.iter()
|
|
.filter_map(|item| item.as_str())
|
|
.map(WaitUntil::from_str)
|
|
.max_by_key(|w| match w {
|
|
WaitUntil::DomContentLoaded => 0,
|
|
WaitUntil::Load => 1,
|
|
WaitUntil::NetworkIdle2 => 2,
|
|
WaitUntil::NetworkIdle0 => 3,
|
|
})
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.unwrap_or(WaitUntil::Load);
|
|
|
|
let preload_scripts: Vec<String> = ctx.preload_scripts.iter().map(|(_, s)| s.clone()).collect();
|
|
|
|
let (frame_id, loader_id, network_events, page_url, page_id, reached_network_idle) = {
|
|
let page = ctx.get_session_page_mut(session_id).ok_or("No page for session")?;
|
|
let frame_id = page.frame_id.clone();
|
|
let loader_id = format!("loader-{}", uuid::Uuid::new_v4());
|
|
|
|
let nav_method = params.get("__method").and_then(|v| v.as_str()).unwrap_or("GET");
|
|
let nav_body = params.get("__body").and_then(|v| v.as_str()).unwrap_or("");
|
|
if nav_method == "POST" && !nav_body.is_empty() {
|
|
page.navigate_with_wait_post(url, wait_until, nav_method, nav_body).await.map_err(|e| e.to_string())?;
|
|
} else {
|
|
page.navigate_with_wait(url, wait_until).await.map_err(|e| e.to_string())?;
|
|
}
|
|
|
|
for source in &preload_scripts {
|
|
if let Err(e) = page.execute_preload_script(source) {
|
|
tracing::debug!("Preload script error: {}", e);
|
|
}
|
|
}
|
|
|
|
let reached_network_idle = page.lifecycle.is_network_idle();
|
|
let network_events: Vec<_> = page.network_events.drain(..).collect();
|
|
let page_url = page.url_string();
|
|
let page_id = page.id.clone();
|
|
(frame_id, loader_id, network_events, page_url, page_id, reached_network_idle)
|
|
};
|
|
|
|
let es = session_id.clone();
|
|
let ts = timestamp();
|
|
|
|
let mut phase1 = vec![
|
|
CdpEvent { method: "Page.lifecycleEvent".into(), params: json!({"frameId": frame_id, "loaderId": loader_id, "name": "init", "timestamp": ts}), session_id: es.clone() },
|
|
CdpEvent { method: "Runtime.executionContextsCleared".into(), params: json!({}), session_id: es.clone() },
|
|
CdpEvent { method: "Page.frameNavigated".into(), params: json!({"frame": {"id": frame_id, "loaderId": loader_id, "url": page_url, "domainAndRegistry": "", "securityOrigin": page_url, "mimeType": "text/html", "adFrameStatus": {"adFrameType": "none"}}, "type": "Navigation"}), session_id: es.clone() },
|
|
CdpEvent { method: "Runtime.executionContextCreated".into(), params: json!({"context": {"id": 2, "origin": page_url, "name": "", "uniqueId": format!("ctx-nav-{}", page_id), "auxData": {"isDefault": true, "type": "default", "frameId": frame_id}}}), session_id: es.clone() },
|
|
];
|
|
// Re-emit each isolated world the client previously registered
|
|
// via Page.createIsolatedWorld. Without this, Playwright's
|
|
// utility-world handle becomes stale after navigation and
|
|
// every subsequent evaluate() (including page.title()) hangs.
|
|
// Fallback to the legacy hardcoded Puppeteer name so older
|
|
// Puppeteer clients that don't call createIsolatedWorld
|
|
// continue to work.
|
|
let world_names: Vec<String> = if ctx.isolated_worlds.is_empty() {
|
|
vec!["__puppeteer_utility_world__24.40.0".to_string()]
|
|
} else {
|
|
ctx.isolated_worlds.clone()
|
|
};
|
|
for (idx, world_name) in world_names.iter().enumerate() {
|
|
let world_ctx_id = 100 + idx as u32;
|
|
phase1.push(CdpEvent {
|
|
method: "Runtime.executionContextCreated".into(),
|
|
params: json!({"context": {"id": world_ctx_id, "origin": page_url, "name": world_name, "uniqueId": format!("ctx-isolated-nav-{}-{}", page_id, idx), "auxData": {"isDefault": false, "type": "isolated", "frameId": frame_id}}}),
|
|
session_id: es.clone(),
|
|
});
|
|
}
|
|
phase1.push(CdpEvent { method: "Page.lifecycleEvent".into(), params: json!({"frameId": frame_id, "loaderId": loader_id, "name": "commit", "timestamp": ts}), session_id: es.clone() });
|
|
ctx.pending_events.extend(phase1);
|
|
|
|
if ctx.fetch_intercept.enabled {
|
|
for net_event in &network_events {
|
|
ctx.pending_events.push(CdpEvent {
|
|
method: "Fetch.requestPaused".into(),
|
|
params: json!({
|
|
"requestId": net_event.request_id,
|
|
"request": {
|
|
"url": net_event.url,
|
|
"method": net_event.method,
|
|
"headers": net_event.headers,
|
|
},
|
|
"frameId": frame_id,
|
|
"resourceType": net_event.resource_type,
|
|
"networkId": net_event.request_id,
|
|
}),
|
|
session_id: es.clone(),
|
|
});
|
|
}
|
|
}
|
|
|
|
for net_event in &network_events {
|
|
ctx.pending_events.push(CdpEvent {
|
|
method: "Network.requestWillBeSent".into(),
|
|
params: json!({"requestId": net_event.request_id, "loaderId": loader_id, "documentURL": page_url, "request": {"url": net_event.url, "method": net_event.method, "headers": net_event.headers}, "timestamp": net_event.timestamp, "wallTime": net_event.timestamp, "initiator": {"type": "other"}, "type": net_event.resource_type, "frameId": frame_id}),
|
|
session_id: es.clone(),
|
|
});
|
|
ctx.pending_events.push(CdpEvent {
|
|
method: "Network.responseReceived".into(),
|
|
params: json!({"requestId": net_event.request_id, "loaderId": loader_id, "timestamp": net_event.timestamp, "type": net_event.resource_type, "response": {"url": net_event.url, "status": net_event.status, "statusText": "", "headers": &*net_event.response_headers, "mimeType": net_event.response_headers.get("content-type").cloned().unwrap_or_default()}, "frameId": frame_id}),
|
|
session_id: es.clone(),
|
|
});
|
|
ctx.pending_events.push(CdpEvent {
|
|
method: "Network.loadingFinished".into(),
|
|
params: json!({"requestId": net_event.request_id, "timestamp": net_event.timestamp, "encodedDataLength": net_event.body_size}),
|
|
session_id: es.clone(),
|
|
});
|
|
}
|
|
|
|
let mut phase3 = vec![
|
|
CdpEvent { method: "Page.lifecycleEvent".into(), params: json!({"frameId": frame_id, "loaderId": loader_id, "name": "DOMContentLoaded", "timestamp": ts}), session_id: es.clone() },
|
|
CdpEvent { method: "Page.domContentEventFired".into(), params: json!({"timestamp": ts}), session_id: es.clone() },
|
|
CdpEvent { method: "Page.lifecycleEvent".into(), params: json!({"frameId": frame_id, "loaderId": loader_id, "name": "load", "timestamp": ts}), session_id: es.clone() },
|
|
CdpEvent { method: "Page.loadEventFired".into(), params: json!({"timestamp": ts}), session_id: es.clone() },
|
|
];
|
|
if reached_network_idle || matches!(wait_until, WaitUntil::Load | WaitUntil::DomContentLoaded) {
|
|
let idle_ts = timestamp();
|
|
phase3.push(CdpEvent { method: "Page.lifecycleEvent".into(), params: json!({"frameId": frame_id, "loaderId": loader_id, "name": "networkIdle", "timestamp": idle_ts}), session_id: es.clone() });
|
|
}
|
|
phase3.push(CdpEvent { method: "Page.frameStoppedLoading".into(), params: json!({"frameId": frame_id}), session_id: es });
|
|
ctx.pending_events.extend(phase3);
|
|
|
|
Ok(json!({
|
|
"frameId": frame_id,
|
|
"loaderId": loader_id,
|
|
}))
|
|
}
|
|
"getFrameTree" => {
|
|
let page = ctx.get_session_page(session_id).ok_or("No page for session")?;
|
|
Ok(json!({
|
|
"frameTree": {
|
|
"frame": {
|
|
"id": page.frame_id,
|
|
"loaderId": "initial-loader",
|
|
"url": page.url_string(),
|
|
"domainAndRegistry": "",
|
|
"securityOrigin": page.url_string(),
|
|
"mimeType": "text/html",
|
|
"adFrameStatus": { "adFrameType": "none" },
|
|
},
|
|
"childFrames": [],
|
|
}
|
|
}))
|
|
}
|
|
"createIsolatedWorld" => {
|
|
let page = ctx.get_session_page(session_id).ok_or("No page for session")?;
|
|
let frame_id_param = params.get("frameId").and_then(|v| v.as_str())
|
|
.unwrap_or(&page.frame_id).to_string();
|
|
let world_name = params.get("worldName").and_then(|v| v.as_str())
|
|
.unwrap_or("").to_string();
|
|
let page_url = page.url_string();
|
|
let page_id = page.id.clone();
|
|
let context_id = 100;
|
|
// Track this world so Page.navigate can re-emit a context for it
|
|
// post-navigation. Without this, Playwright (and Puppeteer)
|
|
// hang in any operation that uses the utility world — including
|
|
// page.title() — because their utility world is gone after
|
|
// Runtime.executionContextsCleared and never re-created.
|
|
if !world_name.is_empty() && !ctx.isolated_worlds.contains(&world_name) {
|
|
ctx.isolated_worlds.push(world_name.clone());
|
|
}
|
|
|
|
ctx.pending_events.push(CdpEvent {
|
|
method: "Runtime.executionContextCreated".to_string(),
|
|
params: json!({
|
|
"context": {
|
|
"id": context_id,
|
|
"origin": page_url,
|
|
"name": world_name,
|
|
"uniqueId": format!("ctx-isolated-{}", page_id),
|
|
"auxData": {
|
|
"isDefault": false,
|
|
"type": "isolated",
|
|
"frameId": frame_id_param,
|
|
}
|
|
}
|
|
}),
|
|
session_id: session_id.clone(),
|
|
});
|
|
|
|
Ok(json!({ "executionContextId": context_id }))
|
|
}
|
|
"setLifecycleEventsEnabled" => Ok(json!({})),
|
|
"addScriptToEvaluateOnNewDocument" => {
|
|
let source = params.get("source").and_then(|v| v.as_str()).unwrap_or("");
|
|
ctx.preload_counter += 1;
|
|
let identifier = format!("{}", ctx.preload_counter);
|
|
if !source.is_empty() {
|
|
ctx.preload_scripts.push((identifier.clone(), source.to_string()));
|
|
}
|
|
Ok(json!({ "identifier": identifier }))
|
|
}
|
|
"removeScriptToEvaluateOnNewDocument" => {
|
|
let identifier = params.get("identifier").and_then(|v| v.as_str()).unwrap_or("");
|
|
ctx.preload_scripts.retain(|(id, _)| id != identifier);
|
|
Ok(json!({}))
|
|
}
|
|
"setInterceptFileChooserDialog" => Ok(json!({})),
|
|
"getNavigationHistory" => {
|
|
let page = ctx.get_session_page(session_id).ok_or("No page for session")?;
|
|
Ok(json!({
|
|
"currentIndex": 0,
|
|
"entries": [{
|
|
"id": 0,
|
|
"url": page.url_string(),
|
|
"userTypedURL": page.url_string(),
|
|
"title": page.title,
|
|
"transitionType": "typed",
|
|
}]
|
|
}))
|
|
}
|
|
_ => Err(format!("Unknown Page method: {}", method)),
|
|
}
|
|
}
|
|
|
|
fn timestamp() -> f64 {
|
|
std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.unwrap_or_default()
|
|
.as_secs_f64()
|
|
}
|