如何在桌面和移动Chrome用户代理之间进行检测?

时间:2022-06-20 01:39:09

For a Chrome Desktop Extension home page, I'm trying to detect whether a user is using Chrome for Desktop or Chrome for Mobile on Android. Currently the script below identifies Android Chrome the same as Desktop chrome. On desktop Chrome it should show "chrome" link; however, if someone is on Chrome for Android, it should show the "mobile-other" link.

对于Chrome桌面扩展主页,我正在尝试检测用户是否在Android上使用Chrome for Desktop或Chrome for Mobile。目前,下面的脚本将Android Chrome与Desktop chrome相同。在桌面Chrome上,它应该显示“chrome”链接;但是,如果有人在Chrome for Android上,它应该显示“移动其他”链接。

Script:

脚本:

<script>$(document).ready(function(){
    var ua = navigator.userAgent;
    if (/Chrome/i.test(ua))
       $('a.chrome').show();

    else if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile/i.test(ua))
       $('a.mobile-other').show();

    else
       $('a.desktop-other').show();
  });</script>

Chrome Android User Agent:

Chrome Android用户代理:

Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>

2 个解决方案

#1


20  

The problem is the user agent will always have "Chrome" whether it is the desktop or mobile version. So you have to check the more specific case first.

问题是用户代理将始终拥有“Chrome”,无论是桌面版还是移动版。所以你必须先检查更具体的案例。

$(document).ready(function(){
    var ua = navigator.userAgent;

    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua))
       $('a.mobile-other').show();

    else if(/Chrome/i.test(ua))
       $('a.chrome').show();

    else
       $('a.desktop-other').show();
});

#2


1  

So to update @imtheman's code according to the newest Chrome for iOS user agent string:

因此,要根据最新的Chrome for iOS用户代理字符串更新@ imtheman的代码:

$(document).ready(function(){
var ua = navigator.userAgent;

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua))
   $('a.mobile-other').show();

else if (/Chrome/i.test(ua))
   $('a.chrome').show();

else
   $('a.desktop-other').show();
});

#1


20  

The problem is the user agent will always have "Chrome" whether it is the desktop or mobile version. So you have to check the more specific case first.

问题是用户代理将始终拥有“Chrome”,无论是桌面版还是移动版。所以你必须先检查更具体的案例。

$(document).ready(function(){
    var ua = navigator.userAgent;

    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua))
       $('a.mobile-other').show();

    else if(/Chrome/i.test(ua))
       $('a.chrome').show();

    else
       $('a.desktop-other').show();
});

#2


1  

So to update @imtheman's code according to the newest Chrome for iOS user agent string:

因此,要根据最新的Chrome for iOS用户代理字符串更新@ imtheman的代码:

$(document).ready(function(){
var ua = navigator.userAgent;

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua))
   $('a.mobile-other').show();

else if (/Chrome/i.test(ua))
   $('a.chrome').show();

else
   $('a.desktop-other').show();
});