use playwright_rs::Playwright;
use playwright_rs::protocol::ProxySettings;
use tracing::info;
use crate::config::BrowserConfig;
use crate::constants::{STEALTH_ARGS, build_args, filter_harmful_args};
use crate::error::Result;
pub fn build_launch_options(
config: &BrowserConfig,
stealth: bool,
extra_stealth_args: &[String],
) -> playwright_rs::LaunchOptions {
let mut args = build_args(stealth);
args.extend(config.extra_flags.clone());
if stealth {
args.extend(STEALTH_ARGS.iter().map(|s| s.to_string()));
}
args.extend(extra_stealth_args.iter().cloned());
if config.dns_over_https {
args.push("--dns-over-https-templates=https://1.1.1.1/dns-query".into());
}
let args = filter_harmful_args(&args);
let mut opts = playwright_rs::LaunchOptions::new()
.headless(config.headless)
.args(args)
.timeout(config.timeout_ms);
if let Some(ref path) = config.executable_path {
opts = opts.executable_path(path.clone());
}
if let Some(ref proxy) = config.proxy {
let ps = ProxySettings {
server: proxy.server.clone(),
bypass: None,
username: proxy.username.clone(),
password: proxy.password.clone(),
};
opts = opts.proxy(ps);
}
if config.real_chrome {
opts = opts.channel("chrome".into());
}
opts
}
pub async fn launch_playwright() -> Result<Playwright> {
info!("launching Playwright");
Playwright::launch()
.await
.map_err(|e| crate::error::BrowserError::Playwright(e.to_string()))
}