Some minor revisions

This commit is contained in:
Faisal Salman 2013-03-16 02:09:12 +07:00
parent 007f74178f
commit d75f84820a
6 changed files with 143 additions and 53 deletions

View File

@ -1,13 +1,14 @@
{ {
"name": "ua-parser-js", "name": "ua-parser-js",
"version": "0.5.15", "version": "0.5.20",
"description": "Lightweight JavaScript-based user-agent string parser", "description": "Lightweight JavaScript-based user-agent string parser",
"keywords": ["user-agent", "parser", "browser", "engine", "os", "device"], "keywords": ["user-agent", "parser", "browser", "engine", "os", "device"],
"scripts": ["ua-parser.js"], "scripts": ["src/ua-parser.js"],
"main": "ua-parser.js", "main": "src/ua-parser.js",
"license": "MIT", "license": "MIT",
"development": { "development": {
"jshint/jshint": "*",
"visionmedia/mocha": "*", "visionmedia/mocha": "*",
"mishoo/UglifyJS2": "*" "mishoo/uglifyjs2": "*"
} }
} }

View File

@ -1,7 +1,7 @@
{ {
"title": "UA-Parser.JS", "title": "UA-Parser.JS",
"name": "ua-parser-js", "name": "ua-parser-js",
"version": "0.5.15", "version": "0.5.20",
"author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)", "author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)",
"description": "Lightweight JavaScript-based user-agent string parser", "description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [ "keywords": [
@ -25,6 +25,7 @@
"test": "./node_modules/mocha/bin/mocha -R spec" "test": "./node_modules/mocha/bin/mocha -R spec"
}, },
"devDependencies": { "devDependencies": {
"jshint": ">= 1.1.0",
"mocha": ">= 1.7.1", "mocha": ">= 1.7.1",
"uglify-js": ">= 1.3.4" "uglify-js": ">= 1.3.4"
}, },
@ -53,7 +54,8 @@
"node": "*" "node": "*"
}, },
"directories": { "directories": {
"lib": "." "lib": "src",
"test": "test"
}, },
"files": [ "files": [
"" ""

View File

@ -1,13 +1,12 @@
# UA-Parser.JS # UAParser.js
Lightweight JavaScript-based User-Agent string parser Lightweight JavaScript-based User-Agent string parser
[![Build Status](https://travis-ci.org/faisalman/ua-parser-js.png)](https://travis-ci.org/faisalman/ua-parser-js) [![Build Status](https://travis-ci.org/faisalman/ua-parser-js.png)](https://travis-ci.org/faisalman/ua-parser-js)
* Author : Faisalman <<fyzlman@gmail.com>> * Author : Faisalman <<fyzlman@gmail.com>>
* Home : http://faisalman.github.com/ua-parser-js * Demo : http://faisalman.github.com/ua-parser-js
* Source : https://github.com/faisalman/ua-parser-js * Source : https://github.com/faisalman/ua-parser-js
* License : GPLv2 & MIT
## Features ## Features
@ -18,17 +17,24 @@ Extract detailed type of web browser, layout engine, operating system, and devic
## Methods ## Methods
* `getBrowser()` * `getBrowser()`
* returns `{ name: '', major: '', version: '' }`
* `getDevice()` * `getDevice()`
* returns `{ model: '', type: '', vendor: '' }`
* `getEngine()` * `getEngine()`
* returns `{ name: '', version: '' }`
* `getOS()` * `getOS()`
* returns `{ name: '', version: '' }`
* `getResult()` * `getResult()`
* returns `{ browser: {}, device: {}, engine: {}, os: {} }`
* `getUA()` * `getUA()`
* returns UA string of current instance
* `setUA(uastring)` * `setUA(uastring)`
* set & parse UA string
## Example ## Example
```html ```html
<script type="text/javascript" src="ua-parser.js"></script> <script type="text/javascript" src="ua-parser.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var parser = new UAParser(); var parser = new UAParser();
@ -60,25 +66,35 @@ Extract detailed type of web browser, layout engine, operating system, and devic
*/ */
// let's test a custom user-agent string as an example // let's test a custom user-agent string as an example
var uastr = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2"; var uastring = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2";
parser.setUA(uastr); parser.setUA(uastring);
console.log(parser.getResult().browser); // {name: "Chromium", major: "15", version: "15.0.874.106"} var result = parser.getResult();
console.log(parser.getResult().device); // {model: undefined, type: undefined, vendor: undefined} // this will also produce the same result (without instantiation):
console.log(parser.getResult().engine); // {name: "WebKit", version: "535.2"} // var result = UAParser(uastring);
console.log(parser.getResult().os); // {name: "Ubuntu", version: "11.10"}
// let's take another test please console.log(result.browser); // {name: "Chromium", major: "15", version: "15.0.874.106"}
console.log(parser.setUA("Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)").getBrowser().name); // "Konqueror" console.log(result.device); // {model: undefined, type: undefined, vendor: undefined}
console.log(result.os); // {name: "Ubuntu", version: "11.10"}
console.log(result.os.version); // "11.10"
console.log(result.engine.name); // "WebKit"
// do some other tests
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
console.log(parser.setUA(uastring2).getBrowser().name); // "Konqueror"
console.log(parser.getOS()); // {name: "OpenBSD", version: undefined} console.log(parser.getOS()); // {name: "OpenBSD", version: undefined}
console.log(parser.getEngine()); // {name: "KHTML", version: "4.1.4"} console.log(parser.getEngine()); // {name: "KHTML", version: "4.1.4"}
var uastring3 = '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(uastring3).getDevice().model); // "PlayBook"
console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
console.log(parser.getBrowser().name); // "Safari"
</script> </script>
``` ```
## Using requirejs ## Using requirejs
If you're using requirejs, you can load UA-Parser like any other module.
```js ```js
require(['ua-parser'], function(UAParser) { require(['ua-parser'], function(UAParser) {
var parser = new UAParser(); var parser = new UAParser();
@ -95,16 +111,30 @@ $ npm install ua-parser-js
```js ```js
var UAParser = require('ua-parser'); var UAParser = require('ua-parser');
var parser = new UAParser(); var parser = new UAParser();
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.getResult());
```
console.log(parser.setUA(uaString).getDevice().model); // "PlayBook" ## Using component
console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
console.log(parser.getEngine().name); // "WebKit" ```sh
$ component install faisalman/ua-parser-js
```
```js
var UAParser = require('ua-parser-js');
var parser = new UAParser();
console.log(parser.getResult());
```
## Using bower
```sh
$ bower install ua-parser-js
``` ```
## Using jQuery ## 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. If you're using jQuery, `$.ua` object will be created automatically based on browser's user-agent. In case you need, `UAParser` constructor is still present in global though. Getter / setter: `$.ua.get()` / `$.ua.set(uastring)`.
```js ```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': // 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':
@ -125,8 +155,26 @@ console.log($.ua.browser.version); // "4.0"
console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4 console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4
``` ```
## Development
Install dependencies
```sh
$ npm install jshint
$ npm install mocha
$ npm install uglify-js
```
Verify, test, & minify script
```sh
$ ./build/build.sh
```
## License ## License
Dual licensed under GPLv2 & MIT
Copyright © 2012-2013 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 Permission is hereby granted, free of charge, to any person obtaining a copy of

View File

@ -1,14 +1,19 @@
// UA-Parser.JS v0.5.15 // UAParser.js v0.5.20
// Lightweight JavaScript-based User-Agent string parser // Lightweight JavaScript-based User-Agent string parser
// https://github.com/faisalman/ua-parser-js // https://github.com/faisalman/ua-parser-js
// //
// Copyright © 2012-2013 Faisalman <fyzlman@gmail.com> // Copyright © 2012-2013 Faisalman <fyzlman@gmail.com>
// Dual licensed under GPLv2 & MIT // Dual licensed under GPLv2 & MIT
(function (global, undefined) { (function (window, undefined) {
'use strict'; 'use strict';
//////////////
// Constants
/////////////
var EMPTY = '', var EMPTY = '',
FUNC = 'function', FUNC = 'function',
UNDEF = 'undefined', UNDEF = 'undefined',
@ -23,6 +28,12 @@
MOBILE = 'mobile', MOBILE = 'mobile',
TABLET = 'tablet'; TABLET = 'tablet';
///////////////
// Map helper
//////////////
var mapper = { var mapper = {
regex : function () { regex : function () {
@ -96,6 +107,12 @@
} }
}; };
///////////////
// String map
//////////////
var maps = { var maps = {
browser : { browser : {
@ -147,6 +164,12 @@
} }
}; };
//////////////
// Regex map
/////////////
var regexes = { var regexes = {
browser : [[ browser : [[
@ -155,7 +178,7 @@
/(opera\smini)\/((\d+)?[\w\.-]+)/i, // Opera Mini /(opera\smini)\/((\d+)?[\w\.-]+)/i, // Opera Mini
/(opera\s[mobiletab]+).+version\/((\d+)?[\w\.-]+)/i, // Opera Mobi/Tablet /(opera\s[mobiletab]+).+version\/((\d+)?[\w\.-]+)/i, // Opera Mobi/Tablet
/(opera).+version\/((\d+)?[\w\.]+)/i, // Opera > 9.80 /(opera).+version\/((\d+)?[\w\.]+)/i, // Opera > 9.80
/(opera)[\/\s]+((\d+)?[\w\.]+)/i, // Opera < 9.80 /(opera)[\/\s]+((\d+)?[\w\.]+)/i // Opera < 9.80
], [NAME, VERSION, MAJOR], [ ], [NAME, VERSION, MAJOR], [
@ -374,26 +397,31 @@
] ]
}; };
/////////////////
// Constructor
////////////////
var UAParser = function UAParser (uastring) { var UAParser = function UAParser (uastring) {
var ua = uastring || ((global && global.navigator && global.navigator.userAgent) ? global.navigator.userAgent : EMPTY); var ua = uastring || ((window && window.navigator && window.navigator.userAgent) ? window.navigator.userAgent : EMPTY);
if (!(this instanceof UAParser)) {
return new UAParser(uastring).getResult();
}
this.getBrowser = function () { this.getBrowser = function () {
return mapper.regex.apply(this, regexes.browser); return mapper.regex.apply(this, regexes.browser);
}; };
this.getDevice = function () { this.getDevice = function () {
return mapper.regex.apply(this, regexes.device); return mapper.regex.apply(this, regexes.device);
}; };
this.getEngine = function () { this.getEngine = function () {
return mapper.regex.apply(this, regexes.engine); return mapper.regex.apply(this, regexes.engine);
}; };
this.getOS = function () { this.getOS = function () {
return mapper.regex.apply(this, regexes.os); return mapper.regex.apply(this, regexes.os);
}; };
this.getResult = function() { this.getResult = function() {
return { return {
browser : this.getBrowser(), browser : this.getBrowser(),
@ -402,19 +430,22 @@
device : this.getDevice() device : this.getDevice()
}; };
}; };
this.getUA = function() { this.getUA = function() {
return ua; return ua;
}; };
this.setUA = function (uastring) { this.setUA = function (uastring) {
ua = uastring; ua = uastring;
return this; return this;
}; };
this.setUA(ua); this.setUA(ua);
}; };
///////////
// Export
//////////
// check js environment // check js environment
if (typeof exports !== UNDEF && !/\[object\s[DOM]*Window\]/.test(global.toString())) { if (typeof exports !== UNDEF && !/\[object\s[DOM]*Window\]/.test(global.toString())) {
// nodejs env // nodejs env
@ -429,21 +460,22 @@
}); });
} else { } else {
// browser env // browser env
global.UAParser = UAParser; window.UAParser = UAParser;
// jQuery specific // jQuery specific
if (typeof global.jQuery !== UNDEF) { if (typeof window.jQuery !== UNDEF) {
var parser = new UAParser(); var parser = new UAParser();
global.jQuery.ua = parser.getResult(); window.jQuery.ua = parser.getResult();
global.jQuery.ua.get = function() { window.jQuery.ua.get = function() {
return parser.getUA(); return parser.getUA();
}; };
global.jQuery.ua.set = function(uastring) { window.jQuery.ua.set = function(uastring) {
parser.setUA(uastring); parser.setUA(uastring);
var result = parser.getResult(); var result = parser.getResult();
for (var prop in result) { for (var prop in result) {
global.jQuery.ua[prop] = result[prop]; window.jQuery.ua[prop] = result[prop];
} }
}; };
} }
} }
})(this); })(this);

7
src/ua-parser.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
{ {
"title": "UA-Parser.JS", "title": "UA-Parser.JS",
"name": "ua-parser-js", "name": "ua-parser-js",
"version": "0.5.15", "version": "0.5.20",
"description": "Lightweight JavaScript-based user-agent string parser", "description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [ "keywords": [
"user-agent", "user-agent",
@ -27,5 +27,5 @@
], ],
"bugs": "https://github.com/faisalman/ua-parser-js/issues", "bugs": "https://github.com/faisalman/ua-parser-js/issues",
"docs": "https://github.com/faisalman/ua-parser-js", "docs": "https://github.com/faisalman/ua-parser-js",
"download": "https://raw.github.com/faisalman/ua-parser-js/master/ua-parser.min.js" "download": "https://raw.github.com/faisalman/ua-parser-js/master/src/ua-parser.min.js"
} }