mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-11-20 17:16:40 +03:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68d124c59c | ||
|
|
33e6fe42d1 | ||
|
|
51822ad172 | ||
|
|
ec40433c5c |
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"title": "UA-Parser.JS",
|
||||
"name": "ua-parser-js",
|
||||
"version": "0.3.4",
|
||||
"version": "0.4.0",
|
||||
"author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)",
|
||||
"description": "Lightweight JavaScript-based user-agent string parser",
|
||||
"keywords": [
|
||||
|
||||
@@ -72,12 +72,9 @@ Extract detailed type of web browser, layout engine, operating system, and devic
|
||||
```js
|
||||
var UAParser = require('ua-parser');
|
||||
var parser = new UAParser();
|
||||
var ua1 = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11';
|
||||
var ua2 = 'Midori/0.2 (X11; Linux; U; cs-cz) WebKit/531.2+';
|
||||
var uaString = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11';
|
||||
|
||||
console.log(parser.setUA(ua1).getDevice().name); // "PlayBook"
|
||||
console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
|
||||
console.log(parser.getOS(ua2)) // {name: "Linux", version: undefined}
|
||||
console.log(parser.setUA(uaString).getDevice().name); // "PlayBook"
|
||||
console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
|
||||
console.log(parser.getEngine().name); // "AppleWebKit"
|
||||
```
|
||||
|
||||
166
ua-parser.js
166
ua-parser.js
@@ -1,4 +1,4 @@
|
||||
// UA-Parser.JS v0.3.4
|
||||
// UA-Parser.JS v0.4.0
|
||||
// Lightweight JavaScript-based User-Agent string parser
|
||||
// https://github.com/faisalman/ua-parser-js
|
||||
//
|
||||
@@ -7,28 +7,36 @@
|
||||
|
||||
(function (undefined) {
|
||||
|
||||
var regexMapper = function (ua, args) {
|
||||
var mapper = {
|
||||
|
||||
var result = {}, i, j, k, l, m;
|
||||
regex : function () {
|
||||
|
||||
var result, i, j, k, l, m, args = arguments;
|
||||
|
||||
// loop through all regexes maps
|
||||
for (i = 1; i < arguments.length; i += 2) {
|
||||
for (i = 0; i < args.length; i += 2) {
|
||||
|
||||
var regex = arguments[i], // even sequence (1,3,5,..)
|
||||
props = arguments[i + 1]; // odd sequence (2,4,6,..)
|
||||
var regex = args[i], // odd sequence (0,2,4,..)
|
||||
props = args[i + 1]; // even sequence (1,3,5,..)
|
||||
|
||||
// build object barebones
|
||||
// construct object barebones
|
||||
if (typeof result === 'undefined') {
|
||||
result = {};
|
||||
for (k = 0; k < props.length; k++) {
|
||||
if (typeof props[k] == 'object') {
|
||||
if (typeof props[k] === 'object') {
|
||||
result[props[k][0]] = undefined;
|
||||
} else {
|
||||
result[props[k]] = undefined;
|
||||
}
|
||||
}
|
||||
if (this.getUA().toString() === '') {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// try matching uastring with regexes
|
||||
for (j = 0; j < regex.length; j++) {
|
||||
l = regex[j].exec(ua);
|
||||
l = regex[j].exec(this.getUA());
|
||||
if (!!l) {
|
||||
for (k = 0; k < props.length; k++) {
|
||||
m = l[k + 1];
|
||||
@@ -47,12 +55,11 @@
|
||||
if(!!l) break; // break the loop immediately if match found
|
||||
}
|
||||
return result;
|
||||
};
|
||||
},
|
||||
|
||||
var maps = {
|
||||
string : function (str, map) {
|
||||
|
||||
check : function(str, map){
|
||||
for (var i in map){
|
||||
for (var i in map) {
|
||||
if (map.hasOwnProperty(i)) {
|
||||
if (typeof map[i] === 'object' && map[i].length > 0) {
|
||||
for (var j = 0; j < map[i].length; j++) {
|
||||
@@ -66,11 +73,14 @@
|
||||
}
|
||||
}
|
||||
return str;
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
var maps = {
|
||||
os : {
|
||||
win : function (match, str1) {
|
||||
return maps.check(str1, {
|
||||
windows : {
|
||||
version : function (match, str1) {
|
||||
return mapper.string(str1, {
|
||||
'ME' : '4.90',
|
||||
'NT 3.11' : 'nt3.51',
|
||||
'NT 4.0' : 'nt4.0',
|
||||
@@ -82,15 +92,12 @@
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var UAParser = function UAParser (uastring) {
|
||||
var regexes = {
|
||||
|
||||
var ua = uastring || (typeof window !== 'undefined' ? window.navigator.userAgent : "");
|
||||
|
||||
this.getBrowser = function () {
|
||||
|
||||
return regexMapper(ua, [
|
||||
browser : [[
|
||||
|
||||
// Mixed
|
||||
/(kindle)\/((\d+)?[\w\.]+)/i, // Kindle
|
||||
@@ -125,12 +132,47 @@
|
||||
|
||||
// Other
|
||||
/(lynx|dillo|icab)[\/\s]?((\d+)?[\w\.]+)/i, // Lynx/Dillo/iCab
|
||||
], ['name', 'version', 'major']);
|
||||
};
|
||||
], ['name', 'version', 'major']
|
||||
],
|
||||
|
||||
this.getEngine = function () {
|
||||
device : [[
|
||||
|
||||
return regexMapper(ua, [
|
||||
/\((ip[honead]+|playbook);/i, // iPod/iPhone/iPad/PlayBook
|
||||
/(blackberry)[\s-]?(\w+)/i, // BlackBerry
|
||||
/(blackberry|benq|palm(?=\-)|sonyericsson|acer|asus|dell|nexus|zte)[\s_-]?([\w-]+)*/i,
|
||||
// BenQ/Palm/Sony-Ericsson/Acer/Asus/Dell/Nexus/ZTE
|
||||
/(hp)\s([\w\s]+\w)/i, // HP iPAQ
|
||||
/(hp).+(touchpad)/i, // HP TouchPad
|
||||
/(kindle)\/([\w\.]+)/i, // Kindle
|
||||
/(lg)[e;\s-]+(\w+)*/i, // LG
|
||||
/(nintendo|playstation)\s([wids3portable]+)/i // Nintendo/Playstation
|
||||
], ['name', 'version'], [
|
||||
|
||||
/(htc)[;_\s-]+([\w\s]+(?=\))|[\w]+)*/i, // HTC
|
||||
/(zte)-([\w]+)*/i
|
||||
], ['name', ['version', /_/g, ' ']], [
|
||||
|
||||
/\s((milestone|mz601|droid[2x]?|xoom))[globa\s]*\sbuild\//i, // Motorola
|
||||
/(mot)[\s-]?(\w+)*/i
|
||||
], [['name', 'Motorola'], 'version'], [
|
||||
|
||||
/((s[cgp]h-\w+|gt-\w+|galaxy\snexus))/i, // Samsung
|
||||
/(sam[sung]*)[\s-]*(\w+-?[\w-]*)*/i,
|
||||
/sec-((sgh\w+))/i
|
||||
], [['name', 'Samsung'], 'version'], [
|
||||
|
||||
/((transfo[prime\s]{4,10}\s\w+))|(?:android.*)((eeepc))/i // Asus
|
||||
], [['name', 'Asus'], 'version'], [
|
||||
|
||||
/(sie)-(\w+)*/i // Siemens
|
||||
], [['name', 'Siemens'], 'version'], [
|
||||
|
||||
/(maemo|nokia).*(n900|lumia\s\d+)/i, // Nokia
|
||||
/(nokia)[\s_-]?([\w-]+)*/i
|
||||
], [['name', 'Nokia'], 'version']
|
||||
],
|
||||
|
||||
engine : [[
|
||||
|
||||
/(presto)\/([\w\.]+)/i, // Presto
|
||||
/([aple]*webkit|trident)\/([\w\.]+)/i, // Webkit/Trident
|
||||
@@ -138,18 +180,16 @@
|
||||
], ['name', 'version'], [
|
||||
|
||||
/rv\:([\w\.]+).*(gecko)/i // Gecko
|
||||
], ['version', 'name']);
|
||||
};
|
||||
], ['version', 'name']
|
||||
],
|
||||
|
||||
this.getOS = function () {
|
||||
|
||||
return regexMapper(ua, [
|
||||
os : [[
|
||||
|
||||
// Windows based
|
||||
/(windows\sphone\sos|windows)\s?([nt\d\.\s]+\d)/i // Windows
|
||||
], ['name', ['version', /(.+)/gi, maps.os.win]], [
|
||||
], ['name', ['version', /(.+)/gi, maps.os.windows.version]], [
|
||||
/(win(?=3|9|n)|win\s9x\s)([nt\d\.]+)/i
|
||||
], [['name', 'Windows'], ['version', /(.+)/gi, maps.os.win]], [
|
||||
], [['name', 'Windows'], ['version', /(.+)/gi, maps.os.windows.version]], [
|
||||
|
||||
// Mobile/Embedded OS
|
||||
/(blackberry).+version\/([\w\.]+)/i, // Blackberry
|
||||
@@ -186,50 +226,28 @@
|
||||
// Other
|
||||
/(haiku)\s(\w+)/i, // Haiku
|
||||
/(macintosh|unix|minix|beos)[\/\s]?()*/i // UNIX/Minix/BeOS
|
||||
], ['name', 'version']);
|
||||
], ['name', 'version']
|
||||
]
|
||||
};
|
||||
|
||||
var UAParser = function UAParser (uastring) {
|
||||
|
||||
var ua = uastring || (typeof window !== 'undefined' ? window.navigator.userAgent : "");
|
||||
|
||||
this.getBrowser = function () {
|
||||
return mapper.regex.apply(this, regexes.browser);
|
||||
};
|
||||
|
||||
this.getDevice = function () {
|
||||
|
||||
return regexMapper(ua, [
|
||||
|
||||
/\((ip[honead]+|playbook);/i, // iPod/iPhone/iPad/PlayBook
|
||||
/(blackberry)[\s-]?(\w+)/i, // BlackBerry
|
||||
/(blackberry|benq|palm(?=\-)|sonyericsson|acer|asus|dell|nexus|zte)[\s_-]?([\w-]+)*/i,
|
||||
// BenQ/Palm/Sony-Ericsson/Acer/Asus/Dell/Nexus/ZTE
|
||||
/(hp)\s([\w\s]+\w)/i, // HP iPAQ
|
||||
/(hp).+(touchpad)/i, // HP TouchPad
|
||||
/(kindle)\/([\w\.]+)/i, // Kindle
|
||||
/(lg)[e;\s-]+(\w+)*/i, // LG
|
||||
/(nintendo|playstation)\s([wids3portable]+)/i // Nintendo/Playstation
|
||||
], ['name', 'version'], [
|
||||
|
||||
/(htc)[;_\s-]+([\w\s]+(?=\))|[\w]+)*/i, // HTC
|
||||
/(zte)-([\w]+)*/i
|
||||
], ['name', ['version', /_/g, ' ']], [
|
||||
|
||||
/\s((milestone|mz601|droid[2x]?|xoom))[globa\s]*\sbuild\//i, // Motorola
|
||||
/(mot)[\s-]?(\w+)*/i
|
||||
], [['name', 'Motorola'], 'version'], [
|
||||
|
||||
/((s[cgp]h-\w+|gt-\w+|galaxy\snexus))/i, // Samsung
|
||||
/(sam[sung]*)[\s-]*(\w+-?[\w-]*)*/i,
|
||||
/sec-((sgh\w+))/i
|
||||
], [['name', 'Samsung'], 'version'], [
|
||||
|
||||
/((transfo[prime\s]{4,10}\s\w+|(?:android.*)eeepc))/i // Asus
|
||||
], [['name', 'Asus'], 'version'], [
|
||||
|
||||
/(sie)-(\w+)*/i // Siemens
|
||||
], [['name', 'Siemens'], 'version'], [
|
||||
|
||||
/(maemo|nokia).*(n900|lumia\s\d+)/i, // Nokia
|
||||
/(nokia)[\s_-]?([\w-]+)*/i
|
||||
], [['name', 'Nokia'], 'version']);
|
||||
return mapper.regex.apply(this, regexes.device);
|
||||
};
|
||||
|
||||
this.getUA = function() {
|
||||
return ua;
|
||||
this.getEngine = function () {
|
||||
return mapper.regex.apply(this, regexes.engine);
|
||||
};
|
||||
|
||||
this.getOS = function () {
|
||||
return mapper.regex.apply(this, regexes.os);
|
||||
};
|
||||
|
||||
this.getResult = function() {
|
||||
@@ -241,6 +259,10 @@
|
||||
};
|
||||
};
|
||||
|
||||
this.getUA = function() {
|
||||
return ua;
|
||||
};
|
||||
|
||||
this.setUA = function (uastring) {
|
||||
ua = uastring;
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user