feat: add 'get styles' command for computed styles extraction (#142)
This commit is contained in:
+9
-2
@@ -555,7 +555,7 @@ pub fn parse_command(args: &[String], flags: &Flags) -> Result<Value, ParseError
|
||||
}
|
||||
|
||||
fn parse_get(rest: &[&str], id: &str) -> Result<Value, ParseError> {
|
||||
const VALID: &[&str] = &["text", "html", "value", "attr", "url", "title", "count", "box"];
|
||||
const VALID: &[&str] = &["text", "html", "value", "attr", "url", "title", "count", "box", "styles"];
|
||||
|
||||
match rest.get(0).map(|s| *s) {
|
||||
Some("text") => {
|
||||
@@ -606,13 +606,20 @@ fn parse_get(rest: &[&str], id: &str) -> Result<Value, ParseError> {
|
||||
})?;
|
||||
Ok(json!({ "id": id, "action": "boundingbox", "selector": sel }))
|
||||
}
|
||||
Some("styles") => {
|
||||
let sel = rest.get(1).ok_or_else(|| ParseError::MissingArguments {
|
||||
context: "get styles".to_string(),
|
||||
usage: "get styles <selector>",
|
||||
})?;
|
||||
Ok(json!({ "id": id, "action": "styles", "selector": sel }))
|
||||
}
|
||||
Some(sub) => Err(ParseError::UnknownSubcommand {
|
||||
subcommand: sub.to_string(),
|
||||
valid_options: VALID,
|
||||
}),
|
||||
None => Err(ParseError::MissingArguments {
|
||||
context: "get".to_string(),
|
||||
usage: "get <text|html|value|attr|url|title|count|box> [args...]",
|
||||
usage: "get <text|html|value|attr|url|title|count|box|styles> [args...]",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
+38
-1
@@ -130,6 +130,40 @@ pub fn print_response(resp: &Response, json_mode: bool) {
|
||||
);
|
||||
return;
|
||||
}
|
||||
// Element styles
|
||||
if let Some(elements) = data.get("elements").and_then(|v| v.as_array()) {
|
||||
for (i, el) in elements.iter().enumerate() {
|
||||
let tag = el.get("tag").and_then(|v| v.as_str()).unwrap_or("?");
|
||||
let text = el.get("text").and_then(|v| v.as_str()).unwrap_or("");
|
||||
println!("[{}] {} \"{}\"", i, tag, text);
|
||||
|
||||
if let Some(box_data) = el.get("box") {
|
||||
let w = box_data.get("width").and_then(|v| v.as_i64()).unwrap_or(0);
|
||||
let h = box_data.get("height").and_then(|v| v.as_i64()).unwrap_or(0);
|
||||
let x = box_data.get("x").and_then(|v| v.as_i64()).unwrap_or(0);
|
||||
let y = box_data.get("y").and_then(|v| v.as_i64()).unwrap_or(0);
|
||||
println!(" box: {}x{} at ({}, {})", w, h, x, y);
|
||||
}
|
||||
|
||||
if let Some(styles) = el.get("styles") {
|
||||
let font_size = styles.get("fontSize").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let font_weight = styles.get("fontWeight").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let font_family = styles.get("fontFamily").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let color = styles.get("color").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let bg = styles.get("backgroundColor").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let radius = styles.get("borderRadius").and_then(|v| v.as_str()).unwrap_or("");
|
||||
|
||||
println!(" font: {} {} {}", font_size, font_weight, font_family);
|
||||
println!(" color: {}", color);
|
||||
println!(" background: {}", bg);
|
||||
if radius != "0px" {
|
||||
println!(" border-radius: {}", radius);
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Closed
|
||||
if data.get("closed").is_some() {
|
||||
println!("\x1b[32m✓\x1b[0m Browser closed");
|
||||
@@ -676,6 +710,7 @@ Subcommands:
|
||||
url Get current URL
|
||||
count <selector> Count matching elements
|
||||
box <selector> Get bounding box (x, y, width, height)
|
||||
styles <selector> Get computed styles of elements
|
||||
|
||||
Global Options:
|
||||
--json Output as JSON
|
||||
@@ -690,6 +725,8 @@ Examples:
|
||||
agent-browser get url
|
||||
agent-browser get count "li.item"
|
||||
agent-browser get box "#header"
|
||||
agent-browser get styles "button"
|
||||
agent-browser get styles @e1
|
||||
"##,
|
||||
|
||||
// === Is ===
|
||||
@@ -1209,7 +1246,7 @@ Navigation:
|
||||
reload Reload page
|
||||
|
||||
Get Info: agent-browser get <what> [selector]
|
||||
text, html, value, attr <name>, title, url, count, box
|
||||
text, html, value, attr <name>, title, url, count, box, styles
|
||||
|
||||
Check State: agent-browser is <what> <selector>
|
||||
visible, enabled, checked
|
||||
|
||||
Reference in New Issue
Block a user