Compare commits

...

20 Commits

Author SHA1 Message Date
Faisal Salman
80d6d137ba Increment build version 2013-07-04 17:49:34 +07:00
Faisal Salman
19b5ddd038 Fix rv token IE11 2013-07-04 17:44:05 +07:00
Faisal Salman
9133d73d50 Merge branch 'ie11' of github.com:faisalman/ua-parser-js 2013-07-04 17:40:44 +07:00
Faisal Salman
3699c70a3a Fix unicode char 2013-07-04 17:39:48 +07:00
Faisal Salman
e6d276032b Motorola droid test case 2013-07-04 16:46:42 +07:00
Faisal Salman
b68caafd94 New test for IE11 2013-07-04 16:37:05 +07:00
Faisal Salman
eed632e3dd Detect IE11 without MSIE token 2013-07-04 15:53:46 +07:00
Faisal Salman
c5ce73f7de Add browsers: Iron, IceDragon, QQBrowser 2013-06-24 19:43:07 +07:00
Faisal Salman
2c9c721a5c Motorola Droid lineup 2013-06-13 13:57:24 +07:00
Faisal Salman
ec50c619ea Merge pull request #17 from silkapp/master
Fix problem with html5shiv and old IE
2013-05-19 22:01:37 -07:00
Erik Hesselink
07a7956eda Change 'for..in' loop to for loop.
The 'for..in' loop caused problems when the array was extended with
more methods (like map), e.g. by html5shiv in old IEs.
2013-05-17 14:05:21 +02:00
Faisal Salman
be0eec6716 Include ua in result object 2013-04-04 14:50:46 +07:00
Faisal Salman
8d439d26b8 Fix PocketPC mistakenly identified as PowerPC 2013-04-04 14:35:02 +07:00
Faisal Salman
dc20c12b63 Add CPU test & increment minor version 2013-04-04 13:46:09 +07:00
Faisal Salman
ecf8f7a849 Merge branch 'dev-cpu' of github.com:faisalman/ua-parser-js 2013-04-04 13:10:59 +07:00
Faisal Salman
3bf60cc830 Merge branch 'master' of github.com:faisalman/ua-parser-js 2013-04-04 13:05:46 +07:00
Faisal Salman
8c94018351 Update readme.md modify node example 2013-04-04 13:05:21 +07:00
Faisal Salman
9098ad501f Update readme.md regarding CPU detection 2013-04-03 21:03:28 +07:00
Faisal Salman
376541a383 Add new getCPU() method to detect CPU architecture 2013-04-03 18:00:27 +07:00
Faisal Salman
9cad6a0f2d Detect new IE 11 user-agent 2013-03-26 13:35:10 +07:00
10 changed files with 228 additions and 48 deletions

View File

@@ -1,8 +1,8 @@
{
"name": "ua-parser-js",
"version": "0.5.26",
"version": "0.6.2",
"description": "Lightweight JavaScript-based user-agent string parser",
"keywords": ["user-agent", "parser", "browser", "engine", "os", "device"],
"keywords": ["user-agent", "parser", "browser", "engine", "os", "device", "cpu"],
"scripts": ["src/ua-parser.js"],
"main": "src/ua-parser.js",
"license": "MIT",

View File

@@ -1,7 +1,7 @@
{
"title": "UAParser.js",
"name": "ua-parser-js",
"version": "0.5.26",
"version": "0.6.2",
"author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)",
"description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [
@@ -10,7 +10,8 @@
"browser",
"engine",
"os",
"device"
"device",
"cpu"
],
"homepage": "http://github.com/faisalman/ua-parser-js",
"contributors": [

View File

@@ -10,7 +10,7 @@ Lightweight JavaScript-based User-Agent string parser. Supports browser & node.j
## Features
Extract detailed type of web browser, layout engine, operating system, and device purely from user-agent string with relatively lightweight footprint (~7KB minified / ~3KB gzipped). Written in vanilla js, which means it doesn't depends on any other library.
Extract detailed type of web browser, layout engine, operating system, cpu architecture, and device purely from user-agent string with relatively lightweight footprint (~7KB minified / ~3KB gzipped). Written in vanilla js, which means it doesn't depends on any other library.
![It's over 9000](https://pbs.twimg.com/media/A9LpEG6CIAA5VrT.jpg)
@@ -23,11 +23,12 @@ Extract detailed type of web browser, layout engine, operating system, and devic
# Possible 'browser.name':
Amaya, Arora, Avant, Baidu, Blazer, Bolt, Camino, Chimera, Chrome, Chromium,
Comodo Dragon, Conkeror, Dillo, Dolphin, Doris, Epiphany, Fennec, Firebird,
Firefox, Flock, GoBrowser, iCab, ICE Browser, IceApe, IceCat, Iceweasel,
IE [Mobile], Jasmine, K-Meleon, Konqueror, Kindle, Links, Lunascape, Lynx, Maemo,
Maxthon, Midori, Minimo, [Mobile] Safari, Mosaic, Mozilla, Netfront, Netscape,
NetSurf, Nokia, OmniWeb, Opera [Mini/Mobi/Tablet], Phoenix, Polaris, RockMelt,
Silk, Skyfire, SeaMonkey, SlimBrowser, Swiftfox, Tizen, UCBrowser, w3m, Yandex
Firefox, Flock, GoBrowser, iCab, ICE Browser, IceApe, IceCat, IceDragon,
Iceweasel, IE [Mobile], Iron, Jasmine, K-Meleon, Konqueror, Kindle, Links,
Lunascape, Lynx, Maemo, Maxthon, Midori, Minimo, [Mobile] Safari, Mosaic, Mozilla,
Netfront, Netscape, NetSurf, Nokia, OmniWeb, Opera [Mini/Mobi/Tablet], Phoenix,
Polaris, QQBrowser, RockMelt, Silk, Skyfire, SeaMonkey, SlimBrowser, Swiftfox,
Tizen, UCBrowser, w3m, Yandex
# 'browser.version' & 'browser.major' determined dynamically
```
@@ -73,8 +74,16 @@ Windows [Phone/Mobile], Zenwalk
# 'os.version' determined dynamically
```
* `getCPU()`
* returns `{ architecture: '' }`
```
# Possible 'cpu.architecture'
68k, amd64, arm, ia32, ia64, irix, irix64, mips, mips64, pa-risc, ppc, sparc, sparc64
```
* `getResult()`
* returns `{ browser: {}, device: {}, engine: {}, os: {} }`
* returns `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
* `getUA()`
* returns UA string of current instance
* `setUA(uastring)`
@@ -96,6 +105,7 @@ Windows [Phone/Mobile], Zenwalk
/*
/// this will print an object structured like this:
{
ua: "",
browser: {
name: "",
version: "",
@@ -113,6 +123,9 @@ Windows [Phone/Mobile], Zenwalk
model: "",
type: "",
vendor: ""
},
cpu: {
architecture: ""
}
}
*/
@@ -130,6 +143,7 @@ Windows [Phone/Mobile], Zenwalk
console.log(result.os); // {name: "Ubuntu", version: "11.10"}
console.log(result.os.version); // "11.10"
console.log(result.engine.name); // "WebKit"
console.log(result.cpu.architecture); // "amd64"
// do some other tests
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
@@ -149,15 +163,6 @@ Windows [Phone/Mobile], Zenwalk
</html>
```
### Using requirejs
```js
require(['ua-parser'], function(UAParser) {
var parser = new UAParser();
console.log(parser.getResult());
});
```
### Using node.js
```sh
@@ -167,7 +172,17 @@ $ npm install ua-parser-js
```js
var UAParser = require('ua-parser-js');
var parser = new UAParser();
console.log(parser.getResult());
var ua = request.headers['user-agent']; // user-agent header from an HTTP request
console.log(parser.setUA(ua).getResult());
```
### Using requirejs
```js
require(['ua-parser'], function(UAParser) {
var parser = new UAParser();
console.log(parser.getResult());
});
```
### Using component
@@ -190,7 +205,7 @@ $ bower install ua-parser-js
### Using jQuery.ua
Although written in vanilla js (which means it doesn't depends on jQuery), if you're using jQuery, this library will automatically detect and create `$.ua` object based on browser's user-agent (although in case you need, `window.UAParser` constructor is still present). To get/set user-agent you can use: `$.ua.get()` / `$.ua.set(uastring)`.
Although written in vanilla js (which means it doesn't depends on jQuery), this library will automatically detect if jQuery is present and create `$.ua` object based on browser's user-agent (although in case you need, `window.UAParser` constructor is still present). To get/set user-agent you can use: `$.ua.get()` / `$.ua.set(uastring)`.
```js
// In browser with default user-agent: 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0':

View File

@@ -1,4 +1,4 @@
// UAParser.js v0.5.26
// UAParser.js v0.6.2
// Lightweight JavaScript-based User-Agent string parser
// https://github.com/faisalman/ua-parser-js
//
@@ -25,6 +25,7 @@
TYPE = 'type',
VENDOR = 'vendor',
VERSION = 'version',
ARCHITECTURE= 'architecture',
CONSOLE = 'console',
MOBILE = 'mobile',
TABLET = 'tablet';
@@ -38,6 +39,9 @@
var util = {
has : function (str1, str2) {
return str2.toLowerCase().indexOf(str1.toLowerCase()) !== -1;
},
lowerize : function (str) {
return str.toLowerCase();
}
};
@@ -80,8 +84,13 @@
// check if given property is actually array
if (typeof(q) === OBJ_TYPE && q.length > 0) {
if (q.length == 2) {
if (typeof(q[1]) == FUNC_TYPE) {
// assign modified match
result[q[0]] = q[1].call(this, match);
} else {
// assign given value, ignore regex match
result[q[0]] = q[1];
}
} else if (q.length == 3) {
// check whether function or regex
if (typeof(q[1]) === FUNC_TYPE && !(q[1].exec && q[1].test)) {
@@ -91,6 +100,8 @@
// sanitize match using given regex
result[q[0]] = match ? match.replace(q[1], q[2]) : undefined;
}
} else if (q.length == 4) {
result[q[0]] = match ? q[3].call(this, match.replace(q[1], q[2])) : undefined;
}
} else {
result[q] = match ? match : undefined;
@@ -110,7 +121,7 @@
for (var i in map) {
// check if array
if (typeof(map[i]) === OBJ_TYPE && map[i].length > 0) {
for (var j in map[i]) {
for (var j = 0; j < map[i].length; j++) {
if (util.has(map[i][j], str)) {
return (i === UNKNOWN) ? undefined : i;
}
@@ -209,14 +220,17 @@
// Trident based
/(avant\s|iemobile|slim|baidu)(?:browser)?[\/\s]?((\d+)?[\w\.]*)/i,
// Avant/IEMobile/SlimBrowser/Baidu
/ms(ie)\s((\d+)?[\w\.]+)/i, // Internet Explorer
/(?:ms|\()(ie)\s((\d+)?[\w\.]+)/i, // Internet Explorer
// Webkit/KHTML based
/(rekonq)((?:\/)[\w\.]+)*/i, // Rekonq
/(chromium|flock|rockmelt|midori|epiphany|silk|skyfire|ovibrowser|bolt)\/((\d+)?[\w\.-]+)/i
// Chromium/Flock/RockMelt/Midori/Epiphany/Silk/Skyfire/Bolt
/(chromium|flock|rockmelt|midori|epiphany|silk|skyfire|ovibrowser|bolt|iron)\/((\d+)?[\w\.-]+)/i
// Chromium/Flock/RockMelt/Midori/Epiphany/Silk/Skyfire/Bolt/Iron
], [NAME, VERSION, MAJOR], [
/(trident).+rv[:\s]((\d+)?[\w\.]+).+like\sgecko/i // IE11
], [[NAME, 'IE'], VERSION, MAJOR], [
/(yabrowser)\/((\d+)?[\w\.]+)/i // Yandex
], [[NAME, 'Yandex'], VERSION, MAJOR], [
@@ -250,15 +264,15 @@
/(navigator|netscape)\/((\d+)?[\w\.-]+)/i // Netscape
], [[NAME, 'Netscape'], VERSION, MAJOR], [
/(swiftfox)/i, // Swiftfox
/(iceweasel|camino|chimera|fennec|maemo\sbrowser|minimo|conkeror)[\/\s]?((\d+)?[\w\.\+]+)/i,
// Iceweasel/Camino/Chimera/Fennec/Maemo/Minimo/Conkeror
/(icedragon|iceweasel|camino|chimera|fennec|maemo\sbrowser|minimo|conkeror)[\/\s]?((\d+)?[\w\.\+]+)/i,
// IceDragon/Iceweasel/Camino/Chimera/Fennec/Maemo/Minimo/Conkeror
/(firefox|seamonkey|k-meleon|icecat|iceape|firebird|phoenix)\/((\d+)?[\w\.-]+)/i,
// Firefox/SeaMonkey/K-Meleon/IceCat/IceApe/Firebird/Phoenix
/(mozilla)\/((\d+)?[\w\.]+).+rv\:.+gecko\/\d+/i, // Mozilla
// Other
/(uc\s?browser|polaris|lynx|dillo|icab|doris|amaya|w3m|netsurf)[\/\s]?((\d+)?[\w\.]+)/i,
// UCBrowser/Polaris/Lynx/Dillo/iCab/Doris/Amaya/w3m/NetSurf
/(uc\s?browser|polaris|lynx|dillo|icab|doris|amaya|w3m|netsurf|qqbrowser)[\/\s]?((\d+)?[\w\.]+)/i,
// UCBrowser/Polaris/Lynx/Dillo/iCab/Doris/Amaya/w3m/NetSurf/QQBrowser
/(links)\s\(((\d+)?[\w\.]+)/i, // Links
/(gobrowser)\/?((\d+)?[\w\.]+)*/i, // GoBrowser
/(ice\s?browser)\/v?((\d+)?[\w\._]+)/i, // ICE Browser
@@ -266,6 +280,29 @@
], [NAME, VERSION, MAJOR]
],
cpu : [[
/(?:(amd|x(?:(?:86|64)[_-])?|wow|win)64)[;\)]/i // AMD64
], [[ARCHITECTURE, 'amd64']], [
/((?:i[346]|x)86)[;\)]/i // IA32
], [[ARCHITECTURE, 'ia32']], [
// PocketPC mistakenly identified as PowerPC
/windows\s(ce|mobile);\sppc;/i
], [[ARCHITECTURE, 'arm']], [
/((?:ppc|powerpc)(?:64)?)(?:\smac|;|\))/i // PowerPC
], [[ARCHITECTURE, /ower/, '', util.lowerize]], [
/(sun4\w)[;\)]/i // SPARC
], [[ARCHITECTURE, 'sparc']], [
/(ia64(?=;)|68k(?=\))|arm(?=v\d+;)|(?:irix|mips|sparc)(?:64)?(?=;)|pa-risc)/i
// IA64, 68K, ARM, IRIX, MIPS, SPARC, PA-RISC
], [ARCHITECTURE, util.lowerize]
],
device : [[
/\((ipad|playbook);[\w\s\);-]+(rim|apple)/i // iPad/PlayBook
@@ -310,7 +347,8 @@
// Alcatel/GeeksPhone/Huawei/Lenovo/Nexian/Panasonic/Sony
], [VENDOR, [MODEL, /_/g, ' '], [TYPE, MOBILE]], [
/\s((milestone|droid[2x]?))[globa\s]*\sbuild\//i, // Motorola
// Motorola
/\s((milestone|droid(?:[2-4x]|\s(?:bionic|x2|pro|razr))?(:?\s4g)?))[\w\s]+build\//i,
/(mot)[\s-]?(\w+)*/i
], [[VENDOR, 'Motorola'], MODEL, [TYPE, MOBILE]], [
/android.+\s((mz60\d|xoom[\s2]{0,2}))\sbuild\//i
@@ -431,6 +469,9 @@
this.getBrowser = function () {
return mapper.rgx.apply(this, regexes.browser);
};
this.getCPU = function () {
return mapper.rgx.apply(this, regexes.cpu);
};
this.getDevice = function () {
return mapper.rgx.apply(this, regexes.device);
};
@@ -442,13 +483,15 @@
};
this.getResult = function() {
return {
ua : this.getUA(),
browser : this.getBrowser(),
engine : this.getEngine(),
os : this.getOS(),
device : this.getDevice()
device : this.getDevice(),
cpu : this.getCPU()
};
};
this.getUA = function() {
this.getUA = function () {
return ua;
};
this.setUA = function (uastring) {
@@ -476,7 +519,7 @@
window.UAParser = UAParser;
// requirejs env (optional)
if (typeof(define) === FUNC_TYPE && define.amd) {
define(function() {
define(function () {
return UAParser;
});
}
@@ -488,7 +531,7 @@
$.ua.get = function() {
return parser.getUA();
};
$.ua.set = function(uastring) {
$.ua.set = function (uastring) {
parser.setUA(uastring);
var result = parser.getResult();
for (var prop in result) {

File diff suppressed because one or more lines are too long

View File

@@ -239,6 +239,26 @@
"major" : "7"
}
},
{
"desc" : "IE 11 with IE token",
"ua" : "Mozilla/5.0 (IE 11.0; Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko",
"expect" :
{
"name" : "IE",
"version" : "11.0",
"major" : "11"
}
},
{
"desc" : "IE 11 without IE token",
"ua" : "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko",
"expect" :
{
"name" : "IE",
"version" : "11.0",
"major" : "11"
}
},
{
"desc" : "K-Meleon",
"ua" : "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.5) Gecko/20031016 K-Meleon/0.8.2",

89
test/cpu-test.json Normal file
View File

@@ -0,0 +1,89 @@
[
{
"desc" : "i686",
"ua" : "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0",
"expect" :
{
"architecture" : "ia32"
}
},
{
"desc" : "i386",
"ua" : "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7) Gecko/20040628 Epiphany/1.2.6",
"expect" :
{
"architecture" : "ia32"
}
},
{
"desc" : "x86-64",
"ua" : "Opera/9.80 (X11; Linux x86_64; U; Linux Mint; en) Presto/2.2.15 Version/10.10",
"expect" :
{
"architecture" : "amd64"
}
},
{
"desc" : "win64",
"ua" : "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Win64; x64; Trident/6.0; .NET4.0E; .NET4.0C)",
"expect" :
{
"architecture" : "amd64"
}
},
{
"desc" : "WOW64",
"ua" : "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
"expect" :
{
"architecture" : "amd64"
}
},
{
"desc" : "ARMv6",
"ua" : "Mozilla/5.0 (X11; U; Linux armv61; en-US; rv:1.9.1b2pre) Gecko/20081015 Fennec/1.0a1",
"expect" :
{
"architecture" : "arm"
}
},
{
"desc" : "Pocket PC",
"ua" : "Opera/9.7 (Windows Mobile; PPC; Opera Mobi/35166; U; en) Presto/2.2.1",
"expect" :
{
"architecture" : "arm"
}
},
{
"desc" : "Mac PowerPC",
"ua" : "Mozilla/4.0 (compatible; MSIE 4.5; Mac_PowerPC)",
"expect" :
{
"architecture" : "ppc"
}
},
{
"desc" : "Mac PowerPC",
"ua" : "Mozilla/4.0 (compatible; MSIE 5.17; Mac_PowerPC Mac OS; en)",
"expect" :
{
"architecture" : "ppc"
}
},
{
"desc" : "Mac PowerPC",
"ua" : "iCab/2.9.5 (Macintosh; U; PPC; Mac OS X)",
"expect" :
{
"architecture" : "ppc"
}
},
{
"desc" : "UltraSPARC",
"ua" : "Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.9b5) Gecko/2008032620 Firefox/3.0b5",
"expect" :
{
"architecture" : "sparc"
}
}]

View File

@@ -18,4 +18,14 @@
"model" : "Nexus 4",
"type" : "mobile"
}
},
{
"desc" : "Motorola Droid RAZR 4G",
"ua" : "Mozilla/5.0 (Linux; U; Android 2.3; xx-xx; DROID RAZR 4G Build/6.5.1-73_DHD-11_M1-29) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"expect" :
{
"vendor" : "Motorola",
"model" : "DROID RAZR 4G",
"type" : "mobile"
}
}]

View File

@@ -1,6 +1,7 @@
var assert = require('assert');
var UAParser = require('./../src/ua-parser');
var browsers = require('./browser-test.json');
var cpus = require('./cpu-test.json');
var devices = require('./device-test.json');
var engines = require('./engine-test.json');
var os = require('./os-test.json');
@@ -12,6 +13,12 @@ var methods = [
list : browsers,
properties : ['name', 'major', 'version']
},
{
title : 'getCPU',
label : 'cpu',
list : cpus,
properties : ['architecture']
},
{
title : 'getDevice',
label : 'device',

View File

@@ -1,7 +1,7 @@
{
"title": "UAParser.js",
"name": "ua-parser-js",
"version": "0.5.26",
"version": "0.6.2",
"description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [
"user-agent",
@@ -9,7 +9,8 @@
"browser",
"engine",
"os",
"device"
"device",
"cpu"
],
"homepage": "https://faisalman.github.com/ua-parser-js",
"author": {