JavaScriptのクライアント検出

21913 ワード

[ブログ]モジュール拡張モードによるクライアント検出
ラベル:ブログ
一般的な検出方法は次のとおりです.
  • [ ] 1 . 能力検査
  • [ ] 2 . ユーザエージェント検出
  • ここでは2ユーザエージェントが検出しています
    検出カードP 211
     IE   : 
    navigator.plugins             ,            :
        name :      .
        description:      
        filename:       
        length:       MIME    
    
            //     ( IE   ):
            function hasPlugin(name){
                name = name.toLowerCase(); //toLowerCase()              
                for(var i=0;i-1){ // >-1    
                        return true
                    }
                }
                return false;
            };
                
            //   Flash
            alert(hasPlugin("Flash"));
            
            //   QuickTime
            alert(hasPlugin("QuickTime"));
                
    
    IE   
            //   IE   
            //  ========== 
            //  = IE COM         , COM            ,             = 
            //  = COM   ,  ,Flash     ShockwaveFlash.ShockwaveFlash = 
            //  ========== 
            function hasIEPlugin(name){
                try{
                    new ActiveXOBject(name);
                    return true;
                }catch(e){
                    return false;
                    //TODO handle the exception
                }
            };
            
            // IE   Flash
            alert(hasIEPlugin("ShockwaveFlash.ShockwaveFlash"));
    
    
             
            //      2     ,          FLash
            function hasFLash(){
                var result = hasPlugin("Flash");
                if(!result){
                    result.hasIEPlugin("ShockwaveFlash.ShockwaveFlash");
                }
                return result; // true or false
            };
            
            function hasQuickTime(){
                var result = hasPlugin("QuickTime");
                if (!result) {
                    result = hasIEPlugin("QuickTime.Quicktime");
                } 
                return result;
            };
    

    ブラウザエンジンの検出:
    
                var client = function (){
                    var engine = {
                        //     
                        ie:0, // IE     
                        gecko:0, // Firefox         
                        webkit:0, // google->chrome apple->safari  
                        khtml:0, // Konqueror(linux         )   ->KHTML
                        opera:0, // Opera
                        
                        //       
                        ver : null
                    };
                    
                    //       ,   ,   
                    return {
                        engine : engine
                    };
                }();
                
                
                //  ====================== 
                //  =      =>       = 
                //  ======================
                var ua = navigator.userAgent;
                console.log(ua);
                //  =       Opera = 
                if(window.opera){ //    Opera   ,         
                    client.ver = window.opera.version(); // window.opera Opera 5.0        , Opera 7.6      ,  version()                   
                    client.opera = parseFloat(client.ver);
                }else if(/AppleWebKit\/(\S+)/.test(ua)){
                    //  =      Webkit = 
                    // webkit          "AppleWebkit"      ,          
                    // \S :           
                    client.ver = RegExp["$1"];
                    client.webkit = parseFloat(client.ver);
                }else if(/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)){
                    //  =      KHTML = 
                    //  Konqueror 3.1         KHTML   ,     Konqueror      
                    client.ver = RegExp["$1"];
                    client.khtml = parseFloat(client.ver)                
                }else if(/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)){
                    //  =      Gecko = 
                    // "Gecko"        "rv:"   ;   "rv:"   ;
                    client.ver = RegExp["$1"];
                    client.khtml = parseFloat(client.ver)
                }else if(/MSIE ([^;]+)/.test(ua)){
                    //  =      IE = 
                    client.ver = RegExp["$1"];
                    client.ie = parseFloat(client.ver)
                }
                
                
                //     
                if(client.ie){ //    IE    , client.ie    0
                    //   IE   
                    alert("  IE ie  ");
                }else if (client.gecko > 1.5){ //    gecko    (firefox)
                    if(client.ver == "1.8.1"){
                        //                              
                    }
                    alert("  firefox gecko  ");
                }else if(client.webkit){
                    alert("  chrome webkie  ");
                }else if(client.khtml){
                    alert("  KHTML   ");
                }else if(client.opera){
                    alert("  opera  ");
                }
    
    
    

    ブラウザブランドの検出:
                var client = function (){
                    var engine = {
                        //     
                        ie:0, // IE     
                        gecko:0, // Firefox         
                        webkit:0, // google->chrome apple->safari  
                        khtml:0, // Konqueror(linux         )   ->KHTML
                        opera:0, // Opera
                        
                        //       
                        ver : null
                    };
                    
                    var browser = {
                        //   
                        ie : 0,
                        firefox:0,
                        safari:0,
                        honq:0,
                        opera:0, 
                        chrome:0,
                        
                        //       
                        ver : null
                    };
                    
                    
                    //       ,   ,   
                    return {
                        engine : engine,
                        browser : browser
                    };
                }();
                
                
                //  ====================== 
                //  =      =>       = 
                //  =      =>      =  
                //  ====================== 
                var ua = navigator.userAgent; 
                //  =       Opera =  
                var o = window.opera || window.opr
                console.log(ua);
                if(o){ //    Opera   ,         
                    client.engine.ver = client.browser.ver = (/OPR\/(.*)/.exec(ua))[1]; // window.opera Opera 5.0        , Opera 7.6      ,  version()                   
                    client.engine.opera = client.browser.opera = parseFloat(client.engine.ver);
                }else if(/AppleWebKit\/(\S+)/.test(ua)){
                    //  =      Webkit = 
                    // webkit          "AppleWebkit"      ,          
                    // \S :         
                    client.engine.ver = RegExp["$1"];
                    client.engine.webkit = parseFloat(client.engine.ver);
    
                    //   Chrome    Safari
                    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.webkit < 100){
                            safariVersion = 1;
                        }else if(client.webkit < 312){
                            safariVersion = 1.2;
                        }else if(client.webkit < 412){
                            safariVersion = 1.3;
                        }else{
                            safariVersion = 2;
                        }
                        client.browser.safari = client.browser.ver = safariVersion;
                    }
                }else if(/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)){
                    //  =      KHTML = 
                    //  Konqueror 3.1         KHTML   ,     Konqueror      
                    client.engine.ver = client.browser.ver = RegExp["$1"];
                    client.engine.khtml = client.browser.konq = parseFloat(client.engine.ver)
                }else if(/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)){
                    //  =      Gecko = 
                    // "Gecko"        "rv:"   ;   "rv:"   ;
                    client.engine.ver = RegExp["$1"];
                    client.engine.gecko = parseFloat(client.engine.ver);
                    
                    //     firefox
                    if(/Firefox\/(\S+)/.test(ua)){
                        client.browser.ver = RegExp["$1"];
                        client.browser.firefox = parseFloat(client.browser.ver);
                    }
                }else if(/MSIE ([^;]+)/.test(ua)){
                    //  =      IE = 
                    client.engine.ver = client.browser.ver = RegExp["$1"];
                    client.engine.ie = client.browser.ie = parseFloat(client.engine.ver)
                }
                
                
                //         
                if(client.engine.ie){ //    IE    , client.engine.ie    0
                    //   IE   
                    alert("  IE ie  ");
                }else if (client.engine.gecko > 1.5){ //    gecko    (firefox)
                    if(client.engine.ver == "1.8.1"){
                        //                              
                    }
                    alert("  firefox gecko  ");
                }else if(client.engine.webkit){
                    alert("  webkie  ");
                }else if(client.engine.khtml){
                    alert("  KHTML   ");
                }else if(client.engine.opera){
                    alert("  opera  ");
                }
                
                //          
                if(client.engine.webkit){ //    webkit  
                    if(client.browser.chrome){
                        alert("  google chrome")
                    }else if(client.browser.safari){
                        alert("  Safari")
                    }
                }else if(client.engine.gecko){
                    if(client.browser.firefox){
                        alert("    ")
                    }else{
                        alert("gecko.....")
                    }
                }else if(client.browser.ie){
                    alert("  IE")
                }else if(client.browser.opera){
                    alert("  Opera")
                }
                

    ユーザエージェント検出要約(上のコードを組み込む)
  • この方法はユーザエージェント文字列に大きな依存性を有し,
  • は推奨されない.
                //              
                var client = function() {
                    var engine = {
                        //     
                        ie: 0, // IE     
                        gecko: 0, // Firefox         
                        webkit: 0, // google->chrome apple->safari  
                        khtml: 0, // Konqueror(linux         )   ->KHTML
                        opera: 0, // Opera
    
                        //       
                        ver: null
                    };
    
                    var browser = {
                        //    
                        ie: 0,
                        firefox: 0,
                        safari: 0,
                        konq: 0,
                        opera: 0,
                        chrome: 0,
    
                        //       
                        ver: null
                    };
    
                    var system = {
                        //   ,        
                        win: false,
                        mac: false,
                        x11: false, //   1
    
                        //     
                        iphone: false,
                        ipod: false,
                        ipad: false,
                        ios: false,
                        android: false,
                        nokiaN: false, //    N  
                        winMobile: false, // window phone  
    
                        //     
                        wii: false, //    
                        ps: false
                    }
    
                    //           
                    var ua = navigator.userAgent;
                    //  =       Opera =  
                    var o = window.opera || window.opr;
                    if(o) { //    Opera   ,         
                        engine.ver = browser.ver = (/OPR\/(.*)/.exec(ua))[1]; // window.opera Opera 5.0        , Opera 7.6      ,  version()                   
                        engine.opera = browser.opera = parseFloat(engine.ver);
                    } else if(/AppleWebKit\/(\S+)/.test(ua)) {
                        //  =      Webkit = 
                        // webkit          "AppleWebkit"      ,          
                        // \S :         
                        engine.ver = RegExp["$1"];
                        engine.webkit = parseFloat(engine.ver);
    
                        //   Chrome    Safari
                        if(/Chrome\/(\S+)/.test(ua)) {
                            browser.ver = RegExp["$1"];
                            browser.chrome = parseFloat(browser.ver);
                        } else if(/Version\/(\S+)/.test(ua)) {
                            browser.ver = RegExp["$1"];
                            browser.safari = parseFloat(browser.ver);
                        } else {
                            //        
                            var safariVersion = 1;
                            if(engine.webkit < 100) {
                                safariVersion = 1;
                            } else if(engine.webkit < 312) {
                                safariVersion = 1.2;
                            } else if(engine.webkit < 412) {
                                safariVersion = 1.3;
                            } else {
                                safariVersion = 2;
                            }
                            browser.safari = browser.ver = safariVersion;
                        }
                    } else if(/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
                        //  =      KHTML = 
                        //  Konqueror 3.1         KHTML   ,     Konqueror      
                        engine.ver = browser.ver = RegExp["$1"];
                        engine.khtml = browser.konq = parseFloat(engine.ver)
                    } else if(/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)) {
                        //  =      Gecko = 
                        // "Gecko"        "rv:"   ;   "rv:"   ;
                        engine.ver = RegExp["$1"];
                        engine.gecko = parseFloat(engine.ver);
    
                        //     firefox
                        if(/Firefox\/(\S+)/.test(ua)) {
                            browser.ver = RegExp["$1"];
                            browser.firefox = parseFloat(browser.ver);
                        }
                    } else if(/MSIE ([^;]+)/.test(ua)) {
                        //  =      IE = 
                        engine.ver = browser.ver = RegExp["$1"];
                        engine.ie = browser.ie = parseFloat(engine.ver)
                    }
    
                    //     
                    var p = navigator.platform; //       :Win32, Win64, MacPPC, MacIntel, X11, Linux i686;
                    //     ,     
                    system.win = p.indexOf("Win") == 0;
                    system.mac = p.indexOf("Mac") == 0;
                    system.x11 = (p.indexOf("X11") == 0) || (p.indexOf("Linux") == 0);
    
                    //   Windows     
                    if(system.win) {
                        if(/Win(?:dows )?([^do]{2})\s?(\d+\.\d+)?/.test(ua)) {
                            if(RegExp["$1"] == "NT") {
                                switch(RegExp["$2"]) {
                                    case "5.0":
                                        system.win = "2000";
                                        break;
                                    case "5.1":
                                        system.win = "Xp";
                                        break;
                                    case "6.0":
                                        system.win = "Vista";
                                        break;
                                    case "6.1":
                                        system.win = "7";
                                        break;
                                    default:
                                        system.win = "NT";
                                        break;
                                }
                            } else if(RegExp["$1"] == "9x") {
                                system.win = "ME";
                            } else {
                                system.win = RegExp["$1"];
                            }
                        }
                    }
                    
                    //       
                    system.iphone = ua.indexOf("iPone") > -1;
                    system.ipod = ua.indexOf("iPod") > -1;
                    system.ipad = ua.indexOf("iPad") > -1;
                    system.nokiaN = ua.indexOf("NokiaN") > -1;
                    // windows mobile  ,   windows CE
                    if(system.win == "CE"){ //     windows Mobile
                        system.winMobile = system.win;
                    }else if(system.win == "Ph"){
                        if(/Window Phone OS (\d+.\d+)/.test(ua)){ // windows phone7   
                            system.win = "Phone";
                            system.winMobile = parseFloat(RegExp["$1"]);
                        }
                    }
                    //   IOS  
                    if(system.mac && ua.indexOf("Mobile") > -1){
                        if(/CPU (?:iPhone )?OS (\d+_\d+)/.test(ua)){
                            system.ios = parseFloat(RegExp.$1.replace("_", "."));
                        }else{
                            system.ios = 2; //     ,       
                        }
                    }
                    //   Android   
                    if(/Android (\d+\.\d+)/.test(ua)){
                        system.android = parseFloat(RegExp.$1);                    
                    }
                    
                    //       
                    system.wii = ua.indexOf("Wii") > -1;
                    system.ps = /playstation/i.test(ua);
    
                    //       ,   ,   
                    return {
                        engine: engine,
                        browser: browser,
                        system: system
                    };
                }();
    
  • []ステップアップ操作↑検出:
  • 
                //         
                if(client.engine.ie) { //    IE    , engine.ie    0
                    //   IE   
                    alert("  IE ie  ");
                } else if(client.engine.gecko > 1.5) { //    gecko    (firefox)
                    if(client.engine.ver == "1.8.1") {
                        //                              
                    }
                    alert("  firefox gecko  ");
                } else if(client.engine.webkit) {
                    alert("  webkie  ");
                } else if(client.engine.khtml) {
                    alert("  KHTML   ");
                } else if(client.engine.opera) {
                    alert("  opera  ");
                }
    
                //            
                if(client.engine.webkit) { //    webkit  
                    if(client.browser.chrome) {
                        alert("  google chrome")
                    } else if(client.browser.safari) {
                        alert("  Safari")
                    }
                } else if(client.engine.gecko) {
                    if(client.browser.firefox) {
                        alert("    ")
                    } else {
                        alert("gecko.....")
                    }
                } else if(client.browser.ie) {
                    alert("  IE")
                } else if(client.browser.opera) {
                    alert("  Opera")
                }
    
                //         
                if(client.system.win) {
                    alert("window   ")
                } else if(client.system.mac) {
                    alert("Mac   ")
                } else if(client.system.x11) {
                    alert("Unix(Linux)   ")
                }
                

    ネット上の検査方法:
    var browser = {
        v: (function() {
            var u = navigator.userAgent,
                app = navigator.appVersion,
                p = navigator.platform;
            return {
                trident: u.indexOf('Trident') > -1, //IE  
                presto: u.indexOf('Presto') > -1, //opera  
                webKit: u.indexOf('AppleWebKit') > -1, //  、    
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //    
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //       
                ios: !!u.match(/i[^;]+;( U;)? CPU.+Mac OS X/), //ios  
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android   uc   
                iPhone: u.indexOf('iPhone') > -1, //   iPhone  QQHD   
                iPad: u.indexOf('iPad') > -1, //  iPad
                weixin: u.indexOf('MicroMessenger') > -1, //    
                webApp: u.indexOf('Safari') == -1, //  web    ,       
                UCB: u.match(/UCBrowser/i) == "UCBrowser",
                QQB: u.match(/MQQBrowser/i) == "MQQBrowser",
                win: p.indexOf('Win') > -1, //     WIN    
                mac: p.indexOf('Mac') > -1 //     Mac    
            };
        })()
    }
            
    if("\v"=="v"){//true IE   ,           ,                 
        // ...
    }
  • 原文アドレス:https://segmentfault.com/a/11...
  • 好きなら、いいね$underline{か}$...
    新しいアイデアがあれば、直接下にコメントしたり、連絡してにコメントしたりすることができます.
    作者[@Qing]