用户代理检测浏览器、引擎、平台、设备、游戏系统

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户代理检测</title>
</head>
<body>
<script>
    var client = {
        engine: {
            ie: 0,
            gecko: 0,
            webkit: 0,
            khtml: 0,
            opera: 0,
            ver: null
        },
        browser: {
            ie: 0,
            firefox: 0,
            safari: 0,
            konq: 0,
            opera: 0,
            chrome: 0,
            ver: null
        },
        system: {
            win: false,
            mac: false,
            x11: false,

            // 移动设备
            iphone: false,
            ipod: false,
            ipad: false,
            ios: false,
            android: false,
            nokiaN: false,
            winMobile: false,

            // 游戏系统
            wii: false,
            ps: false
        }
    };

    var ua = navigator.userAgent;
    // 1.识别呈现引擎, 识别浏览器
    if (window.opera) {
        client.engine.ver = client.browser.ver = window.opera.version();
        client.engine.opera = client.browser.opera = parseFloat(client.engine.ver);
    } else if (/AppleWebKit\/(\S+)/.test(ua)) {
        client.engine.ver = RegExp['$1'];
        client.engine.webkit = parseFloat(client.engine.ver);

        if (/Chrome\/(\S+)/.test(ua)) {
            client.browser.ver = RegExp['$1'];
            client.browser.chrome = parseFloat(client.browser.ver);
        } else if (/Version\\(\S+)/.test(ua)) {
            client.browser.ver = RegExp['$1'];
            client.browser.safari = parseFloat(client.browser.ver);
        } else {
            var safariVersion = 1;
            if (client.engine.webkit < 100) {
                safariVersion = 1;
            } else if (client.engine.webkit < 312) {
                safariVersion = 1.2;
            } else if (client.engine.webkit < 412) {
                safariVersion = 1.3;
            } else {
                safariVersion = 2;
            }
            client.browser.safari = client.browser.ver = safariVersion;
        }
    } else if (/KHTML\/(\S+)/.test(ua)) { // KDE Linux集成的Konqueror,一款基于KHTML开源呈现引擎的浏览器
        client.engine.ver = client.browser.ver = RegExp['$1'];
        client.engine.khtml = client.browser.kong = parseFloat(client.engine.ver);
    } else if (/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)) {
        client.engine.ver = RegExp['$1'];
        client.engine.gecko = parseFloat(client.engine.ver);
        if (/Firefox\/(\S+)/.test(ua)) {
            client.browser.ver = RegExp['$1'];
            client.browser.firefox = parseFloat(client.browser.ver);
        }
    } else if (/MSIE ([^;]+)/.test(ua)) {
        client.browser.ver = client.engine.ver = RegExp['$1'];
        client.browser.ie = client.engine.ie = parseFloat(client.browser.ver);
    }

    // 识别平台
    var p = navigator.platform;
    client.system.win = p.indexOf('Win') === 0;
    client.system.mac = p.indexOf('Mac') === 0;
    client.system.x11 = p.indexOf('X11') === 0; // X11代表Unix;

     // windows平台检测
    if (client.system.win) {
        if (/Win(?:dows )?([^do]{2})\s?(\d+\.\d+)/.test(ua)) {
            if (RegExp['$1'] == 'NT') {
                switch(RegExp['$2']) {
                    case '5.0':
                        client.system.win = '2000';
                        break;
                    case '5.1':
                        client.system.win = 'XP';
                        break;
                    case '6.0':
                        client.system.win = 'Vista';
                        break;
                    case '6.1':
                        client.system.win = 'NT';
                        break;
                }
            } else if (RegExp['$1'] == '9x') {
                client.system.win = 'ME';
            } else {
                client.system.win = RegExp['$1'];
            }
        }
    }

    // 移动设备
    client.system.iphone = ua.indexOf('iPhone') > -1;
    client.system.ipod = ua.indexOf('iPod') > -1;
    client.system.ipad = ua.indexOf('iPad') > -1;

    // 检测IOS版本
    if (client.system.mac && ua.indexOf('Mobile') > -1) {
        if (/CPU (?:iPhone ) ?OS (\d+_\d+)/.test(ua)) {
            client.system.ios = parseFloat(RegExp.$1.replace('_', '.'));
        } else {
            client.system.ios = 2; // 不能真正检测出来,所以只能猜测
        }
    }

     // 检测Android版本
    if (/Android (\d+\.\d+)/.test(ua)) {
        client.system.andriod = parseFloat(RegExp.$1);
    }

    // 检测诺基亚手机
    client.system.nokiaN = ua.indexOf('NokiaN') > -1;

    // Windows Mobile
    client.system.winMobile = (client.system.win == 'CE');
    if (client.system.win == 'CE') { // Pocket PC 和 Smartphone
        client.system.winMobile = client.system.win;
    } else if (client.system.win == 'Ph') { // window phone
        if (/Window Phone OS (\d+.\d+)/.test(ua)) {
            client.system.win = 'Phone';
            client.system.winMobile = parseFloat(RegExp['$1']);
        }
    }

    // 游戏系统  任天堂Wii和Playstation3或者内置浏览器
    client.system.wii = ua.indexOf('Wii') > -1;
    client.system.ps = /playstation/i.test(ua);

    console.log(client.engine);
</script>
</body>
</html>

 

浏览器类型检测精简版

    // Opera 8.0+
    var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

    // Firefox 1.0+
    var isFirefox = typeof InstallTrigger !== 'undefined';

    // Safari 3.0+ "[object HTMLElementConstructor]"
    var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) {
        return p.toString() === "[object SafariRemoteNotification]";
    })(!window['safari'] || safari.pushNotification);

    // Internet Explorer 6-11
    var isIE = /*@cc_on!@*/ false || !!document.documentMode;

    // Edge 20+
    var isEdge = !isIE && !!window.StyleMedia;

    // Chrome 1+
    var isChrome = !!window.chrome && !!window.chrome.webstore;

    // Blink engine detection
    var isBlink = (isChrome || isOpera) && !!window.CSS;

摘自金山云,参考:https://github.com/ksvc/h265webplayer

posted @ 2017-12-10 19:04  井凉一一  阅读(259)  评论(0编辑  收藏  举报