最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Opera Mini Browser Detection Using Javascript - Stack Overflow

programmeradmin1浏览0评论

I have written a javascript code for my website to detect if its running on an Opera Mini browser on mobile devices. Since Opera Mini has a data saving feature, when it is enabled it sometimes doesn't load the site properly, hence I want to display a message by detecting whether the browser used is Opera Mini.

The code posted below works perfectly for Opera Mini on iOS but it doesn't work on Android. Any suggestions to make the code also work for Opera Mini on Android?

<script type="text/javascript">
 function o(){
   var isMobile = {
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
                      },
                  };
     if( isMobile.Opera() ) alert('If you are using Opera Mini please disable Data Savings Mode to Have a pleasant browsing experience :)');
             }
 window.onload = o;
</script>

I have written a javascript code for my website to detect if its running on an Opera Mini browser on mobile devices. Since Opera Mini has a data saving feature, when it is enabled it sometimes doesn't load the site properly, hence I want to display a message by detecting whether the browser used is Opera Mini.

The code posted below works perfectly for Opera Mini on iOS but it doesn't work on Android. Any suggestions to make the code also work for Opera Mini on Android?

<script type="text/javascript">
 function o(){
   var isMobile = {
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
                      },
                  };
     if( isMobile.Opera() ) alert('If you are using Opera Mini please disable Data Savings Mode to Have a pleasant browsing experience :)');
             }
 window.onload = o;
</script>

Share Improve this question asked Apr 15, 2016 at 17:18 Adhip RebelloAdhip Rebello 1452 silver badges12 bronze badges 3
  • 1 Wouldn't it be better to figure out why the site isn't loading properly and fix that issue? – Jeremy J Starcher Commented Apr 15, 2016 at 17:23
  • @JeremyJStarcher When Data Saving mode is enabled the website is sent to opera's servers where the site is pressed and then sent back to the requesting browser, the resulting site will load in a pletely different manner. – Adhip Rebello Commented Apr 15, 2016 at 17:27
  • Would adding a cache-buster prevent that? – Jeremy J Starcher Commented Apr 15, 2016 at 17:44
Add a ment  | 

3 Answers 3

Reset to default 5

Never heard of Opera Mini before, but I did google for it and found https://dev.opera./articles/opera-mini-and-javascript/. Basically, she is using the user agent string to determine if it's opera mini by

var isOperaMini = (navigator.userAgent.indexOf('Opera Mini') > -1);

which is very similar to your method.

However, she also suggests that you can use the window object to determine this. "Opera Mini also includes an operamini object as a property of the window object. To check for the presence of this object, use the following code."

var isOperaMini = Object.prototype.toString.call(window.operamini) === "[object OperaMini]"

If you still can't get it to work, I would propose another approach to this. Approach this problem by trying to save using the saving feature, and if it fails, use the whatever fall back saving feature you intend to use. Therefore, whenever it's opera mini, it would be able to use the data saving feature, but when it isn't opera mini, it would use the alternative feature. Think of a try/catch here. Of course, you would want to consider the implementation when retrieving the saved data as well.

I'm dealing with the same challenge of detecting whether the browser is implementing data saving mode by using a proxy server, but that would include more than Opera Mini in Extreme mode. It would include UC Browser Mini for Android in Speed Mode (very popular in China, India and Indonesia) and Chrome for Android's Data Saver mode. Fortunately, this mode can now be turned off on all three browsers - though many people in the developing world can't afford to, which is probably why the option is so popular.

Dean Hume gives an example of how to detect if the user has turned data saver mode in any browser. But to get those request headers, you would need to use server side scripts, or in his example, a service worker, but not traditional Javascript.

This answer explains how to detect UC Browser Mini in Speed Mode.

Tiffany Brown's article on Dev.Opera, which @Jared Drake cited, does reluctantly remend browser detection for Opera rather than feature detection. As Jared mentioned, you could use:

 var isOperaMini = (navigator.userAgent.indexOf('Opera Mini') > -1);

 var isOperaMini = Object.prototype.toString.call(window.operamini) === "[object OperaMini]"

But she lists some unsupported browser events that maybe you could sniff for, such as mousemove or scroll. And she has written a whole article on Viewing and Exporting Source in Opera Mini. She points out that Opera Mini, at least with the Extreme Data Saver mode, is actually displaying OPML instead of HTML.

Thankfully, Opera Mini offers the ability to inspect the current DOM tree as rendered by Opera's proxy servers. Enter server:source in the Opera Mini address bar to view the source of the currently loaded page.

You might see something in that source that would help your detection, though I didn't see it in mine.

My solution is mostly a simplification of what's proposed already, tested on real device.

Detecting Opera Mini is as simple as this:

if(window.operamini) { /* do something */ }

or

isoperamini = !!window.operamini;  // returns true or false
发布评论

评论列表(0)

  1. 暂无评论