Chrome使用移动用户代理无法正常工作 [英] Chrome use-mobile-user-agent not working

查看:180
本文介绍了Chrome使用移动用户代理无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从命令行运行带有标志的chrome use-mobile-user-agent 不会在移动上下文(用户代理)中打开浏览器。

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

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

注意:


通过用户代理选项确实有效,但是我认为这不是正确的处理方式因为chrome为您提供了在移动环境中引导的标志。

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 so urce代码,我看到以下内容:

Chromium source code

reading some of the chromium source code, i see the following:

定义<$ c $ use-mobile-user-agent 标志中的c> kUseMobileUserAgent :
Chromium应该何时使用移动用户代理。 / p>

define kUseMobileUserAgent from "use-mobile-user-agent" flag: Set when Chromium should use a mobile user agent.

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



shell_content_client.cc



添加移动

shell_content_client.cc

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



其他详细信息(从硒开始)



作为一个额外的细节,我使用硒运行chrome并通过以下配置:

Extra detail (running from selenium)

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

 ...


推荐答案

GetShellUserAgent 中然后在 BuildUserAgentFromProduct ,不使用产品,并将其传递给 BuildUserAgentFromOSAndProduct ,后者应该将字符串格式化为这样的格式;

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

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

产品字符串插入到令牌4中,其中第四个替换令牌在 Safari之前。因此,应将 Chrome / 53.0.2785.116 Mobile放置在此处。

The product string is inserted into token four, where the fourth replacement token is before "Safari". Therefore "Chrome/53.0.2785.116 Mobile" should be placed there.

无论是否带有标志,我的用户代理都是相同的。

With and without the 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 this mean, is it broken? Quite possibly.

src / extensions / shell / common / shell_content_client.cc ShellContentClient :: GetUserAgent 中调用BuildUserAgentFromProduct( Chrome / PRODUCT_VERSION)。只是绕过了 GetShellUserAgent 的调用。

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

好。出现了移动用户代理标志。在其他地方也有可能会更换该产品,但这是罪魁祸首。

Well. There goes the mobile user agent flag. There's other places it's possible for the product to be replaced, but that's the one that sticks out as the culprit.

这篇关于Chrome使用移动用户代理无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆