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 the client.
我见过的大多数实现都是用于客户端的浏览器检测。我想知道是否可以在向客户端发送任何资源之前进行浏览器检测。
Thanks.
谢谢。
12 个解决方案
#1
73
var ua = request.headers['user-agent'],
$ = {};
if (/mobile/i.test(ua))
$.Mobile = true;
if (/like Mac OS X/.test(ua)) {
$.iOS = /CPU( iPhone)? OS ([0-9\._]+) like Mac OS X/.exec(ua)[2].replace(/_/g, '.');
$.iPhone = /iPhone/.test(ua);
$.iPad = /iPad/.test(ua);
}
if (/Android/.test(ua))
$.Android = /Android ([0-9\.]+)[\);]/.exec(ua)[1];
if (/webOS\//.test(ua))
$.webOS = /webOS\/([0-9\.]+)[\);]/.exec(ua)[1];
if (/(Intel|PPC) Mac OS X/.test(ua))
$.Mac = /(Intel|PPC) Mac OS X ?([0-9\._]*)[\)\;]/.exec(ua)[2].replace(/_/g, '.') || true;
if (/Windows NT/.test(ua))
$.Windows = /Windows NT ([0-9\._]+)[\);]/.exec(ua)[1];
That should work for you. Just put it in your response handler.
那应该对你有用。把它放到响应处理程序中。
#2
28
The ua-parser library for node (npm install ua-parser
) exposes a big set of regexes for browser user-agent strings. I'd strongly recommend it for your needs.
用于节点的ui解析器库(npm安装ui解析器)公开了一组用于浏览器用户代理字符串的regexe。我强烈推荐你的需要。
#3
12
I threw this together using ua-parser-js. I'm sure it can be improved but it's functional.
我使用ua-parser-js将它们结合在一起。我确信它可以改进,但它是功能性的。
Install the package:
安装包:
sudo npm install ua-parser-js
In your routes file require UAParser:
在您的路由文件要求UAParser:
var UAParser = require('ua-parser-js');
Do some stuff with it:
用它做点什么:
function ensureLatestBrowser(req, res, next) {
var parser = new UAParser();
var ua = req.headers['user-agent'];
var browserName = parser.setUA(ua).getBrowser().name;
var fullBrowserVersion = parser.setUA(ua).getBrowser().version;
var browserVersion = fullBrowserVersion.split(".",1).toString();
var browserVersionNumber = Number(browserVersion);
if (browserName == 'IE' && browserVersion <= 9)
res.redirect('/update/');
else if (browserName == 'Firefox' && browserVersion <= 24)
res.redirect('/update/');
else if (browserName == 'Chrome' && browserVersion <= 29)
res.redirect('/update/');
else if (browserName == 'Canary' && browserVersion <= 32)
res.redirect('/update/');
else if (browserName == 'Safari' && browserVersion <= 5)
res.redirect('/update/');
else if (browserName == 'Opera' && browserVersion <= 16)
res.redirect('/update/');
else
return next();
}
and then in your route just call:
然后在你的路线上,只要打个电话:
app.all(/^(?!(\/update)).*$/, ensureLatestBrowser);
If you want to see what other information you can get with UAParser check out their demo page.
如果你想看看你能从UAParser那里得到什么其他信息,请查看他们的演示页面。
#4
4
I wanted to do a simple redirection to a mobile version of my site, so user-agent is reliable enough. I wanted to do it server-side so I didn't waste time loading unnecessary css and js on the client. http://detectmobilebrowsers.com/ had the most robust regex to match. So I threw together some express middleware that will let you do the redirection by just adding two lines of code to your app.
我想对我的站点做一个简单的重定向,这样用户代理就足够可靠了。我想在服务器端做,这样就不会浪费时间在客户端加载不必要的css和js。http://detectmobilebrowsers.com/拥有最健壮的regex。所以我把一些express中间件放在一起,通过向应用程序添加两行代码就可以实现重定向。
npm install detectmobilebrowsers
to install
npm安装detectmobilebrowser来安装
express = require 'express'
mobile = require 'detectmobilebrowsers'
app = express()
app.configure () ->
app.use mobile.redirect 'http://m.domain.com'
app.get '/', (req, res) ->
res.send 'Not on Mobile'
app.listen 3000
#5
2
Here's another one: https://github.com/koudelka/node-useragent_parser
这是另一个:https://github.com/koudelka/node-useragent_parser
#6
2
if your using express you can easily check the ua with something like this:
如果你使用的是快递,你可以很容易地用这样的方式检查ua:
app.get('/ua', function(req, res){
res.send('user ' + req.headers['user-agent']);
});
#7
2
ua = request.headers['user-agent'];
if( /firefox/i.test(ua) )
browser = 'firefox';
else if( /chrome/i.test(ua) )
browser = 'chrome';
else if( /safari/i.test(ua) )
browser = 'safari';
else if( /msie/i.test(ua) )
browser = 'msie';
else
browser = 'unknown';
#8
1
Most browsers provide an HTTP request header called "User-Agent" This is the same as the navigator.userAgent property on the client side.
大多数浏览器都提供一个名为“User-Agent”的HTTP请求头,与导航器相同。客户端上的userAgent属性。
#10
0
If you want to control mobile in the templating layer, I just wrote a module for that. https://github.com/Fresheyeball/isMobile-node
如果您想在模板层中控制mobile,我只为此编写了一个模块。https://github.com/Fresheyeball/isMobile-node
#11
0
I improved a bit @duck5auce's code to be actually useful and support IE 10-12 (Edge).
我改进了位@duck5auce的代码,使之真正有用并支持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]);
};
#12
0
You might want to have a look at Apache DeviceMap.
您可能想了解一下Apache DeviceMap。
JavaScript libraries out of the box are more on the client side right now, but much will work on Node.JS or Angular in a similar way. Unlike simple pattern matching of UA strings DeviceMap comes with a vast range of devices and device families in its Device Description Repository (DDR) based on W3C standards.
现在,客户端的JavaScript库更多的是开箱即用的,但是在Node上会有很多工作要做。以类似的方式。不同于简单的UA strings DeviceMap的模式匹配,DeviceMap在基于W3C标准的设备描述库(DDR)中提供了大量的设备和设备族。
#1
73
var ua = request.headers['user-agent'],
$ = {};
if (/mobile/i.test(ua))
$.Mobile = true;
if (/like Mac OS X/.test(ua)) {
$.iOS = /CPU( iPhone)? OS ([0-9\._]+) like Mac OS X/.exec(ua)[2].replace(/_/g, '.');
$.iPhone = /iPhone/.test(ua);
$.iPad = /iPad/.test(ua);
}
if (/Android/.test(ua))
$.Android = /Android ([0-9\.]+)[\);]/.exec(ua)[1];
if (/webOS\//.test(ua))
$.webOS = /webOS\/([0-9\.]+)[\);]/.exec(ua)[1];
if (/(Intel|PPC) Mac OS X/.test(ua))
$.Mac = /(Intel|PPC) Mac OS X ?([0-9\._]*)[\)\;]/.exec(ua)[2].replace(/_/g, '.') || true;
if (/Windows NT/.test(ua))
$.Windows = /Windows NT ([0-9\._]+)[\);]/.exec(ua)[1];
That should work for you. Just put it in your response handler.
那应该对你有用。把它放到响应处理程序中。
#2
28
The ua-parser library for node (npm install ua-parser
) exposes a big set of regexes for browser user-agent strings. I'd strongly recommend it for your needs.
用于节点的ui解析器库(npm安装ui解析器)公开了一组用于浏览器用户代理字符串的regexe。我强烈推荐你的需要。
#3
12
I threw this together using ua-parser-js. I'm sure it can be improved but it's functional.
我使用ua-parser-js将它们结合在一起。我确信它可以改进,但它是功能性的。
Install the package:
安装包:
sudo npm install ua-parser-js
In your routes file require UAParser:
在您的路由文件要求UAParser:
var UAParser = require('ua-parser-js');
Do some stuff with it:
用它做点什么:
function ensureLatestBrowser(req, res, next) {
var parser = new UAParser();
var ua = req.headers['user-agent'];
var browserName = parser.setUA(ua).getBrowser().name;
var fullBrowserVersion = parser.setUA(ua).getBrowser().version;
var browserVersion = fullBrowserVersion.split(".",1).toString();
var browserVersionNumber = Number(browserVersion);
if (browserName == 'IE' && browserVersion <= 9)
res.redirect('/update/');
else if (browserName == 'Firefox' && browserVersion <= 24)
res.redirect('/update/');
else if (browserName == 'Chrome' && browserVersion <= 29)
res.redirect('/update/');
else if (browserName == 'Canary' && browserVersion <= 32)
res.redirect('/update/');
else if (browserName == 'Safari' && browserVersion <= 5)
res.redirect('/update/');
else if (browserName == 'Opera' && browserVersion <= 16)
res.redirect('/update/');
else
return next();
}
and then in your route just call:
然后在你的路线上,只要打个电话:
app.all(/^(?!(\/update)).*$/, ensureLatestBrowser);
If you want to see what other information you can get with UAParser check out their demo page.
如果你想看看你能从UAParser那里得到什么其他信息,请查看他们的演示页面。
#4
4
I wanted to do a simple redirection to a mobile version of my site, so user-agent is reliable enough. I wanted to do it server-side so I didn't waste time loading unnecessary css and js on the client. http://detectmobilebrowsers.com/ had the most robust regex to match. So I threw together some express middleware that will let you do the redirection by just adding two lines of code to your app.
我想对我的站点做一个简单的重定向,这样用户代理就足够可靠了。我想在服务器端做,这样就不会浪费时间在客户端加载不必要的css和js。http://detectmobilebrowsers.com/拥有最健壮的regex。所以我把一些express中间件放在一起,通过向应用程序添加两行代码就可以实现重定向。
npm install detectmobilebrowsers
to install
npm安装detectmobilebrowser来安装
express = require 'express'
mobile = require 'detectmobilebrowsers'
app = express()
app.configure () ->
app.use mobile.redirect 'http://m.domain.com'
app.get '/', (req, res) ->
res.send 'Not on Mobile'
app.listen 3000
#5
2
Here's another one: https://github.com/koudelka/node-useragent_parser
这是另一个:https://github.com/koudelka/node-useragent_parser
#6
2
if your using express you can easily check the ua with something like this:
如果你使用的是快递,你可以很容易地用这样的方式检查ua:
app.get('/ua', function(req, res){
res.send('user ' + req.headers['user-agent']);
});
#7
2
ua = request.headers['user-agent'];
if( /firefox/i.test(ua) )
browser = 'firefox';
else if( /chrome/i.test(ua) )
browser = 'chrome';
else if( /safari/i.test(ua) )
browser = 'safari';
else if( /msie/i.test(ua) )
browser = 'msie';
else
browser = 'unknown';
#8
1
Most browsers provide an HTTP request header called "User-Agent" This is the same as the navigator.userAgent property on the client side.
大多数浏览器都提供一个名为“User-Agent”的HTTP请求头,与导航器相同。客户端上的userAgent属性。
#9
#10
0
If you want to control mobile in the templating layer, I just wrote a module for that. https://github.com/Fresheyeball/isMobile-node
如果您想在模板层中控制mobile,我只为此编写了一个模块。https://github.com/Fresheyeball/isMobile-node
#11
0
I improved a bit @duck5auce's code to be actually useful and support IE 10-12 (Edge).
我改进了位@duck5auce的代码,使之真正有用并支持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]);
};
#12
0
You might want to have a look at Apache DeviceMap.
您可能想了解一下Apache DeviceMap。
JavaScript libraries out of the box are more on the client side right now, but much will work on Node.JS or Angular in a similar way. Unlike simple pattern matching of UA strings DeviceMap comes with a vast range of devices and device families in its Device Description Repository (DDR) based on W3C standards.
现在,客户端的JavaScript库更多的是开箱即用的,但是在Node上会有很多工作要做。以类似的方式。不同于简单的UA strings DeviceMap的模式匹配,DeviceMap在基于W3C标准的设备描述库(DDR)中提供了大量的设备和设备族。