Skip to main content

eoka_server/
annotate.rs

1use crate::eoka::{Page, Result};
2
3use crate::InteractiveElement;
4
5const ANNOTATE_JS: &str = include_str!("js/annotate.js");
6
7pub async fn annotated_screenshot(page: &Page, elements: &[InteractiveElement]) -> Result<Vec<u8>> {
8    if elements.is_empty() {
9        return page.screenshot().await;
10    }
11
12    let elem_data: Vec<serde_json::Value> = elements
13        .iter()
14        .map(|el| {
15            serde_json::json!({
16                "i": el.index,
17                "x": el.bbox.x as i32,
18                "y": el.bbox.y as i32,
19                "w": el.bbox.width as i32,
20                "h": el.bbox.height as i32,
21            })
22        })
23        .collect();
24
25    let json = serde_json::to_string(&elem_data).unwrap_or_default();
26    let inject_js = ANNOTATE_JS.replace("/*ELEM_DATA*/", &json);
27
28    page.execute(&inject_js).await?;
29    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
30    let png = page.screenshot().await?;
31    page.execute("document.getElementById('__eoka_overlay')?.remove()")
32        .await?;
33
34    Ok(png)
35}