use anyhow::Result;
use async_trait::async_trait;
use chromiumoxide::Page;
use super::{parse_detection_result, CaptchaInfo, Detector};
pub struct ImageCaptchaDetector;
impl ImageCaptchaDetector {
pub const JS: &'static str = r#"(function() {
const imgCaptcha = document.querySelector(
'img[src*="captcha"], img[src*="Captcha"], .captcha img, #captcha img'
);
if (imgCaptcha) {
return { kind: 'image', site_key: null, container: '.captcha, #captcha' };
}
return { kind: 'none', site_key: null, container: null };
})()"#;
}
#[async_trait]
impl Detector for ImageCaptchaDetector {
fn name(&self) -> &'static str {
"image_captcha"
}
fn priority(&self) -> i32 {
40
}
async fn detect(&self, page: &Page) -> Result<Option<CaptchaInfo>> {
let raw = page.evaluate(Self::JS).await?;
let val = raw.into_value::<serde_json::Value>()?;
Ok(parse_detection_result(val))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn image_captcha_detector_js_has_selectors() {
assert!(ImageCaptchaDetector::JS.contains("img[src*=\"captcha\"]"));
assert!(ImageCaptchaDetector::JS.contains(".captcha img"));
}
}