通过JS解析手机浏览器UA标志中的各种设备信息

通过获取手机浏览器的UA标志后,对UA字符串进行解析,得出手机的各种基本信息。

/*
 * @name Operating System
 * @desc
Currently  is only to userAgent
 */
(function () {
    if (module.declare === undefined) {
        throw 'There is no global module.declare method!';
    }
    //userAgent regexp
    var Exp_USERAGENT = {
        //browser
        MSIE : /(msie) ([\w.]+)/,
        MOZILLA : /(mozilla)(?:.*? rv:([\w.]+)|)/,
        SAFARI : /(safari)(?:.*version|)[\/]([\d.]+)/,
        CHROME : /(chrome|crios)[\/]([\w.]+)/,
        OPERA : /(opera|opr)(?:.*version|)[\/]([\w.]+)/,
        WEBOS : /(webos|hpwos)[\s\/]([\d.]+)/,
        DOLFIN : /(dolfin)(?:.*version|)[\/]([\w.]+)/, //
        SILK : /(silk)(?:.*version|)[\/]([\w.]+)/, //
        UC : /(uc)browser(?:.*version|)[\/]([\w.]+)/, //
        TAOBAO : /(tao|taobao)browser(?:.*version|)[\/]([\w.]+)/,
        LIEBAO : /(lb)browser(?:.*? rv:([\w.]+)|)/,
        //AMAYA:/(amaya)[\/]([\w.]+)/,
        //SEAMONKEY:/(seamonkey)[\/]([\w.]+)/,
        //OMNIWEB:/(omniweb)[\/]v([\w.]+)/,
        //FLOCK:/(flock)[\/]([\w.]+)/,
        //EPIPHANY:/(epiphany)[\/]([\w.]+)/,
        MicroMessenger :/micromessenger/i,//

        //engine
        WEBKIT : /webkit[\/]([\w.]+)/,
        GECKO : /gecko[\/]([\w.]+)/, //
        PRESTO : /presto[\/]([\w.]+)/, //
        TRIDENT : /trident[\/]([\w.]+)/,
        
        //device
        MAC : /(mac os x)\s+([\w_]+)/, //
        WINNDOWS : /(windows nt)\s+([\w.]+)/, //
        LINUX : /linux/, //
        //IOS : /i(?:pad|phone|pod)(?:.*)cpu(?: iphone)? os/,
        IOS : /(i(?:pad|phone|pod))(?:.*)cpu(?: i(?:pad|phone|pod))? os (\d+(?:[\.|_]\d+){1,})/,
        ANDROID : /(android)\s+([\d.]+)/,
        WINDOWSPHONE : /windowsphone/, //
        IPAD : /(ipad).*os\s([\d_]+)/,
        IPHONE : /(iphone\sos)\s([\d_]+)/,
        IPOD : /(ipod)(?:.*)cpu(?: iphone)? os (\d+(?:[\.|_]\d+){1,})/,
        TOUCHPAD : /touchpad/,
        BLACKBERRY : /(playbook|blackberry|bb\d+).*version\/([\d.]+)/,
        RIMTABLET : /rimtablet/, //
        BADA : /bada/, //
        CHROMEOS : /cromeos///
    };
    //define os module
    module.declare('os', function (require, exports, module) {
        function detect(ua) {
            var os = {},
            //browser
            chrome = ua.match(Exp_USERAGENT.CHROME),
            opera = ua.match(Exp_USERAGENT.OPERA),
            msie = ua.match(Exp_USERAGENT.MSIE),
            safari = (ua + ua.replace(Exp_USERAGENT.SAFARI, ' ')).match(Exp_USERAGENT.SAFARI), //modify the jquery bug
            mozilla = ua.match(Exp_USERAGENT.MOZILLA),
            webos = ua.match(Exp_USERAGENT.WEBOS),
            dolphi = ua.match(Exp_USERAGENT.DOLFIN),
            silk = ua.match(Exp_USERAGENT.SILK),
            uc = ua.match(Exp_USERAGENT.UC),
            taobao=ua.match(Exp_USERAGENT.TAOBAO),
            liebao=ua.match(Exp_USERAGENT.LIEBAO),
            
            //engine
            webkit = ua.match(Exp_USERAGENT.WEBKIT),
            gecko = ua.match(Exp_USERAGENT.GECKO),
            presto = ua.match(Exp_USERAGENT.PRESTO),
            trident = ua.match(Exp_USERAGENT.TRIDENT),
            
            //device
            mac = ua.match(Exp_USERAGENT.MAC),
            windows = ua.match(Exp_USERAGENT.WINNDOWS),
            linux = ua.match(Exp_USERAGENT.LINUX),
            chromeos = ua.match(Exp_USERAGENT.CHROMEOS),
            
            //pad
            ipad = ua.match(Exp_USERAGENT.IPAD),
            rimtablet = ua.match(Exp_USERAGENT.RIMTABLET),
            touchpad = webos && ua.match(Exp_USERAGENT.TOUCHPAD),
            
            //mobile
            ios = ua.match(Exp_USERAGENT.IOS),
            ipod = ua.match(Exp_USERAGENT.IPOD),
            iphone = !ipad && ua.match(Exp_USERAGENT.IPHONE),
            android = ua.match(Exp_USERAGENT.ANDROID),
            windowsphone = ua.match(Exp_USERAGENT.WINDOWSPHONE),
            blackberry = ua.match(Exp_USERAGENT.BLACKBERRY),
            bada = ua.match(Exp_USERAGENT.BADA);
            
            //engine
            if (webkit)
                os.webkit = true;
            if (gecko)
                os.gecko = true;
            if (presto)
                os.presto = true;
            if (trident)
                os.trident = true;
            //device
            os['device'] = {};
            //
            if (mac)
                os.mac = true, os['device']['name'] = 'mac os', os.version = mac[2];
            if (windows)
                os.windows = true, os['device']['name'] = 'window', os.version = windows[2];
            if (linux)
                os.linux = true, os['device']['name'] = 'linux'; //
            if (chromeos)
                os.chromeos = true, os['device']['name'] = 'chromeos', os.version = chromeos[2];
            //if (ios) os.ios=true;//
            if (android)
                os.android = true, os['device']['name'] = 'android', os.version = android[2];
            if (iphone)
                os.ios = true, os['device']['name'] = 'iphone',os.version = iphone[2].replace(/_/g, '.'), os.iphone = true;
            if (ipod)
                os.ios = true, os['device']['name'] = 'ipod',os.version = ipod[2].replace(/_/g, '.'), os.ipod = true;
            if (ipad)
                os.ios = true, os['device']['name'] = 'ipad', os.version = ipad[2].replace(/_/g, '.'), os.ipad = true;
            if (webos)
                os.webos = true, os['device']['name'] = 'webos',os.version = webos[2];
            if (blackberry)
                os.blackberry = true, os['device']['name'] = 'blackberry', os.version = blackberry[2];
            if (bada)
                os.bada = true, os['device']['name'] = 'bada',os.version = ''; //
            if (rimtablet)
                os.rimtablet = true, os['device']['name'] = 'rimtablet', os.version = ''; //
            if (touchpad)
                os.touchpad = true, os['device']['name'] = 'touchpad',os.version = ''; //
            //os.device.version
            os['device']['version'] = os.version;
            if (!(android || iphone || ipad || ipod || webos || blackberry || bada || rimtablet || touchpad))
                os.desktop = true, os.version = '';
            //browser
            var match = dolphi || silk || uc || msie || taobao || liebao || opera || chrome || safari || (ua.indexOf('compatible') < 0 && mozilla);
            //chrome
            match[1] = match[1] === 'crios' ? 'chrome' : match[1];
            //taobao
            match[1] = match[1] === 'tao' ? 'taobao' : match[1];
            //
            os[match[1]] = true;
            os['browser'] = match[1];
            os['version'] = match[2]||'';
            //major
            os['version'] && (os['major'] = parseInt(os['version'],10));
            //revise
            //safari
            if (os.ios && os.webkit && !os.desktop) {
                try{
                    //in node js
                    os.safari = (window.canSetSearchEngine || window.TrackEvent) ? true : false;
                }catch(e){}
                var v=os['major']||parseInt(os['device-version'],10)||'';
                v && (os['ios'+v]=true);
            }
            //ie 11
            if(os.trident && os.major >= 11){
                os.browser='msie';
                os.msie=true;
                delete os.mozilla;
            }
            //mozilla/firefox
            if (os.mozilla) {
                os.firefox = true;
            }
            //opera
            if(os.browser === 'opr'){
                os.browser = 'opera';
                os.opera=os.opr;
            }
            //blackberry
            if(os.blackberry){
                //
                delete os.safari;
            }
            //MicroMessager
            if(Exp_USERAGENT.MicroMessenger.test(ua)){
                //
                os.micromessage=true;
            }
            //uc
            var DOMWindow = DOMWindow || {};
            if (DOMWindow && DOMWindow.UCNewsJSController) {
                os.uc = true,
                os.browser = 'uc';
            }
            //
            try{
                //orientation
                os.orientation = (window.orientation === 180 || window.orientation === 0) ? 'portrait' : 'landscape';
            }catch(e){}
            //os.device.type
            if(os.desktop){
                os.device['type']='desktop';
            }else{
                os.device['type']='mobile';
            }
            //
            return os;
        };
        //navigator.userAgent.toLowerCase()
        return detect(navigator.userAgent.toLowerCase());
    });
})();


解析UserAgent字符串是识别用户设备浏览器特性的关键技术之一。为了帮助你掌握这一技能,并解决当前问题,推荐参考《2022年微信浏览器UA分析:最新10万条数据》。在这份资料,你将找到大量微信浏览器UserAgent的实例,以及它们所包含的丰富信息。 参考资源链接:[2022年微信浏览器UA分析:最新10万条数据](https://wenku.csdn.net/doc/7oaaa6gyrg) 要通过JavaScript解析UserAgent,你可以使用正则表达式来匹配并提取所需的字段。以下是一个示例代码,展示如何在微信浏览器解析UserAgent字符串: ``` function parseUserAgent(ua) { const uaPattern = /Mozilla\/5.0 \((.+); Android (\d+)\.(\d+); (.+) Android; (.+); (ARM64|armeabi-v7a|armeabi); (\S+); (MicroMessenger|Weixin)\/(\d+\.\d+\.\d+\.\d+); (WIFI|4G|NETTYPE\/\S+); (Language|language)\/(\S+)/; const match = uaPattern.exec(ua); if (match) { const [fullUA, platformInfo, majorOS, minorOS, deviceInfo, brand, abi, netType, wechatVersion, wechatName, language] = match; return { platformInfo, majorOS, minorOS, deviceInfo, brand, abi, netType, wechatVersion, wechatName, language }; } return null; } // 示例使用 const userAgent = navigator.userAgent; const parsedUA = parseUserAgent(userAgent); console.log(parsedUA); ``` 在这个示例,我们定义了一个`parseUserAgent`函数,它接受一个UserAgent字符串作为参数,并返回一个包含解析信息的对象。使用正则表达式`uaPattern`来匹配UserAgent字符串的各个部分。一旦匹配成功,就可以从`match`数组提取相关信息。这个函数可以被用来检查微信浏览器的UserAgent,从而获得用户设备、操作系统、微信版本、网络类型等信息。 通过这样的解析,开发者可以针对不同的设备浏览器特性,为用户提供更加个性化和优化的体验。在你的开发实践,建议将解析到的信息用于网站兼容性测试和性能调优。通过《2022年微信浏览器UA分析:最新10万条数据》的数据进行比对和验证,可以进一步提升你对微信浏览器UserAgent解析的理解和准确性。 参考资源链接:[2022年微信浏览器UA分析:最新10万条数据](https://wenku.csdn.net/doc/7oaaa6gyrg)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值