limit_cli/tools/browser/handlers/
query.rs1use super::super::args::ArgsExt;
2use super::super::client::BrowserClient;
3use super::super::client_ext::QueryExt;
4use limit_agent::error::AgentError;
5use serde_json::Value;
6
7pub async fn get(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
8 let what = args.get_str("get_what", "get")?;
9 let content = client.get(what).await?;
10 Ok(serde_json::json!({
11 "success": true,
12 "content": content
13 }))
14}
15
16pub async fn get_attr(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
17 let selector = args.get_str("selector", "get_attr")?;
18 let attr = args.get_str("attr", "get_attr")?;
19 let value = client.get_attr(selector, attr).await?;
20 Ok(serde_json::json!({
21 "success": true,
22 "value": value,
23 "message": format!("Attribute '{}' = '{}'", attr, value)
24 }))
25}
26
27pub async fn get_count(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
28 let selector = args.get_str("selector", "get_count")?;
29 let count = client.get_count(selector).await?;
30 Ok(serde_json::json!({
31 "success": true,
32 "count": count,
33 "message": format!("Found {} elements matching {}", count, selector)
34 }))
35}
36
37pub async fn get_box(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
38 let selector = args.get_str("selector", "get_box")?;
39 let bbox = client.get_box(selector).await?;
40 Ok(serde_json::json!({
41 "success": true,
42 "x": bbox.x,
43 "y": bbox.y,
44 "width": bbox.width,
45 "height": bbox.height,
46 "message": format!("Element bounding box: ({}, {}) {}x{}", bbox.x, bbox.y, bbox.width, bbox.height)
47 }))
48}
49
50pub async fn get_styles(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
51 let selector = args.get_str("selector", "get_styles")?;
52 let styles = client.get_styles(selector).await?;
53 Ok(serde_json::json!({
54 "success": true,
55 "styles": styles,
56 "message": format!("Got {} computed styles for {}", styles.len(), selector)
57 }))
58}
59
60pub async fn eval(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
61 let script = args.get_str("script", "eval")?;
62 let result = client.eval(script).await?;
63 Ok(serde_json::json!({
64 "success": true,
65 "result": result
66 }))
67}