Chrome use-mobile-user-agent无效

时间:2016-09-18 15:45:54

标签: c++ google-chrome selenium user-agent chromium

Chrome use-mobile-user-agent 无效

使用标记--use-mobile-user-agent从命令行运行chrome不会在移动环境(用户代理)中打开浏览器。

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

注意:

  

传递用户代理选项确实有用,但是我认为这不是正确的做事方式,因为chrome为您提供了在移动环境中启动此标志。

--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源代码

阅读一些chromium source code,我看到以下内容:

content_switches.cc

kUseMobileUserAgent标志定义"use-mobile-user-agent": 在Chromium使用移动用户代理时设置。

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

shell_content_client.cc

如果我们的变量开关为真/设置,则将“Mobile”添加到产品中。

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);
}

额外细节(从selenium运行)

作为一个额外的细节,我使用selenium运行chrome并传递配置:

 ...

 "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"
     ]
 },

 ...

1 个答案:

答案 0 :(得分:3)

字符串是根据" Chrome / 53.0.2785.116 Mobile"在GetShellUserAgent中,然后在BuildUserAgentFromProduct中,产品未被使用,并传递给BuildUserAgentFromOSAndProduct"Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d" 应该格式化字符串;

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

将产品字符串插入令牌4中,其中第四个替换令牌位于" Safari"之前。因此" Chrome / 53.0.2785.116移动"应放在那里。

有和没有标志,我的用户代理是相同的。

src/extensions/shell/common/shell_content_client.cc

那么这意味着什么呢,它被打破了吗?很可能。

BuildUserAgentFromProduct("Chrome/" PRODUCT_VERSION)中,在ShellContentClient::GetUserAgent中调用GetShellUserAgent。这只是绕过对[ec2-user@mongo-client ~]$ mongo --host s-1/10.1.1.1:27017,10.1.1.2:27017,10.1.1.3:27017 admin MongoDB shell version: 3.2.9 connecting to: s-1/10.1.1.1:27017,10.1.1.2:27017,10.1.1.3:27017/admin 2016-09-27T20:08:26.741+0000 I NETWORK [thread1] Starting new replica set monitor for s-1/10.1.1.1:27017,10.1.1.2:27017,10.1.1.3:27017 2016-09-27T20:08:26.741+0000 I NETWORK [ReplicaSetMonitorWatcher] starting 2016-09-27T20:08:31.738+0000 W NETWORK [thread1] Failed to connect to 10.1.1.2:27017, reason: errno:115 Operation now in progress 2016-09-27T20:08:36.738+0000 W NETWORK [thread1] Failed to connect to 10.1.1.3:27017, reason: errno:115 Operation now in progress 2016-09-27T20:08:41.738+0000 W NETWORK [thread1] Failed to connect to 10.1.1.1:27017, reason: errno:115 Operation now in progress 2016-09-27T20:08:41.738+0000 W NETWORK [thread1] No primary detected for set s-1 2016-09-27T20:08:41.738+0000 I NETWORK [thread1] All nodes for set s-1 are down. This has happened for 1 checks in a row. Polling will stop after 29 more failed checks 2016-09-27T20:08:41.738+0000 E QUERY [thread1] Error: connect failed to replica set s-1/10.1.1.1:27017,10.1.1.2:27017,10.1.1.3:27017 : connect@src/mongo/shell/mongo.js:231:14 @(connect):1:6 exception: connect failed 的调用。

好。有移动用户代理标志。还有其他地方可以替换产品,但那是一个突出的罪魁祸首。