codetether-browser 0.1.0

Browser automation runtime for CodeTether
Documentation
//! Text extraction for native browser pages.

use crate::browser::{BrowserError, BrowserOutput, output::TextContent, request::ScopeRequest};
use tetherscript::browser::{query_selector, text_content};

/// Return text for the current page or selected element.
///
/// # Errors
///
/// Returns [`BrowserError`] when the session is not started.
pub(in crate::browser::session::native) async fn text(
    session: &super::super::super::BrowserSession,
    request: ScopeRequest,
) -> Result<BrowserOutput, BrowserError> {
    let native = session.inner.native.lock().await;
    let page = native
        .as_ref()
        .ok_or(BrowserError::SessionNotStarted)?
        .current()?;
    let text = request
        .selector
        .as_deref()
        .and_then(|selector| {
            query_selector(&page.session.document, selector)
                .first()
                .cloned()
        })
        .map(|node| text_content(&node))
        .unwrap_or_else(|| super::title::document_text(page));
    Ok(BrowserOutput::Text(TextContent { text }))
}