use rand::{Rng, SeedableRng};
use runtime_foxdriver::Page;
pub use runtime_foxdriver::ScrollDirection;
use std::time::Duration;
use crate::human::timing::{jittered_step, ActionDelay};
mod keyboard;
mod typing;
pub use keyboard::{copy, cut, key_combo, paste, select_all};
pub use typing::{type_at_wpm, 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() {
ActionDelay::wait_idle().await;
}
pub async fn micro_pause() {
ActionDelay::wait_between_actions().await;
}
pub async fn random_pause(min_ms: u64, max_ms: u64) {
ActionDelay::wait_uniform(min_ms, max_ms).await;
}
pub async fn triple_click(page: &Page, x: f64, y: f64) -> anyhow::Result<()> {
let mut rng = rand::rngs::StdRng::from_entropy();
for _ in 0..3 {
page.click_at(x, y).await?;
tokio::time::sleep(jittered_step(Duration::from_millis(60), 0.35, &mut rng)).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);
let mut rng = rand::rngs::StdRng::from_entropy();
page.mouse_down(x0, y0).await?;
let (mut px, mut py) = (x0, y0);
for (x, y) in swipe_points(x0, y0, x1, y1, steps) {
page.mouse_move_human(px, py, x, y).await?;
px = x;
py = y;
tokio::time::sleep(jittered_step(
Duration::from_millis(step_ms),
0.30,
&mut rng,
))
.await;
}
page.mouse_up(x1, y1).await?;
Ok(())
}
fn swipe_points(x0: f64, y0: f64, x1: f64, y1: f64, steps: u32) -> Vec<(f64, f64)> {
let steps = steps.max(1);
(1..=steps)
.map(|s| {
let t = f64::from(s) / f64::from(steps);
let eased = 1.0 - (1.0 - t).powi(2);
(x0 + (x1 - x0) * eased, y0 + (y1 - y0) * eased)
})
.collect()
}
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.move_mouse_to(x, y).await?;
tokio::time::sleep(jittered_step(
Duration::from_millis(cycle_ms / steps),
0.30,
&mut rng,
))
.await;
}
}
Ok(())
}
#[cfg(test)]
mod random_pause_tests;
#[cfg(test)]
mod tests;