Skip to content

Commit

Permalink
Fix replace with real UserAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
initprism committed Jan 24, 2023
1 parent c96c976 commit ce087b2
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/browser/tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ pub struct NavigationFailed {
#[error("No LocalStorage item was found")]
pub struct NoLocalStorageItemFound {}

#[derive(Debug, Error)]
#[error("No UserAgent evaluated")]
pub struct NoUserAgentEvaluated {}

impl NoElementFound {
pub fn map(error: Error) -> Error {
match error.downcast::<RemoteError>() {
Expand Down Expand Up @@ -1718,8 +1722,20 @@ impl Tab {
}

fn bypass_user_agent(&self) -> Result<()> {
self.set_user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36", None, None)?;
Ok(())
let object = self.evaluate("window.navigator.userAgent", true)?;

match object.value.map(|x| x.to_string()) {
Some(mut ua) => {
ua = ua.replace("HeadlessChrome/", "Chrome/");

let re = regex::Regex::new(r"\(([^)]+)\)").unwrap();
ua = re.replace(&ua, "(Windows NT 10.0; Win64; x64)").to_string();

self.set_user_agent(&ua, None, None)?;
Ok(())
}
None => Err(NoUserAgentEvaluated {}.into()),
}
}

fn bypass_wedriver(&self) -> Result<()> {
Expand Down

0 comments on commit ce087b2

Please sign in to comment.