use rand::{Rng, SeedableRng};
pub use runtime_foxdriver::ScrollDirection;
use runtime_foxdriver::Page;
use std::time::Duration;
mod typing;
pub use typing::{type_human, type_realistic};
pub async fn mouse_move_bezier(
page: &Page,
x0: f64,
y0: f64,
x1: f64,
y1: f64,
) -> anyhow::Result<()> {
let mut rng = rand::rngs::StdRng::from_entropy();
let steps = rng.gen_range(8..=20);
let cx1 = x0 + (x1 - x0) * rng.gen_range(0.2..0.5) + rng.gen_range(-30.0..30.0);
let cy1 = y0 + (y1 - y0) * rng.gen_range(0.1..0.4) + rng.gen_range(-20.0..20.0);
let cx2 = x0 + (x1 - x0) * rng.gen_range(0.6..0.9) + rng.gen_range(-20.0..20.0);
let cy2 = y0 + (y1 - y0) * rng.gen_range(0.5..0.8) + rng.gen_range(-15.0..15.0);
for i in 0..=steps {
let t = i as f64 / steps as f64;
let u = 1.0 - t;
let x = u * u * u * x0 + 3.0 * u * u * t * cx1 + 3.0 * u * t * t * cx2 + t * t * t * x1;
let y = u * u * u * y0 + 3.0 * u * u * t * cy1 + 3.0 * u * t * t * cy2 + t * t * t * y1;
page.move_mouse_to(x, y).await?;
let ease = (std::f64::consts::PI * t).sin(); let base_ms = rng.gen_range(8..25);
let delay_ms = base_ms + ((1.0 - ease) * 15.0) as u64;
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
}
Ok(())
}
pub async fn mouse_move_human(
page: &Page,
x0: f64,
y0: f64,
x1: f64,
y1: f64,
) -> anyhow::Result<()> {
page.mouse_move_human(x0, y0, x1, y1).await
}
pub async fn click_realistic(page: &Page, x: f64, y: f64) -> anyhow::Result<()> {
page.click_at(x, y).await
}
pub async fn scroll_realistic(
page: &Page,
direction: ScrollDirection,
amount: u32,
) -> anyhow::Result<()> {
page.scroll_realistic(direction, amount).await
}
pub async fn idle_pause() {
let ms = rand::rngs::StdRng::from_entropy().gen_range(500..2000);
tokio::time::sleep(Duration::from_millis(ms)).await;
}
pub async fn micro_pause() {
let ms = rand::rngs::StdRng::from_entropy().gen_range(100..400);
tokio::time::sleep(Duration::from_millis(ms)).await;
}
pub async fn random_pause(min_ms: u64, max_ms: u64) {
assert!(
min_ms <= max_ms,
"random_pause: min_ms ({min_ms}) > max_ms ({max_ms})"
);
if min_ms == max_ms {
tokio::time::sleep(Duration::from_millis(min_ms)).await;
return;
}
let ms = rand::rngs::StdRng::from_entropy().gen_range(min_ms..=max_ms);
tokio::time::sleep(Duration::from_millis(ms)).await;
}
pub async fn triple_click(page: &Page, x: f64, y: f64) -> anyhow::Result<()> {
for _ in 0..3 {
page.click_at(x, y).await?;
tokio::time::sleep(Duration::from_millis(60)).await;
}
Ok(())
}
pub async fn hover_dwell(page: &Page, x: f64, y: f64) -> anyhow::Result<()> {
page.mouse_move_human(0.0, 0.0, x, y).await?;
let ms = rand::rngs::StdRng::from_entropy().gen_range(200..700);
tokio::time::sleep(Duration::from_millis(ms)).await;
Ok(())
}
pub async fn touch_swipe(
page: &Page,
x0: f64,
y0: f64,
x1: f64,
y1: f64,
duration_ms: u64,
) -> anyhow::Result<()> {
let steps = 25;
let step_ms = (duration_ms / steps as u64).max(8);
page.mouse_down(x0, y0).await?;
for s in 1..=steps {
let t = s as f64 / steps as f64;
let eased = 1.0 - (1.0 - t).powi(2);
let x = x0 + (x1 - x0) * eased;
let y = y0 + (y1 - y0) * eased;
page.mouse_move_human(x0, y0, x, y).await?;
tokio::time::sleep(Duration::from_millis(step_ms)).await;
}
page.mouse_up(x1, y1).await?;
Ok(())
}
pub async fn pointer_jitter(
page: &Page,
cx: f64,
cy: f64,
cycles: u32,
cycle_ms: u64,
) -> anyhow::Result<()> {
let mut rng = rand::rngs::StdRng::from_entropy();
for c in 0..cycles {
let amp_x: f64 = rng.gen_range(2.0..4.5);
let amp_y: f64 = rng.gen_range(2.0..4.5);
let phase: f64 = rng.gen_range(0.0..std::f64::consts::TAU);
let steps = 8u64;
for s in 0..steps {
let theta = phase + (c as f64 + s as f64 / steps as f64) * std::f64::consts::TAU;
let x = cx + amp_x * theta.sin();
let y = cy + amp_y * (theta * 1.7).cos();
page.mouse_move_human(cx, cy, x, y).await?;
tokio::time::sleep(Duration::from_millis(cycle_ms / steps)).await;
}
}
Ok(())
}
#[cfg(test)]
mod random_pause_tests;
#[cfg(test)]
mod tests;