From ac1f92d3c6c7c1be77227f86a5c5eb377020d12d Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Fri, 14 Sep 2012 02:42:11 +0700 Subject: [PATCH 1/3] Change license paragraph in readme from GPL2 to MIT --- package.json | 4 ++-- readme.md | 43 ++++++++++++++++++++++--------------------- ua-parser.js | 16 ++++++++-------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 5d5e293..f84efb9 100644 --- a/package.json +++ b/package.json @@ -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 (http://faisalman.com)", "description": "Lightweight JavaScript-based user-agent string parser", "keywords": [ diff --git a/readme.md b/readme.md index 0e70a5a..af75017 100644 --- a/readme.md +++ b/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 <> * 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 <> -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. \ No newline at end of file +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. diff --git a/ua-parser.js b/ua-parser.js index 2b39821..68062b6 100644 --- a/ua-parser.js +++ b/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,10 @@ // regexp mapper var regxMap = function (ua) { - var result = {}, i, j, k, l; + var result = {}, i, j, k, l, m; 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,..) for (k = 0; k < props.length; k++) { if (typeof props[k] == 'object') { result[props[k][0]] = undefined; @@ -24,17 +24,17 @@ } } for (j = 0; j < regex.length; j++) { - var match = regex[j].exec(ua); - if (!!match) { + m = regex[j].exec(ua); + if (!!m) { l = 1; for (k = 0; k < props.length; k++) { 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[k + l]) ? m[k + l].replace(props[k][1], props[k][2]) : undefined; } else { - result[props[k]] = (!!match[k + l]) ? match[k + l] : undefined; + result[props[k]] = (!!m[k + l]) ? m[k + l] : undefined; } } break; From 7cd76a3fba49fcea9b74a16e4f847a631ddf95d8 Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Fri, 14 Sep 2012 04:04:45 +0700 Subject: [PATCH 2/3] Fix Opera, Add match for replacement --- ua-parser.js | 57 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/ua-parser.js b/ua-parser.js index 68062b6..8937cfa 100644 --- a/ua-parser.js +++ b/ua-parser.js @@ -12,10 +12,16 @@ // regexp mapper var regxMap = function (ua) { + 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,..) + + // build object barebones for (k = 0; k < props.length; k++) { if (typeof props[k] == 'object') { result[props[k][0]] = undefined; @@ -23,30 +29,33 @@ result[props[k]] = undefined; } } + + // try matching uastring with regexes for (j = 0; j < regex.length; j++) { - m = regex[j].exec(ua); - if (!!m) { - 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]] = (!!m[k + l]) ? m[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]] = (!!m[k + l]) ? m[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){ + + check : function(str, map){ for (var i = 0; i < map.length; i++) { if (str.toLowerCase().indexOf(map[i][0]) !== -1) { return map[i][1]; @@ -54,8 +63,9 @@ } return str; }, + os : { - win: function (match, str1) { + win : function (match, str1) { var map = [ ['4.90', 'ME'], ['nt3.51', 'NT 3.11'], @@ -83,8 +93,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 +105,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 +160,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 +202,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']); }; From e41fa1c82931963eb674c797d9dca8c0d4770869 Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Fri, 14 Sep 2012 04:29:00 +0700 Subject: [PATCH 3/3] Modify mapper method --- ua-parser.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/ua-parser.js b/ua-parser.js index 8937cfa..d5145e8 100644 --- a/ua-parser.js +++ b/ua-parser.js @@ -56,9 +56,17 @@ 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]; + 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; @@ -66,18 +74,16 @@ 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); + 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' + }); } } };