Using Chrome-mobile-user-agent does not work

Using Chrome-mobile-user-agent does not work

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

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

Note:

passing the user-agent parameter really works, but I feel that this is not the right way to do things, since chrome offers you this flag to load in a 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 

Chrome source code

after reading some chrome source code, I see the following:

content_switches.cc

Define kUseMobileUserAgent from the "use-mobile-user-agent" flag: Set when Chromium should use the mobile user agent.

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

shell_content_client.cc

add "Mobile" to the 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); } 

Additional information (works from selenium)

As an additional 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" ] }, ... 
+5
source share
1 answer

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

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

The product line is inserted in token 4, where the fourth replacement token is before "Safari". Therefore, "Chrome / 53.0.2785.116 Mobile" should be placed there.

With and without a 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 that mean, is it broken? Quite possible.

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

Well. The mobile user flag appears. In other places, it is possible for the product to be replaced, but one that sticks out as the culprit.

+3
source

All Articles