use anyhow::Result;
use rand::Rng;
use runtime_foxdriver::Page;
use std::time::Duration;
pub async fn key_combo(page: &Page, keys: &[&str]) -> Result<()> {
if keys.is_empty() {
return Ok(());
}
let mut rng = rand::thread_rng();
let hold_ms = rng.gen_range(60..=140);
let stagger_ms = rng.gen_range(15..=45);
for (i, key) in keys.iter().enumerate() {
page.key_down(key).await?;
if i + 1 < keys.len() {
tokio::time::sleep(Duration::from_millis(stagger_ms)).await;
}
}
tokio::time::sleep(Duration::from_millis(hold_ms)).await;
for key in keys.iter().rev() {
page.key_up(key).await?;
}
Ok(())
}
pub async fn select_all(page: &Page) -> Result<()> {
key_combo(page, &["Control", "a"]).await
}
pub async fn copy(page: &Page) -> Result<()> {
key_combo(page, &["Control", "c"]).await
}
pub async fn paste(page: &Page) -> Result<()> {
key_combo(page, &["Control", "v"]).await
}
pub async fn cut(page: &Page) -> Result<()> {
key_combo(page, &["Control", "x"]).await
}