added fullscreening of video, chrome using default data dir

This commit is contained in:
puffaboo 2022-07-01 20:59:48 +01:00
parent 6564c7b546
commit 7eecbe0687
1 changed files with 57 additions and 10 deletions

View File

@ -1,23 +1,56 @@
use std::{thread, time::Duration};
use std::{env, path::Path};
use futures::TryFutureExt;
use thirtyfour::{
prelude::ElementQueryable, session::handle::SessionHandle, By, DesiredCapabilities, WebDriver,
};
use thirtyfour::{session::handle::SessionHandle, By, DesiredCapabilities, WebDriver};
pub struct BrowserSession {
driver: WebDriver,
}
const DATA_DIR_ARG: &str = "--user-data-dir";
#[cfg(target_os = "linux")]
const DATA_DIR_CHROMIUM: &str = "/.config/chromium";
#[cfg(target_os = "linux")]
const DATA_DIR_CHROME: &str = "~/.config/google-chrome";
#[cfg(target_os = "linux")]
const HOME_VARIABLE: &str = "HOME";
#[cfg(target_os = "windows")]
const DATA_DIR_CHROME: &str = "\\Google\\Chrome\\User Data";
#[cfg(target_os = "windows")]
const DATA_DIR_CHROMIUM: &str = "\\Google\\Chromium\\User Data";
#[cfg(target_os = "windows")]
const HOME_VARIABLE: &str = "LOCALAPPDATA";
#[derive(Clone)]
pub struct BrowserHandle {
handle: SessionHandle,
}
fn get_data_dir() -> String {
let mut home = String::new();
for (key, val) in env::vars() {
if key == HOME_VARIABLE {
home = val;
}
}
if home.is_empty() {
panic!("cannot find home env variable");
}
if Path::new(format!("{}{}", home, DATA_DIR_CHROMIUM).as_str()).exists() {
println!("{}{}", home, DATA_DIR_CHROMIUM);
return format!("{}{}", home, DATA_DIR_CHROMIUM);
}
println!("{}{}", home, DATA_DIR_CHROME);
return format!("{}{}", home, DATA_DIR_CHROME);
}
impl BrowserSession {
pub async fn new() -> Result<Self, anyhow::Error> {
let caps = DesiredCapabilities::chrome();
// TODO: error if chrome is already running
let mut caps = DesiredCapabilities::chrome();
// Use the normal data directory (for extensions and logins)
caps.add_chrome_arg(format!("{}={}", DATA_DIR_ARG, get_data_dir()).as_str())?;
let driver = WebDriver::new("http://localhost:6444", caps).await?;
println!("started browser");
Ok(Self { driver: driver })
}
@ -35,17 +68,31 @@ impl BrowserSession {
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?;
self.handle.fullscreen_window().await?;
println!("!");
self.handle
.get(format!("http://{}", start_url))
.await
.unwrap_or(()); // Ignore errors ig? idfk it errors out in some weirdo way but it works
println!("?");
Ok(())
}
async fn decline_cookies(&self) -> Result<(), anyhow::Error> {
async fn fullscreen_youtube(&self) -> Result<(), anyhow::Error> {
let elems = self
.handle
.find_elements(By::ClassName("ytp-fullscreen-button"))
.await?;
if elems.len() != 0 {
if let Some(button) = elems.first() {
button.click().await?;
}
}
Ok(())
}
async fn youtube(&self) -> Result<(), anyhow::Error> {
self.decline_cookies().await?;
self.fullscreen_youtube().await?;
Ok(())
}