Chrome use-mobile-user-agent not working

Chrome use-mobile-user-agent not working

本文关键字:working not use-mobile-user-agent Chrome      更新时间:2023-10-16

Chrome use-mobile-user-agent不工作

在带有--use-mobile-user-agent标志的命令行中运行chrome不能在移动环境(user-agent)中打开浏览器。

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

注意:

传递user-agent选项确实有效,但是我觉得这不是正确的方法,因为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源代码,我看到以下内容:

content_switches.cc

define kUseMobileUserAgent from "use-mobile-user-agent" flag:设置Chromium何时应该使用移动用户代理。

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

shell_content_client.cc

如果我们的变量开关为true/set,则添加"Mobile"到product。

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在使用硒和传递配置:

 ...
 "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中,不使用product,并传递给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的调用。

。移动用户代理标志消失了。还有其他地方可以更换产品,但这是最突出的罪魁祸首。