Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome use-mobile-user-agent not working

Chrome use-mobile-user-agent not working

Running chrome from command line with flag --use-mobile-user-agent does not open the browser in mobile context (user-agent).

chrome --use-mobile-user-agent= true

Note:

passing user-agent option does work, but i feel its not the right way of doing things as chrome offers you this flag to boot in mobile context.

--user-agent= Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; ar) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3

Chromium source code

reading some of the chromium source code, i see the following:

content_switches.cc

define kUseMobileUserAgent from "use-mobile-user-agent" flag: Set when Chromium should use a mobile user agent.

const char kUseMobileUserAgent[] = "use-mobile-user-agent";

shell_content_client.cc

add "Mobile" to product if our variable switch is true/set.

std::string GetShellUserAgent() {
  std::string product = "Chrome/" CONTENT_SHELL_VERSION;
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(switches::kUseMobileUserAgent))
    product += " Mobile";
  return BuildUserAgentFromProduct(product);
}

Extra detail (running from selenium)

As an extra detail, i run chrome in using selenium and pass the configurations:

 ...

 "browserName": "chrome",
 "chromeOptions": {
     "args": [
         "--user-agent= Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; ar) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3",
         "--window-size=320,640",
         "--disable-popup-blocking",
         "--incognito",
         "--test-type"
     ]
 },

 ...
like image 570
Bamieh Avatar asked Sep 18 '16 15:09

Bamieh


People also ask

How do I change the user agent in Chrome mobile?

While there's no quick toggle to change your user agent in the mobile versions of Chrome and Safari, you can easily make websites think your phone is a computer. On Android, open Chrome and tap the three-dot Menu button in the top-right. Check the Desktop site box and it will reload to show you the full version.

How do I bypass user agent?

Press Command + Shift + P (Mac) or Control + Shift + P (Windows, Linux, ChromeOS) to open the Command Menu. Type network conditions , select Show Network conditions, and press Enter to open the Network conditions tab. In the User agent section disable the Select automatically checkbox.

How do I change my browser user agent in Chrome?

This includes old Android versions, Microsoft Edge, Opera, Firefox, Chrome, Safari, and UC Browser on different operating systems. Select any of the user-agent here and then reload the page (press F5 key), and make sure the Inspect element window is when you do it. The reloaded page will open with the new user-agent.

How do I change user agent on Android?

Switch to the “Advanced” tab in the top-right corner, then tap “User agent” at the top of the “Customize” sub-section. Tap “User agent” at the top of the “Customize” sub-section of the “Advanced” tab. Select one of the four built-in user agents or tap “Custom” and enter your own value, then tap “OK” to save.


1 Answers

The string is built to "Chrome/53.0.2785.116 Mobile" in GetShellUserAgent, then in BuildUserAgentFromProduct, product is not used, and passed on to BuildUserAgentFromOSAndProduct, which is supposed to format a string as such;

"Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d"

The product string is inserted into token four, where the fourth replacement token is before "Safari". Therefore "Chrome/53.0.2785.116 Mobile" should be placed there.

With and without the flag, my user agent is the same.

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36

So what does this mean, is it broken? Quite possibly.

In src/extensions/shell/common/shell_content_client.cc, BuildUserAgentFromProduct("Chrome/" PRODUCT_VERSION) is called in ShellContentClient::GetUserAgent. That just circumvents the call to GetShellUserAgent.

Well. There goes the mobile user agent flag. There's other places it's possible for the product to be replaced, but that's the one that sticks out as the culprit.

like image 139
TylerY86 Avatar answered Oct 22 '22 20:10

TylerY86