Skip to content

Commit

Permalink
feat: browser add user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Dec 26, 2023
1 parent 6cbdf3b commit d7cadb0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pub fn cli() -> Result<(), Box<dyn std::error::Error>> {
])
.get_matches();

let buf = std::env::current_dir()
.unwrap()
.join("files/user_agent.toml");
std::env::set_var("user_agent", buf);

if app.subcommand().is_none() {
return crawler::browse_wikipedia(LaunchOptions::default());
}
Expand Down
9 changes: 9 additions & 0 deletions src/handler/crawler.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use headless_chrome::{Browser, LaunchOptions};

use crate::common;

pub fn browse_wikipedia(launch_options: LaunchOptions) -> Result<(), Box<dyn std::error::Error>> {
let browser = Browser::new(launch_options)?;
let tab = browser.new_tab()?;
let random_ug = common::user_agent::random_user_agent();
tab.set_user_agent(random_ug.as_str(), None, None).unwrap();
tab.navigate_to("https://example.com")?;
let h1 = tab.wait_for_xpath("/html/body/div/h1")?;
assert_eq!(h1.get_inner_text().unwrap().as_str(), "Example Domain");
let ug = tab
.evaluate("window.navigator.userAgent", false)?
.value
.unwrap();
assert_eq!(random_ug, ug);
Ok(())
}

0 comments on commit d7cadb0

Please sign in to comment.