eoka 0.17.1

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
Documentation
//! Page Abstraction
//!
//! High-level API for interacting with a browser page.

mod diagnostics;
mod elements;
mod evaluate;
mod frames;
mod input;
mod navigation;
mod network;
mod response_capture;
mod wait;

pub use diagnostics::{ScreenshotClip, ScreenshotFormat, ScreenshotOptions};
pub use evaluate::{Eval, Frame};

pub use response_capture::{
    CaptureBodyError, CapturedFrameResponse, FrameResponseCapture, FrameResponseCaptureOptions,
    FrameResponseCaptureReport,
};

use std::sync::Arc;

use crate::cdp::Session;
use crate::error::Result;
use crate::StealthConfig;

pub(crate) use input::{
    coordinated_key_char, coordinated_key_down, coordinated_key_up, coordinated_mouse_down,
    coordinated_mouse_move, coordinated_mouse_up, coordinated_mouse_wheel, ActionInput, DragInput,
    HeldInputState,
};

use crate::element::Element;
pub(crate) use navigation::{classify_navigation, NavigationStart};
pub(crate) use wait::poll;

/// A mouse button used by held pointer input methods.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MouseButton {
    /// Primary (usually left) mouse button.
    Left,
    /// Auxiliary (usually middle) mouse button.
    Middle,
    /// Secondary (usually right) mouse button.
    Right,
    /// Browser-back mouse button.
    Back,
    /// Browser-forward mouse button.
    Forward,
}

/// A browser page with stealth capabilities. Cheap to clone (`Arc`-backed
/// shared state) — clones refer to the same tab.
#[derive(Clone)]
pub struct Page {
    pub(crate) session: Session,
    pub(crate) action_defaults: crate::ActionDefaults,
    pub(crate) config: Arc<StealthConfig>,
}

impl Page {
    /// Create a new Page wrapping a CDP session
    pub(crate) fn new(session: Session, config: Arc<StealthConfig>) -> Self {
        Self {
            action_defaults: crate::ActionDefaults::default(),
            session,
            config,
        }
    }

    /// Return a page handle whose shorthand actions use these defaults, such as
    /// a longer deadline. Other handles retain their own. Elements found through
    /// the returned handle inherit them.
    pub fn with_action_defaults(&self, defaults: crate::ActionDefaults) -> Self {
        let mut page = self.clone();
        page.action_defaults = defaults;
        page
    }

    /// This handle's [`crate::ActionDefaults`] as [`crate::ActionOptions`], the
    /// options its shorthand actions run with. Set `expect` for [`Page::act`].
    pub fn action_options(&self) -> crate::ActionOptions {
        self.action_defaults.options()
    }

    /// Override this page's desktop viewport in CSS pixels and its device scale factor.
    ///
    /// Dimensions must be positive and no greater than `i32::MAX`; the scale must
    /// be positive and finite. Invalid values fail before sending a CDP command.
    /// A 640 × 360 viewport at scale 2 produces a 1280 × 720 PNG screenshot.
    /// The override survives navigation and is shared by Eoka handles to this
    /// tab connected through the same browser endpoint in this process.
    /// Independent CDP clients and other processes are outside this coordinator;
    /// explicitly set the desired viewport after attachment before capturing
    /// a tab whose metrics were emulated by another client.
    /// Fractional native display scales can quantize layout dimensions to
    /// physical pixels; Chrome may also report DPR with renderer float precision.
    /// Captures retain Chrome's native rasterization at fractional boundaries,
    /// including quantization or partial coverage in the outermost pixels.
    /// A timeout or lost connection does not roll back a dispatched command.
    pub async fn set_viewport(
        &self,
        width: u32,
        height: u32,
        device_scale_factor: f64,
    ) -> Result<()> {
        self.session
            .set_viewport(width, height, device_scale_factor)
            .await
    }

    /// Clear the page's viewport override, restoring Chrome's native window
    /// content dimensions and device scale rather than the launch-configured
    /// override. CSS layout and screenshot pixel dimensions follow those native
    /// metrics; this does not resize the browser window.
    /// Waits for native surface/layout completion without activating the tab.
    /// A native capture can refresh Chrome's JavaScript-reported outer dimensions
    /// for a hidden tab independently of its unchanged browser-window bounds.
    pub async fn reset_viewport(&self) -> Result<()> {
        self.session.reset_viewport().await
    }

    /// This page's document count; see `Transport::document_epoch`.
    pub(crate) fn document(&self) -> u64 {
        self.session
            .transport()
            .document_epoch(self.session.session_id())
    }

    /// Get the underlying CDP session
    pub fn session(&self) -> &Session {
        &self.session
    }

    /// Get the target ID for this page (tab identifier)
    pub fn target_id(&self) -> &str {
        self.session.target_id()
    }
}

/// Information about a frame/iframe
#[derive(Debug, Clone)]
pub struct FrameInfo {
    /// Frame ID
    pub id: String,
    /// Frame URL
    pub url: String,
    /// Frame name (if any)
    pub name: Option<String>,
}

/// Result of clicking an element found through a shadow root.
///
/// Coordinates are top-level viewport CSS pixels, the same space used by
/// [`Page::click_at`]. The selector is evaluated in the frame's light DOM and
/// in every shadow root in that document, including closed roots.
#[derive(Debug, Clone, PartialEq)]
pub struct ShadowClickReceipt {
    /// Frame that owned the clicked node.
    pub frame_id: String,
    /// Selector that matched the node.
    pub selector: String,
    /// Viewport x coordinate of the click.
    pub x: f64,
    /// Viewport y coordinate of the click.
    pub y: f64,
}

/// Debug information about page state
#[derive(Debug, Clone, serde::Deserialize)]
pub struct PageState {
    /// Current page URL.
    pub url: String,
    /// Current page title.
    pub title: String,
    /// Number of `<input>` elements.
    pub input_count: u32,
    /// Number of `<button>` elements.
    pub button_count: u32,
    /// Number of `<a>` link elements.
    pub link_count: u32,
    /// Number of `<form>` elements.
    pub form_count: u32,
}