mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-09-28 00:18:45 +03:00
Merge branch 'master' into gh-pages
This commit is contained in:
commit
fd2ff2cce4
@ -1,7 +1,7 @@
|
||||
{
|
||||
"title": "UA-Parser.js",
|
||||
"title": "UA-Parser.JS",
|
||||
"name": "ua-parser-js",
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.3",
|
||||
"author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)",
|
||||
"description": "Lightweight JavaScript-based user-agent string parser",
|
||||
"keywords": [
|
||||
|
43
readme.md
43
readme.md
@ -1,6 +1,6 @@
|
||||
# UA-Parser.js
|
||||
# UA-Parser.JS
|
||||
|
||||
Light-weight JavaScript-based all-in-one user-agent parser
|
||||
Lightweight JavaScript-based User-Agent string parser
|
||||
|
||||
* Author : Faisalman <<fyzlman@gmail.com>>
|
||||
* Home : http://faisalman.github.com/ua-parser-js
|
||||
@ -9,14 +9,15 @@ Light-weight JavaScript-based all-in-one user-agent parser
|
||||
|
||||
## Features
|
||||
|
||||
Get detailed type and version of web browser, layout engine, operating system, and device.
|
||||
Extract detailed type of web browser, layout engine, operating system, and device purely from user-agent string.
|
||||
|
||||
## Methods
|
||||
|
||||
* `getBrowser([uastring])`
|
||||
* `getDevice([uastring])`
|
||||
* `getEngine([uastring])`
|
||||
* `getOS([uastring])`
|
||||
* `getBrowser()`
|
||||
* `getDevice()`
|
||||
* `getEngine()`
|
||||
* `getOS()`
|
||||
* `getUA()`
|
||||
* `setUA(uastring)`
|
||||
|
||||
## Properties
|
||||
@ -31,7 +32,7 @@ Get detailed type and version of web browser, layout engine, operating system, a
|
||||
|
||||
var parser = new UAParser();
|
||||
|
||||
// by default it takes ua string from current browser's window.navigator
|
||||
// by default it takes ua string from current browser's window.navigator.userAgent
|
||||
console.log(parser.result);
|
||||
/*
|
||||
/// this will print an object structured like this:
|
||||
@ -77,23 +78,23 @@ 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+';
|
||||
|
||||
parser.setUA(ua1).getDevice().name; // "PlayBook"
|
||||
parser.getOS() // {name: "RIM Tablet OS", version: "1.0.0"}
|
||||
parser.getOS(ua2) // {name: "Linux", version: undefined}
|
||||
parser.getOS() // {name: "RIM Tablet OS", version: "1.0.0"}
|
||||
parser.getEngine().name; // "AppleWebKit"
|
||||
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.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
|
||||
console.log(parser.getEngine().name); // "AppleWebKit"
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Copyright © 2012 Faisalman <<fyzlman@gmail.com>>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
101
ua-parser.js
101
ua-parser.js
@ -1,4 +1,4 @@
|
||||
// UA-Parser.js v0.3.2
|
||||
// UA-Parser.JS v0.3.3
|
||||
// Lightweight JavaScript-based User-Agent string parser
|
||||
// https://github.com/faisalman/ua-parser-js
|
||||
//
|
||||
@ -12,10 +12,16 @@
|
||||
|
||||
// regexp mapper
|
||||
var regxMap = function (ua) {
|
||||
var result = {}, i, j, k, l;
|
||||
|
||||
var result = {}, i, j, k, l, m;
|
||||
|
||||
// loop through all regexes maps
|
||||
for (i = 1; i < arguments.length; i += 2) {
|
||||
var regex = arguments[i], // odd sequence (2,4,6,..)
|
||||
props = arguments[i + 1]; // even sequence (3,5,7,..)
|
||||
|
||||
var regex = arguments[i], // odd sequence (2,4,6,..)
|
||||
props = arguments[i + 1]; // even sequence (3,5,7,..)
|
||||
|
||||
// build object barebones
|
||||
for (k = 0; k < props.length; k++) {
|
||||
if (typeof props[k] == 'object') {
|
||||
result[props[k][0]] = undefined;
|
||||
@ -23,51 +29,61 @@
|
||||
result[props[k]] = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// try matching uastring with regexes
|
||||
for (j = 0; j < regex.length; j++) {
|
||||
var match = regex[j].exec(ua);
|
||||
if (!!match) {
|
||||
l = 1;
|
||||
l = regex[j].exec(ua);
|
||||
if (!!l) {
|
||||
for (k = 0; k < props.length; k++) {
|
||||
m = l[k + 1];
|
||||
if (typeof props[k] === 'object' && props[k].length === 2) {
|
||||
result[props[k][0]] = props[k][1];
|
||||
l -= 1;
|
||||
} else if (typeof props[k] === 'object' && props[k].length === 3) {
|
||||
result[props[k][0]] = (!!match[k + l]) ? match[k + l].replace(props[k][1], props[k][2]) : undefined;
|
||||
result[props[k][0]] = m ? m.replace(props[k][1], props[k][2]) : undefined;
|
||||
} else {
|
||||
result[props[k]] = (!!match[k + l]) ? match[k + l] : undefined;
|
||||
result[props[k]] = m ? m : undefined;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!!l) break; // break the loop if match found
|
||||
|
||||
if(!!l) break; // break the loop immediately if match found
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
var mapper = {
|
||||
check: function(str, map){
|
||||
for (var i = 0; i < map.length; i++) {
|
||||
if (str.toLowerCase().indexOf(map[i][0]) !== -1) {
|
||||
return map[i][1];
|
||||
|
||||
check : function(str, 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++) {
|
||||
if (str.toLowerCase().indexOf(map[i][j]) !== -1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
} else if (str.toLowerCase().indexOf(map[i]) !== -1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
},
|
||||
|
||||
os : {
|
||||
win: function (match, str1) {
|
||||
var map = [
|
||||
['4.90', 'ME'],
|
||||
['nt3.51', 'NT 3.11'],
|
||||
['nt4.0', 'NT 4.0'],
|
||||
['nt 5.0', '2000'],
|
||||
['nt 5.1', 'XP'],
|
||||
['nt 5.2', 'XP'],
|
||||
['nt 6.0', 'Vista'],
|
||||
['nt 6.1', '7'],
|
||||
['nt 6.2', '8'],
|
||||
];
|
||||
return mapper.check(str1, map);
|
||||
win : function (match, str1) {
|
||||
return mapper.check(str1, {
|
||||
'ME' : '4.90',
|
||||
'NT 3.11' : 'nt3.51',
|
||||
'NT 4.0' : 'nt4.0',
|
||||
'2000' : 'nt 5.0',
|
||||
'XP' : ['nt 5.1', 'nt 5.2'],
|
||||
'Vista' : 'nt 6.0',
|
||||
'7' : 'nt 6.1',
|
||||
'8' : 'nt 6.2'
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -83,8 +99,9 @@
|
||||
// Presto based
|
||||
/(opera\smini)\/((\d+)?[\w\.-]+)/i, // Opera Mini
|
||||
/(opera\smobi)\/((\d+)?[\w\.-]+)/i, // Opera Mobile
|
||||
/(opera).*\/((\d+)?[\w\.]+)/i, // Opera
|
||||
|
||||
/(opera).+version\/((\d+)?[\w\.]+)/i, // Opera
|
||||
/(opera)[\/\s]+((\d+)?[\w\.]+)/i,
|
||||
|
||||
// Trident based
|
||||
/(avant\sbrowser|iemobile|slimbrowser)[\/\s]?((\d+)?[\w\.]*)/i, // Avant/IEMobile/SlimBrowser
|
||||
/ms(ie)\s((\d+)?[\w\.]+)/i, // Internet Explorer
|
||||
@ -94,7 +111,7 @@
|
||||
/(chrome|omniweb|arora|dolfin|[tizenaok]{5}\s?browser)\/((\d+)?[\w\.]+)/i,
|
||||
// Chrome/OmniWeb/Arora/Dolphin/Tizen/Nokia
|
||||
], ['name', 'version', 'major'], [
|
||||
/(?:android.+crmo|crios)\/((\d+)?[\w\.]+)/i, // Chrome for Android/iOS
|
||||
/(?:android.+(crmo|crios))\/((\d+)?[\w\.]+)/i, // Chrome for Android/iOS
|
||||
], [['name', 'Chrome'], 'version', 'major'], [
|
||||
/(mobile\ssafari|safari|konqueror)\/((\d+)?[\w\.]+)/i, // Safari/Konqueror
|
||||
/(applewebkit|khtml)\/((\d+)?[\w\.]+)/i,
|
||||
@ -149,11 +166,11 @@
|
||||
/(gnu|linux)\s?([\w\.]+)*/i // Other GNU/Linux
|
||||
], ['name', 'version'], [
|
||||
|
||||
/cros\s([\w\.\s]+\d)/i // Chromium OS
|
||||
/(cros)\s([\w\.\s]+\d)/i // Chromium OS
|
||||
], [['name', 'Chromium OS'], 'version'],[
|
||||
|
||||
// Solaris
|
||||
/sunos\s?([\w\.\s]+\d)*/i // Solaris
|
||||
/(sunos)\s?([\w\.\s]+\d)*/i // Solaris
|
||||
], [['name', 'Solaris'], 'version'], [
|
||||
|
||||
// BSD based
|
||||
@ -191,23 +208,23 @@
|
||||
/(zte)-([\w]+)*/i
|
||||
], ['name', ['version', /_/g, ' ']], [
|
||||
|
||||
/\s(milestone|mz601|droid[2x]?|xoom)[globa\s]*\sbuild\//i, // Motorola
|
||||
/mot[\s-]?(\w+)*/i
|
||||
/\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
|
||||
/((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
|
||||
/((transfo[prime\s]{4,10}\s\w+|(?:android.*)eeepc))/i // Asus
|
||||
], [['name', 'Asus'], 'version'], [
|
||||
|
||||
/sie-(\w+)*/i // Siemens
|
||||
/(sie)-(\w+)*/i // Siemens
|
||||
], [['name', 'Siemens'], 'version'], [
|
||||
|
||||
/(?=maemo|nokia).*(n900|lumia\s\d+)/i, // Nokia
|
||||
/nokia[\s_-]?([\w-]+)*/i
|
||||
/(maemo|nokia).*(n900|lumia\s\d+)/i, // Nokia
|
||||
/(nokia)[\s_-]?([\w-]+)*/i
|
||||
], [['name', 'Nokia'], 'version']);
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user