Chrome 使用移动用户代理不工作
Chrome use-mobile-user-agent not working
Chrome 使用移动用户代理 不工作
运行 chrome 来自带有标志 --use-mobile-user-agent
的命令行不会在移动上下文(用户代理)中打开浏览器。
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
Chromium 源代码
阅读一些 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)
作为额外的细节,我 运行 chrome 使用 selenium 并传递配置:
...
"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"
产品字符串被插入到标记四中,其中第四个替换标记在 "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
中,在ShellContentClient::GetUserAgent
中调用了BuildUserAgentFromProduct("Chrome/" PRODUCT_VERSION)
。这只是绕过了对 GetShellUserAgent
.
的调用
嗯。有移动用户代理标志。还有其他地方可能会更换产品,但这是罪魁祸首。
Chrome 使用移动用户代理 不工作
运行 chrome 来自带有标志 --use-mobile-user-agent
的命令行不会在移动上下文(用户代理)中打开浏览器。
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
Chromium 源代码
阅读一些 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)
作为额外的细节,我 运行 chrome 使用 selenium 并传递配置:
...
"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"
产品字符串被插入到标记四中,其中第四个替换标记在 "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
中,在ShellContentClient::GetUserAgent
中调用了BuildUserAgentFromProduct("Chrome/" PRODUCT_VERSION)
。这只是绕过了对 GetShellUserAgent
.
嗯。有移动用户代理标志。还有其他地方可能会更换产品,但这是罪魁祸首。