frc/src/browser.rs

62 lines
1.5 KiB
Rust

use std::{thread, time::Duration};
use futures::TryFutureExt;
use thirtyfour::{
prelude::ElementQueryable, session::handle::SessionHandle, By, DesiredCapabilities, WebDriver,
};
pub struct BrowserSession {
driver: WebDriver,
}
#[derive(Clone)]
pub struct BrowserHandle {
handle: SessionHandle,
}
impl BrowserSession {
pub async fn new() -> Result<Self, anyhow::Error> {
let caps = DesiredCapabilities::chrome();
let driver = WebDriver::new("http://localhost:6444", caps).await?;
Ok(Self { driver: driver })
}
pub fn handle(&self) -> BrowserHandle {
BrowserHandle {
handle: self.driver.clone(),
}
}
pub async fn stop(self) -> Result<(), anyhow::Error> {
self.driver.quit().await?;
Ok(())
}
}
impl BrowserHandle {
pub async fn start(&self, start_url: &str) -> Result<(), anyhow::Error> {
// self.handle.fullscreen_window().await?;
self.go_to(format!("http://{}", start_url).as_str()).await?;
Ok(())
}
async fn decline_cookies(&self) -> Result<(), anyhow::Error> {
Ok(())
}
async fn youtube(&self) -> Result<(), anyhow::Error> {
self.decline_cookies().await?;
Ok(())
}
pub async fn go_to(&self, url: &str) -> Result<(), anyhow::Error> {
self.handle.get(url).await?;
if let Some(domain) = self.handle.current_url().await?.domain() {
if domain == "www.youtube.com" {
self.youtube().await?;
}
}
Ok(())
}
}