Compare commits

..

14 Commits

Author SHA1 Message Date
Faisal Salman
615d6a2034 Add new Webkit Opera 2013-03-05 21:05:38 +07:00
Faisal Salman
40ac5c5bc3 Fix issue #12 2013-02-27 21:44:43 +07:00
Faisal Salman
3eb1c1c352 Revise jQuery plugin API 2013-02-15 19:02:44 +07:00
Faisal Salman
217cef226a Update version 2013-02-15 10:23:49 +07:00
Faisal Salman
a5e4b7bf72 Merge pull request #11 from jbuck/requirejs
Add requirejs module support
2013-02-14 18:25:59 -08:00
Jon Buckley
8aaf7db771 Add requirejs module support
Fixes #10.
2013-02-14 14:52:41 -05:00
Faisal Salman
0109c76858 Automatically create $.ua object if jQuery is present 2013-02-07 23:25:21 +07:00
Faisal Salman
d1120a4d5d Update contributors 2013-02-05 20:43:15 +07:00
Faisal Salman
204345a8bf Add Rekonq, Comodo Dragon, Conkeror, Amaya, w3m, Links, ICE Browser, NetSurf, RISC OS 2013-02-05 20:29:26 +07:00
Faisal Salman
a20d8f7052 Sprint default vendor to Sprint 2013-02-05 14:43:17 +07:00
Faisal Salman
9a90d932ce Add new OS: Firefox OS 2013-01-28 18:46:03 +07:00
Faisal Salman
cea29e5c1b Add new devices: Alcatel, GeeksPhone, Huawei, Lenovo, Nexian, Panasonic, Sony 2013-01-28 18:21:12 +07:00
Faisal Salman
e0b6ae65e5 Better fix for issue #8 2013-01-28 17:11:12 +07:00
Faisal Salman
59714adea3 Add a test for issue #8 2013-01-25 20:33:03 +07:00
6 changed files with 192 additions and 53 deletions

View File

@@ -1,11 +1,11 @@
{
"title": "UA-Parser.JS",
"name": "ua-parser-js",
"version": "0.5.0",
"version": "0.5.15",
"author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)",
"description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [
"user agent",
"user-agent",
"parser",
"browser",
"engine",
@@ -14,7 +14,11 @@
],
"homepage": "http://github.com/faisalman/ua-parser-js",
"contributors": [
"Faisal Salman <fyzlman@gmail.com>"
"Faisal Salman <fyzlman@gmail.com>",
"Christopher De Cairos <chris.decairos@gmail.com>",
"John Tantalo <john.tantalo@gmail.com>",
"Jon Buckley <jon@jbuckley.ca>",
"Lee Treveil <leetreveil@gmail.com>"
],
"main": "ua-parser",
"scripts": {

View File

@@ -11,7 +11,7 @@ Lightweight JavaScript-based User-Agent string parser
## Features
Extract detailed type of web browser, layout engine, operating system, and device purely from user-agent string.
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).
![It's over 9000](https://pbs.twimg.com/media/A9LpEG6CIAA5VrT.jpg)
@@ -65,7 +65,7 @@ Extract detailed type of web browser, layout engine, operating system, and devic
console.log(parser.getResult().browser); // {name: "Chromium", major: "15", version: "15.0.874.106"}
console.log(parser.getResult().device); // {model: undefined, type: undefined, vendor: undefined}
console.log(parser.getResult().engine); // {name: "AppleWebKit", version: "535.2"}
console.log(parser.getResult().engine); // {name: "WebKit", version: "535.2"}
console.log(parser.getResult().os); // {name: "Ubuntu", version: "11.10"}
// let's take another test please
@@ -75,6 +75,17 @@ Extract detailed type of web browser, layout engine, operating system, and devic
</script>
```
## Using requirejs
If you're using requirejs, you can load UA-Parser like any other module.
```js
require(['ua-parser'], function(UAParser) {
var parser = new UAParser();
console.log(parser.getResult());
});
```
## Using node.js
```sh
@@ -88,12 +99,35 @@ var uaString = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWe
console.log(parser.setUA(uaString).getDevice().model); // "PlayBook"
console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
console.log(parser.getEngine().name); // "AppleWebKit"
console.log(parser.getEngine().name); // "WebKit"
```
## Using jQuery
If you're using jQuery, `$.ua` object will be created automatically based on container's user-agent. Use `$.ua.get()` / `$.ua.set(uastring)` to get/set user-agent. In case you need, `UAParser` constructor is still present in global though.
```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':
// Do some tests
console.log($.ua.device); // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"}
console.log($.ua.os); // {name: "Android", version: "2.3.4"}
console.log($.ua.os.name); // "Android"
console.log($.ua.get()); // "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0"
// reset to custom user-agent
$.ua.set('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13');
// Test again
console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"}
console.log($.ua.engine.name); // "Webkit"
console.log($.ua.browser.version); // "4.0"
console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4
```
## License
Copyright © 2012 Faisalman <<fyzlman@gmail.com>>
Copyright © 2012-2013 Faisalman <<fyzlman@gmail.com>>
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

28
test.js
View File

@@ -462,6 +462,16 @@ var browsers = [
major : '11'
}
},
{
desc : 'Opera Webkit',
ua : 'Mozilla/5.0 AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.123 Mobile Safari/537.22 OPR/14.0.1025.52315',
expect :
{
name : 'Opera',
version : '14.0.1025.52315',
major : '14'
}
},
{
desc : 'Opera Mini',
ua : 'Opera/9.80 (J2ME/MIDP; Opera Mini/5.1.21214/19.916; U; en) Presto/2.5.25',
@@ -797,6 +807,15 @@ var os = [
version : '7.0'
}
},
{
desc : 'Windows Phone 8',
ua : 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; HTC; Windows Phone 8X by HTC)',
expect :
{
name : 'Windows Phone',
version : '8.0'
}
},
{
desc : 'BlackBerry',
ua : 'BlackBerry9300/5.0.0.912 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/378',
@@ -896,6 +915,15 @@ var os = [
version : ''
}
},
{
desc : 'Firefox OS',
ua : 'Mozilla/5.0 (Mobile; rv:14.0) Gecko/14.0 Firefox/14.0',
expect :
{
name : 'Firefox OS',
version : undefined
}
},
{
desc : 'Nintendo',
ua : '',

31
ua-parser-js.jquery.json Normal file
View File

@@ -0,0 +1,31 @@
{
"title": "UA-Parser.JS",
"name": "ua-parser-js",
"version": "0.5.15",
"description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [
"user-agent",
"parser",
"browser",
"engine",
"os",
"device"
],
"homepage": "https://faisalman.github.com/ua-parser-js",
"author": {
"name": "Faisal Salman",
"email": "fyzlman@gmail.com"
},
"dependencies": {
"jquery": ">=1.5"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
],
"bugs": "https://github.com/faisalman/ua-parser-js/issues",
"docs": "https://github.com/faisalman/ua-parser-js",
"download": "https://raw.github.com/faisalman/ua-parser-js/master/ua-parser.min.js"
}

View File

@@ -1,24 +1,24 @@
// UA-Parser.JS v0.5.1
// UA-Parser.JS v0.5.15
// Lightweight JavaScript-based User-Agent string parser
// https://github.com/faisalman/ua-parser-js
//
// Copyright © 2012 Faisalman
// Copyright © 2012-2013 Faisalman
// Dual licensed under GPLv2 & MIT
(function (global, undefined) {
'use strict';
var EMPTY = '',
FUNC = 'function',
UNDEF = 'undefined',
OBJ = 'object',
OBJ = 'object',
MAJOR = 'major',
MODEL = 'model',
NAME = 'name',
TYPE = 'type',
VENDOR = 'vendor',
VERSION = 'version',
VERSION = 'version',
CONSOLE = 'console',
MOBILE = 'mobile',
TABLET = 'tablet';
@@ -97,14 +97,14 @@
};
var maps = {
browser : {
oldsafari : {
browser : {
oldsafari : {
major : {
'1' : ['/85', '/125', '/312'],
'2' : ['/412', '/416', '/417', '/419'],
'undefined' : '/'
},
},
version : {
'1.0' : '/85',
'1.2' : '/125',
@@ -117,17 +117,21 @@
}
}
},
device : {
htc : {
device : {
sprint : {
model : {
'Evo Shift 4G' : '7373KT'
},
vendor : {
'HTC' : 'APA',
'Sprint' : 'Sprint'
}
}
},
os : {
windows : {
os : {
windows : {
version : {
'ME' : '4.90',
'NT 3.11' : 'NT3.51',
@@ -152,6 +156,11 @@
/(opera\s[mobiletab]+).+version\/((\d+)?[\w\.-]+)/i, // Opera Mobi/Tablet
/(opera).+version\/((\d+)?[\w\.]+)/i, // Opera > 9.80
/(opera)[\/\s]+((\d+)?[\w\.]+)/i, // Opera < 9.80
], [NAME, VERSION, MAJOR], [
/\s(opr)\/((\d+)?[\w\.]+)/i // Opera Webkit
], [[NAME, 'Opera'], VERSION, MAJOR], [
// Mixed
/(kindle)\/((\d+)?[\w\.]+)/i, // Kindle
@@ -164,32 +173,36 @@
/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
], [NAME, VERSION, MAJOR], [
/(yabrowser)\/((\d+)?[\w\.]+)/i // Yandex
], [[NAME, 'Yandex'], VERSION, MAJOR], [
/(comodo_dragon)\/((\d+)?[\w\.]+)/i // Comodo Dragon
], [[NAME, /_/g, ' '], VERSION, MAJOR], [
/(chrome|omniweb|arora|[tizenoka]{5}\s?browser)\/v?((\d+)?[\w\.]+)/i
// Chrome/OmniWeb/Arora/Tizen/Nokia
], [NAME, VERSION, MAJOR], [
/(dolfin)\/((\d+)?[\w\.]+)/i // Dolphin
], [[NAME, 'Dolphin'], VERSION, MAJOR], [
/((?:android.+)crmo|crios)\/((\d+)?[\w\.]+)/i // Chrome for Android/iOS
], [[NAME, 'Chrome'], VERSION, MAJOR], [
/version\/((\d+)?[\w\.]+).+?mobile\/\w+\s(safari)/i // Mobile Safari
], [VERSION, MAJOR, [NAME, 'Mobile Safari']], [
/version\/((\d+)?[\w\.]+).+?(mobile\s?safari|safari)/i // Safari & Safari Mobile
], [VERSION, MAJOR, NAME], [
/applewebkit.+?(mobile\s?safari|safari)((\/[\w\.]+))/i // Safari < 3.0
], [NAME, [MAJOR, mapper.string, maps.browser.oldsafari.major], [VERSION, mapper.string, maps.browser.oldsafari.version]], [
/(konqueror)\/((\d+)?[\w\.]+)/i, // Konqueror
/(applewebkit|khtml)\/((\d+)?[\w\.]+)/i
], [NAME, VERSION, MAJOR], [
@@ -198,16 +211,18 @@
/(navigator|netscape)\/((\d+)?[\w\.-]+)/i // Netscape
], [[NAME, 'Netscape'], VERSION, MAJOR], [
/(swiftfox)/i, // Swiftfox
/(iceweasel|camino|chimera|fennec|maemo\sbrowser|minimo)[\/\s]?((\d+)?[\w\.\+]+)/i,
// Iceweasel/Camino/Chimera/Fennec/Maemo/Minimo
/(iceweasel|camino|chimera|fennec|maemo\sbrowser|minimo|conkeror)[\/\s]?((\d+)?[\w\.\+]+)/i,
// 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)[\/\s]?((\d+)?[\w\.]+)/i,
// UCBrowser/Polaris/Lynx/Dillo/iCab/Doris
/(uc\s?browser|polaris|lynx|dillo|icab|doris|amaya|w3m|netsurf)[\/\s]?((\d+)?[\w\.]+)/i,
// UCBrowser/Polaris/Lynx/Dillo/iCab/Doris/Amaya/w3m/NetSurf
/(links)\s\(((\d+)?[\w\.]+)/i, // Links
/(gobrowser)\/?((\d+)?[\w\.]+)*/i, // GoBrowser
/(ice\s?browser)\/v?((\d+)?[\w\._]+)/i, // ICE Browser
/(mosaic)[\/\s]((\d+)?[\w\.]+)/i // Mosaic
], [NAME, VERSION, MAJOR]
],
@@ -216,7 +231,7 @@
/\((ipad|playbook);[\w\s\);-]+(rim|apple)/i // iPad/PlayBook
], [MODEL, VENDOR, [TYPE, TABLET]], [
/(hp).+(touchpad)/i, // HP TouchPad
/(kindle)\/([\w\.]+)/i, // Kindle
/\s(nook)[\w\s]+build\/(\w+)/i, // Nook
@@ -225,7 +240,7 @@
/\((ip[honed]+);.+(apple)/i // iPod/iPhone
], [MODEL, VENDOR, [TYPE, MOBILE]], [
/(blackberry)[\s-]?(\w+)/i, // BlackBerry
/(blackberry|benq|palm(?=\-)|sonyericsson|acer|asus|dell|huawei|meizu|motorola)[\s_-]?([\w-]+)*/i,
// BenQ/Palm/Sony-Ericsson/Acer/Asus/Dell/Huawei/Meizu/Motorola
@@ -243,12 +258,14 @@
/(nintendo|playstation)\s([wids3portablev]+)/i // Nintendo/Playstation
], [VENDOR, MODEL, [TYPE, CONSOLE]], [
/(sprint\sapa)(\w+)/i
], [[VENDOR, 'HTC'], [MODEL, mapper.string, maps.device.htc.model], [TYPE, MOBILE]], [
/(sprint\s(\w+))/i // Sprint Phones
], [[VENDOR, mapper.string, maps.device.sprint.vendor], [MODEL, mapper.string, maps.device.sprint.model], [TYPE, MOBILE]], [
/(htc)[;_\s-]+([\w\s]+(?=\))|\w+)*/i, // HTC
/(zte)-(\w+)*/i // ZTE
/(zte)-(\w+)*/i, // ZTE
/(alcatel|geeksphone|huawei|lenovo|nexian|panasonic|;\ssony)[_\s-]?([\w-]+)*/i
// Alcatel/GeeksPhone/Huawei/Lenovo/Nexian/Panasonic/Sony
], [VENDOR, [MODEL, /_/g, ' '], [TYPE, MOBILE]], [
/\s((milestone|droid[2x]?))[globa\s]*\sbuild\//i, // Motorola
@@ -258,7 +275,7 @@
], [[VENDOR, 'Motorola'], MODEL, [TYPE, TABLET]], [
/android.+((sch-i[89]0\d|shw-m380s|gt-p\d{4}|gt-n8000|sgh-t8[56]9))/i
], [[VENDOR, 'Samsung'], MODEL, [TYPE, TABLET]], [ // Samsung
], [[VENDOR, 'Samsung'], MODEL, [TYPE, TABLET]], [ // Samsung
/((s[cgp]h-\w+|gt-\w+|galaxy\snexus))/i,
/(sam[sung]*)[\s-]*(\w+-?[\w-]*)*/i,
/sec-((sgh\w+))/i
@@ -277,7 +294,7 @@
], [[VENDOR, 'LG'], MODEL, [TYPE, TABLET]], [
/(lg)[e;\s-\/]+(\w+)*/i
], [[VENDOR, 'LG'], MODEL, [TYPE, MOBILE]], [
/(mobile|tablet);.+rv\:.+gecko\//i // Unidentifiable
], [TYPE, VENDOR, MODEL]
],
@@ -285,9 +302,11 @@
engine : [[
/(presto)\/([\w\.]+)/i, // Presto
/(webkit|trident|netfront)\/([\w\.]+)/i, // WebKit/Trident/NetFront
/(webkit|trident|netfront|netsurf|amaya|lynx|w3m)\/([\w\.]+)/i, // WebKit/Trident/NetFront/NetSurf/Amaya/Lynx/w3m
/(khtml)\/([\w\.]+)/i, // KHTML
/(tasman)\s([\w\.]+)/i // Tasman
/(tasman)\s([\w\.]+)/i, // Tasman
/(links)\s\(([\w\.]+)/i, // Links
/(icab)[\/\s]([2-3]\.[\d\.]+)/i // iCab
], [NAME, VERSION], [
/rv\:([\w\.]+).*(gecko)/i // Gecko
@@ -298,7 +317,7 @@
// Windows based
/(windows)\snt\s6\.2;\s(arm)/i, // Windows RT
/(windows\sphone\sos|windows\smobile|windows)[\s\/]?([ntce\d\.\s]+\w)/i
/(windows\sphone(?:\sos)*|windows\smobile|windows)[\s\/]?([ntce\d\.\s]+\w)/i
], [NAME, [VERSION, mapper.string, maps.os.windows.version]], [
/(win(?=3|9|n)|win\s9x\s)([nt\d\.]+)/i
], [[NAME, 'Windows'], [VERSION, mapper.string, maps.os.windows.version]], [
@@ -313,7 +332,10 @@
], [NAME, VERSION], [
/(symbian\s?os|symbos|s60(?=;))[\/\s-]?([\w\.]+)*/i // Symbian
], [[NAME, 'Symbian'], VERSION],[
/mozilla.+\(mobile;.+gecko.+firefox/i // Firefox OS
], [[NAME, 'Firefox OS'], VERSION], [
// Console
/(nintendo|playstation)\s([wids3portablev]+)/i, // Nintendo/Playstation
// GNU/Linux based
@@ -344,9 +366,9 @@
// Other
/(haiku)\s(\w+)/i, // Haiku
/(aix)\s((\d)(?=\.|\)|\s)[\w\.]*)*/i, // AIX
/(macintosh|mac(?=_powerpc)|plan\s9|minix|beos|os\/2|amigaos|morphos)/i,
// Plan9/Minix/BeOS/OS2/AmigaOS/MorphOS
/(aix)\s((\d)(?=\.|\)|\s)[\w\.]*)*/i, // AIX
/(macintosh|mac(?=_powerpc)|plan\s9|minix|beos|os\/2|amigaos|morphos|risc\sos)/i,
// Plan9/Minix/BeOS/OS2/AmigaOS/MorphOS/RISCOS
/(unix)\s?([\w\.]+)*/i // UNIX
], [NAME, VERSION]
]
@@ -400,8 +422,28 @@
exports = module.exports = UAParser;
}
exports.UAParser = UAParser;
} else if (typeof define === FUNC && define.amd) {
// requirejs env
define(function() {
return UAParser;
});
} else {
// browser env
global['UAParser'] = UAParser;
global.UAParser = UAParser;
// jQuery specific
if (typeof global.jQuery !== UNDEF) {
var parser = new UAParser();
global.jQuery.ua = parser.getResult();
global.jQuery.ua.get = function() {
return parser.getUA();
};
global.jQuery.ua.set = function(uastring) {
parser.setUA(uastring);
var result = parser.getResult();
for (var prop in result) {
global.jQuery.ua[prop] = result[prop];
}
};
}
}
})(this);

6
ua-parser.min.js vendored

File diff suppressed because one or more lines are too long