5

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.

  • 1
    Does the user agent string not give you that information? `navigator.userAgent` https://www.whatismybrowser.com/developers/tools/user-agent-parser/browse/browser-name/uc-browser-user-agents – mwilson Jun 29 '16 at 03:28

3 Answers3

6

Use the navigator.userAgent to see if either Opera Mini or UC Browser is being used.

The following example is how to detect if UCBrowser is being used

if (navigator.userAgent.indexOf(' UCBrowser/') >= 0) {
   //  do stuff here
}

The navigator.userAgent will return a list of stuff in string. Like the example above, navigation.userAgent will return a string of stuff where "UCBrowser" is included as the javascript is used in UC Browser. So the snippet above therefore indexes for such line and determine the condition should the indexed line be found in the userAgent output.

I am not sure whether this will work since I have trouble trying to test my local files on Opera Mini, but based on my research, the below code may work.

if (navigator.userAgent.indexOf('Opera Mini') >= 0) {
   //  do stuff here
}
benChung
  • 186
  • 1
  • 8
4

Solution

To detect UC Mini in JavaScript:

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

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"

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 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/

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\/$/

Or, slightly more conservative:

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

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.

Timo Tijhof
  • 9,597
  • 6
  • 31
  • 45
  • 1
    For reliable JS signals: If you enumerate plugins, UCMini has a specific one called 'QvodInsert'. Also, both `document.clientWidth` and `window.outerWidth` are 0. That's what I use. – hexalys Apr 27 '17 at 08:00
1

navigator.userAgent is what you're looking for, though it is deprecated.

But ideally you don't want to rely on the user agent. (See: https://developer.mozilla.org/en-US/docs/Browser_detection_using_the_user_agent ). If the rendering bug exists in UC Browser/Mini, it may exist in other browsers too. It's better to find a way to fix the bug than to attempt to find and target every browser in which it occurs.

  • 1
    A good answer doesn't require visiting external links in order to answer the question. Please include the relevant parts of those pages in your answer. – 4castle Jun 29 '16 at 03:32
  • 1
    @4castle Edited to reduce need to click links. Thanks for the advice. –  Jun 29 '16 at 03:40