use serde_json::{Value, json};
use tokio::sync::broadcast;
use crate::action_parse::parse_action;
use crate::client::CdpClient;
use crate::config::Config;
use crate::error::{CdpError, Result};
use crate::keys::key_info;
use crate::types::ConsoleMessage;
pub use crate::action::BrowserAction;
pub use crate::action_builder::ActionBuilder;
const SELECTOR_POLL_MS: u64 = 100;
const CONSOLE_BACKLOG: usize = 64;
fn quote(s: &str) -> String {
serde_json::to_string(s).unwrap_or_else(|_| format!("\"{}\"", s.replace('"', "\\\"")))
}
#[derive(Debug, Clone)]
pub struct ActionResult {
pub success: bool,
pub value: Option<Value>,
pub error: Option<String>,
}
impl ActionResult {
pub fn is_success(&self) -> bool {
self.success
}
fn failed(error: impl std::fmt::Display) -> Self {
ActionResult {
success: false,
value: None,
error: Some(error.to_string()),
}
}
}
impl std::fmt::Display for ActionResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.success {
write!(f, "Ok({:?})", self.value)
} else {
write!(f, "Err({})", self.error.as_deref().unwrap_or("unknown"))
}
}
}
pub struct BrowserAgent {
client: CdpClient,
}
impl BrowserAgent {
pub async fn connect(host: &str, port: u16) -> Result<Self> {
let client = CdpClient::connect_to_page(host, port).await?;
for domain in ["Page", "Runtime", "DOM", "Network"] {
client.enable_domain(domain).await?;
}
Ok(BrowserAgent { client })
}
pub async fn connect_with_config(config: &Config) -> Result<Self> {
let agent = Self::connect(&config.host, config.port).await?;
agent
.client
.set_viewport(config.viewport_width, config.viewport_height, false)
.await?;
Ok(agent)
}
pub fn from_client(client: CdpClient) -> Self {
BrowserAgent { client }
}
pub async fn close(&self) -> Result<()> {
self.client.close().await
}
pub fn capture_console(&self) -> broadcast::Receiver<ConsoleMessage> {
let (tx, rx) = broadcast::channel(CONSOLE_BACKLOG);
let mut events = self.client.subscribe_events();
tokio::spawn(async move {
while let Ok((method, params)) = events.recv().await {
if method != "Runtime.consoleAPICalled" {
continue;
}
let level = params["type"].as_str().unwrap_or("log").to_string();
let text = params["args"]
.as_array()
.and_then(|args| args.first())
.and_then(|a| a["value"].as_str())
.unwrap_or("")
.to_string();
let url = params["stackTrace"]["callFrames"][0]["url"]
.as_str()
.map(str::to_owned);
let line = params["stackTrace"]["callFrames"][0]["lineNumber"].as_u64();
let _ = tx.send(ConsoleMessage {
level,
text,
url,
line,
});
}
});
rx
}
pub async fn execute(&self, action: BrowserAction) -> ActionResult {
match self.dispatch(action).await {
Ok(value) => ActionResult {
success: true,
value: Some(value),
error: None,
},
Err(e) => ActionResult::failed(e),
}
}
pub async fn execute_many(&self, actions: Vec<BrowserAction>) -> Vec<ActionResult> {
let mut results = Vec::with_capacity(actions.len());
for action in actions {
results.push(self.execute(action).await);
}
results
}
pub async fn execute_json(&self, json_str: &str) -> ActionResult {
match parse_action(json_str) {
Ok(action) => self.execute(action).await,
Err(e) => ActionResult::failed(e),
}
}
async fn dispatch(&self, action: BrowserAction) -> Result<Value> {
match action {
BrowserAction::Navigate { url } => {
let nav = self.client.navigate(&url).await?;
Ok(json!({ "frameId": nav.frame_id }))
}
BrowserAction::GoBack => self.client.send_command("Page.goBack", json!({})).await,
BrowserAction::GoForward => self.client.send_command("Page.goForward", json!({})).await,
BrowserAction::Reload => self.client.send_command("Page.reload", json!({})).await,
BrowserAction::Click { selector, x, y } => {
if let Some(sel) = selector {
self.client
.eval(&format!("document.querySelector({})?.click()", quote(&sel)))
.await?;
} else if let (Some(cx), Some(cy)) = (x, y) {
for event_type in ["mousePressed", "mouseReleased"] {
self.client
.send_command(
"Input.dispatchMouseEvent",
json!({
"type": event_type, "x": cx, "y": cy,
"button": "left", "clickCount": 1,
}),
)
.await?;
}
} else {
return Err(CdpError::Protocol("click: need selector or (x, y)".into()));
}
Ok(json!(null))
}
BrowserAction::Type { text, selector } => {
if let Some(sel) = selector {
self.client
.eval(&format!("document.querySelector({})?.focus()", quote(&sel)))
.await?;
}
self.client
.send_command("Input.insertText", json!({ "text": text }))
.await?;
Ok(json!(null))
}
BrowserAction::Fill { selector, value } => {
self.client
.eval(&format!(
"(sel => {{ \
let el = document.querySelector(sel); \
if (!el) return; \
el.focus(); \
el.value = {}; \
el.dispatchEvent(new Event('input', {{bubbles:true}})); \
el.dispatchEvent(new Event('change', {{bubbles:true}})); \
}})({})",
quote(&value),
quote(&selector),
))
.await?;
Ok(json!(null))
}
BrowserAction::Submit { selector } => {
let sel = selector.as_deref().unwrap_or("form");
self.client
.eval(&format!("document.querySelector({})?.submit()", quote(sel)))
.await?;
Ok(json!(null))
}
BrowserAction::PressKey { key } => {
let (code, vk) = key_info(&key);
self.client
.send_command(
"Input.dispatchKeyEvent",
json!({
"type": "keyDown", "key": key, "code": code,
"windowsVirtualKeyCode": vk,
}),
)
.await?;
self.client
.send_command(
"Input.dispatchKeyEvent",
json!({
"type": "keyUp", "key": key, "code": code,
}),
)
.await?;
Ok(json!(null))
}
BrowserAction::GetTitle => Ok(json!(self.client.eval("document.title").await?)),
BrowserAction::GetUrl => Ok(json!(self.client.eval("window.location.href").await?)),
BrowserAction::GetText => Ok(json!(self.client.eval("document.body.innerText").await?)),
BrowserAction::GetContent { selector } => {
let expr = match selector {
Some(sel) => format!("document.querySelector({})?.innerHTML", quote(&sel)),
None => "document.documentElement.outerHTML".into(),
};
Ok(json!(self.client.eval(&expr).await?))
}
BrowserAction::GetLinks => {
let ev = self
.client
.evaluate(
"Array.from(document.querySelectorAll('a'))\
.map(a => ({ href: a.href, text: a.innerText.trim() }))",
)
.await?;
Ok(ev.result.value.unwrap_or(json!([])))
}
BrowserAction::GetAttributes { selector } => {
let ev = self
.client
.evaluate(&format!(
"(sel => {{ \
let el = document.querySelector(sel); \
if (!el) return null; \
let attrs = {{}}; \
for (let a of el.attributes) attrs[a.name] = a.value; \
return attrs; \
}})({})",
quote(&selector)
))
.await?;
Ok(ev.result.value.unwrap_or(json!(null)))
}
BrowserAction::Exists { selector } => {
let ev = self
.client
.evaluate(&format!("!!document.querySelector({})", quote(&selector)))
.await?;
Ok(ev.result.value.unwrap_or(json!(false)))
}
BrowserAction::Screenshot { path } => match path {
Some(p) => {
self.client.full_page_screenshot_to_file(&p).await?;
Ok(json!(p))
}
None => Ok(json!(self.client.full_page_screenshot().await?.len())),
},
BrowserAction::Evaluate { expression } => {
let ev = self.client.evaluate(&expression).await?;
Ok(ev.result.value.unwrap_or(json!(null)))
}
BrowserAction::Wait { ms } => {
tokio::time::sleep(std::time::Duration::from_millis(ms)).await;
Ok(json!(null))
}
BrowserAction::WaitForSelector {
selector,
timeout_ms,
} => {
let deadline =
std::time::Instant::now() + std::time::Duration::from_millis(timeout_ms);
let expr = format!("!!document.querySelector({})", quote(&selector));
loop {
let ev = self.client.evaluate(&expr).await?;
if ev.result.value == Some(json!(true)) {
return Ok(json!(true));
}
if std::time::Instant::now() >= deadline {
return Err(CdpError::Timeout);
}
tokio::time::sleep(std::time::Duration::from_millis(SELECTOR_POLL_MS)).await;
}
}
BrowserAction::Scroll { x, y } => {
self.client
.eval(&format!("window.scrollTo({x}, {y})"))
.await?;
Ok(json!(null))
}
BrowserAction::SetViewport {
width,
height,
mobile,
} => {
self.client.set_viewport(width, height, mobile).await?;
Ok(json!(null))
}
BrowserAction::GetMetrics => {
self.client
.send_command("Performance.getMetrics", json!({}))
.await
}
}
}
}