use std::time::Duration;
use void_crawl_core::{Page, VoidCrawlError};
pub async fn apply(
page: &Page,
spec: Option<&str>,
timeout: Duration,
) -> Result<(), VoidCrawlError> {
apply_with_idle_state(page, spec, timeout, false).await
}
pub async fn apply_post_navigate(
page: &Page,
spec: Option<&str>,
timeout: Duration,
) -> Result<(), VoidCrawlError> {
apply_with_idle_state(page, spec, timeout, true).await
}
async fn apply_with_idle_state(
page: &Page,
spec: Option<&str>,
timeout: Duration,
already_idle: bool,
) -> Result<(), VoidCrawlError> {
let raw = spec.unwrap_or("networkidle").trim();
if raw.eq_ignore_ascii_case("networkidle") {
if already_idle {
return Ok(());
}
page.wait_for_network_idle(timeout).await?;
return Ok(());
}
if let Some(css) = raw.strip_prefix("selector:") {
return page.wait_for_selector(css.trim(), timeout).await;
}
Err(VoidCrawlError::Other(format!(
"unknown wait_for spec: {raw:?} (expected 'networkidle' or 'selector:<css>')"
)))
}