server-side browser detection? node.js

前端 未结 15 1089
不知归路
不知归路 2020-12-07 09:29

Most implementations i\'ve seen are for browser detection on the client side. I was just wondering if it was possible to do browser detection before sending any resources to

15条回答
  •  自闭症患者
    2020-12-07 09:52

    I improved a bit @duck5auce's code to be actually useful and support IE 10-12 (Edge).

    var getDevice = function(ua) {
        var $ = {active: false, subactive: false};
    
        if (/mobile/i.test(ua)) {
            $.active = 'mobile';
            $.Mobile = true;
        }
    
        if (/like Mac OS X/.test(ua)) {
            $.active = 'iOS';
            $.iOS = /CPU( iPhone)? OS ([0-9\._]+) like Mac OS X/.exec(ua)[2].replace(/_/g, '.');
            if (/like Mac OS X/.test(ua)) {
                $.subactive = 'iPhone';
                $.iPhone = /iPhone/.test(ua);
            }
            if (/like Mac OS X/.test(ua)) {
                $.subactive = 'iPad';
                $.iPad = /iPad/.test(ua);
            }
        }
    
        if (/Android/.test(ua)) {
            $.active = 'Android';
            $.Android = /Android ([0-9\.]+)[\);]/.exec(ua)[1];
        }
    
        if (/webOS\//.test(ua)) {
            $.active = 'webOS';
            $.webOS = /webOS\/([0-9\.]+)[\);]/.exec(ua)[1];
        }
    
        if (/(Intel|PPC) Mac OS X/.test(ua)) {
            $.active = 'Safari';
            $.Safari = /(Intel|PPC) Mac OS X ?([0-9\._]*)[\)\;]/.exec(ua)[2].replace(/_/g, '.') || true;
        }
    
        if (/Windows NT/.test(ua)) {
            $.active = 'IE';
            $.IE = /Windows NT ([0-9\._]+)[\);]/.exec(ua)[1];
        }
        if (/MSIE/.test(ua)) {
            $.active = 'IE';
            $.IE = /MSIE ([0-9]+[\.0-9]*)/.exec(ua)[1];
        }
        if (/Trident/.test(ua)) {
            $.active = 'IE';
            $.IE = /Trident\/.*rv:([0-9]+[\.0-9]*)/.exec(ua)[1];
        }
        if (/Edge\/\d+/.test(ua)) {
            $.active = 'IE Edge';
            $.IE = /Edge\/(\d+)/.exec(ua)[1];
        }
    
        return $.active + ' ' + $[$.active] + ($.subactive && ' ' + $.subactive + ' ' + $[$.subactive]);
    };
    

提交回复
热议问题