本文介绍了Javascript:如何检测用户是否使用UC Browser / Mini?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个在UC Browser / Mini中无法正确呈现的网站。我打算用javascript覆盖样式。在此先感谢。

I am creating a website that is not rendered properly in UC Browser/Mini. I am planning to override the style using javascript. Thanks in advance.

推荐答案

解决方案



检测UC Mini in JavaScript:

Solution

To detect UC Mini in JavaScript:

if (navigator.userAgent.match(/^Mozilla\/5\.0 .+ Gecko\/$/)) {
    // ...
}



背景



官方记录了浏览器中使用的各种用户代理字符串。主要的两种模式是这两种模式,用于UC浏览器和UC Mini。

Background

The official Developer Documentation(1) provided by UC documents the various User-Agent strings used in their browsers. The main two patterns are these two, for UC Browser and UC Mini.

# UC Browser
"Mozilla/5.0 (..) .. UCBrowser/10.7.9.856 U2/1.0.0 .."

# UC Mini with Speed Mode on (enabled by default)
"UCWEB/2.0 (..) .. UCBrowser/10.7.9.856 U2/1.0.0 Mobile"

这里要注意的一件事是UC浏览器和UC Mini是不同的应用程序。 UC浏览器适用于iPhone / iOS和Android,具有各种数据保护选项(包括Speedmode选项),但与UC Mini的速度模式无关,也不会改变用户代理。

One thing to note here is that UC Browser and UC Mini are different apps. UC Browser exists for iPhone/iOS and Android and has various data saver options (including a "Speedmode" option) but isn't related to UC Mini's Speed Mode and doesn't alter the user agent.

UC Mini仅适用于Android。在速度模式下,UC Mini是代理浏览器,这意味着其请求通过远程服务器进行代理。此代理服务器将UCWEB字段添加到用户代理。与Opera Mini类似,UC Mini Speed Mode在远程服务器上呈现内容(包括JavaScript)。有趣的是,此远程Web浏览器中的DOM报告的用户代理与其网络层不同。 UC Mini Speed Mode中的 navigator.userAgent 始终报告以下奇怪的固定字符串,没有任何UCWEB,UCBrowser或客户端自己的用户代理的痕迹:

UC Mini is only available for Android. In Speed Mode, UC Mini is a proxy browser, meaning its requests are proxied through a remote server. This proxy server adds a "UCWEB" field to the user-agent. Similar to Opera Mini, UC Mini Speed Mode renders the content (including JavaScript) on the remote server. Interestingly, the DOM in this remote web browser reports a different user-agent than its network layer. navigator.userAgent in UC Mini Speed Mode consistently reports the following odd-looking fixed string without any trace of "UCWEB", "UCBrowser" or the client's own user agent:

Mozilla/5.0 (X11; U; Linux i686; zh-CN; r:1.2.3.4) Gecko/

此精确字符串用于在速度模式下使用UC Mini for Android的所有客户端。请注意尾部斜杠Linux和zh-CN。我建议使用如下的正则表达式来检测它:

This exact string is used for all clients using UC Mini for Android in Speed Mode. Note the trailing slash, "Linux" and "zh-CN". I suggest using a regex like the following to detect it:

/^Gecko\/$/

或稍微保守一点:

/^Mozilla\/5\.0 .+ Gecko\/$/

这是一个浏览器嗅到噩梦,但这都是我们所拥有的。为了验证,我针对一周的维基百科生产流量(未采样,~100B请求)运行它,零匹配。当然,UC Mini也不匹配,因为它只在DOM中使用该字符串。理论上,其他浏览器也可能有不同用户代理字符串的DOM,但至少我们知道它匹配当前正在使用的任何内容。

It's a browser sniffing nightmare, but it's all we've got. To verify, I ran it against a week's worth of Wikipedia production traffic (unsampled, ~100B requests) with zero matches. UC Mini didn't match either, of course, as it only uses that string in the DOM. In theory other browsers may also have DOMs with different user-agent strings, but at least we know it matches nothing currently in use.


  • ( 1)

  • (1) http://www.ucweb.com/download/UCBrowser_User_Agent_en.pdf

这篇关于Javascript:如何检测用户是否使用UC Browser / Mini?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 04:33