Chrome use-mobile-user-agent 不工作
使用标志 --use-mobile-user-agent
从命令行运行 chrome 不会在移动上下文(用户代理)中打开浏览器。
chrome --use-mobile-user-agent= true
注意:
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
Chrome 源代码
阅读一些 chromium source code ,我看到以下内容:
content_switches.cc
从 "use-mobile-user-agent"
标志定义 kUseMobileUserAgent
:
设置 Chromium 何时应使用移动用户代理。
const char kUseMobileUserAgent[] = "use-mobile-user-agent";
shell_content_client.cc
如果我们的变量 switch 为 true/set,则将“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"
]
},
...
最佳答案
该字符串在 GetShellUserAgent
中构建为“Chrome/53.0.2785.116 Mobile” , 然后在 BuildUserAgentFromProduct
,产品未使用,并传递给 BuildUserAgentFromOSAndProduct
,它应该格式化一个字符串;
"Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d"
产品字符串被插入到标记 4 中,其中第四个替换标记在“Safari”之前。因此“Chrome/53.0.2785.116 Mobile”应该放在那里。
有和没有标志,我的用户代理是一样的。
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
那么这是什么意思,是坏了吗?很有可能。
在src/extensions/shell/common/shell_content_client.cc
, BuildUserAgentFromProduct("Chrome/" PRODUCT_VERSION)
在 ShellContentClient::GetUserAgent
中调用.这只是绕过了对 GetShellUserAgent
的调用.
嗯。有移动用户代理标志。产品可能在其他地方被更换,但罪魁祸首就是那个地方。
关于c++ - Chrome 使用移动用户代理不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559523/