mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-09-27 07:58:45 +03:00
Some minor revisions
This commit is contained in:
parent
007f74178f
commit
d75f84820a
@ -1,13 +1,14 @@
|
||||
{
|
||||
"name": "ua-parser-js",
|
||||
"version": "0.5.15",
|
||||
"version": "0.5.20",
|
||||
"description": "Lightweight JavaScript-based user-agent string parser",
|
||||
"keywords": ["user-agent", "parser", "browser", "engine", "os", "device"],
|
||||
"scripts": ["ua-parser.js"],
|
||||
"main": "ua-parser.js",
|
||||
"scripts": ["src/ua-parser.js"],
|
||||
"main": "src/ua-parser.js",
|
||||
"license": "MIT",
|
||||
"development": {
|
||||
"jshint/jshint": "*",
|
||||
"visionmedia/mocha": "*",
|
||||
"mishoo/UglifyJS2": "*"
|
||||
"mishoo/uglifyjs2": "*"
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"title": "UA-Parser.JS",
|
||||
"name": "ua-parser-js",
|
||||
"version": "0.5.15",
|
||||
"version": "0.5.20",
|
||||
"author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)",
|
||||
"description": "Lightweight JavaScript-based user-agent string parser",
|
||||
"keywords": [
|
||||
@ -25,6 +25,7 @@
|
||||
"test": "./node_modules/mocha/bin/mocha -R spec"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jshint": ">= 1.1.0",
|
||||
"mocha": ">= 1.7.1",
|
||||
"uglify-js": ">= 1.3.4"
|
||||
},
|
||||
@ -53,7 +54,8 @@
|
||||
"node": "*"
|
||||
},
|
||||
"directories": {
|
||||
"lib": "."
|
||||
"lib": "src",
|
||||
"test": "test"
|
||||
},
|
||||
"files": [
|
||||
""
|
||||
|
86
readme.md
86
readme.md
@ -1,13 +1,12 @@
|
||||
# UA-Parser.JS
|
||||
# UAParser.js
|
||||
|
||||
Lightweight JavaScript-based User-Agent string parser
|
||||
|
||||
[](https://travis-ci.org/faisalman/ua-parser-js)
|
||||
|
||||
* 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
|
||||
* License : GPLv2 & MIT
|
||||
|
||||
## Features
|
||||
|
||||
@ -18,17 +17,24 @@ Extract detailed type of web browser, layout engine, operating system, and devic
|
||||
## Methods
|
||||
|
||||
* `getBrowser()`
|
||||
* returns `{ name: '', major: '', version: '' }`
|
||||
* `getDevice()`
|
||||
* returns `{ model: '', type: '', vendor: '' }`
|
||||
* `getEngine()`
|
||||
* returns `{ name: '', version: '' }`
|
||||
* `getOS()`
|
||||
* returns `{ name: '', version: '' }`
|
||||
* `getResult()`
|
||||
* returns `{ browser: {}, device: {}, engine: {}, os: {} }`
|
||||
* `getUA()`
|
||||
* returns UA string of current instance
|
||||
* `setUA(uastring)`
|
||||
* set & parse UA string
|
||||
|
||||
## Example
|
||||
|
||||
```html
|
||||
<script type="text/javascript" src="ua-parser.js"></script>
|
||||
<script type="text/javascript" src="ua-parser.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
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
|
||||
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";
|
||||
parser.setUA(uastr);
|
||||
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(uastring);
|
||||
|
||||
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: "WebKit", version: "535.2"}
|
||||
console.log(parser.getResult().os); // {name: "Ubuntu", version: "11.10"}
|
||||
var result = parser.getResult();
|
||||
// this will also produce the same result (without instantiation):
|
||||
// var result = UAParser(uastring);
|
||||
|
||||
// let's take another test please
|
||||
console.log(parser.setUA("Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)").getBrowser().name); // "Konqueror"
|
||||
console.log(result.browser); // {name: "Chromium", major: "15", version: "15.0.874.106"}
|
||||
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.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>
|
||||
```
|
||||
|
||||
## 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();
|
||||
@ -95,16 +111,30 @@ $ npm install ua-parser-js
|
||||
```js
|
||||
var UAParser = require('ua-parser');
|
||||
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"
|
||||
console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
|
||||
console.log(parser.getEngine().name); // "WebKit"
|
||||
## Using component
|
||||
|
||||
```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
|
||||
|
||||
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
|
||||
// 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
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
Install dependencies
|
||||
|
||||
```sh
|
||||
$ npm install jshint
|
||||
$ npm install mocha
|
||||
$ npm install uglify-js
|
||||
```
|
||||
|
||||
Verify, test, & minify script
|
||||
|
||||
```sh
|
||||
$ ./build/build.sh
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Dual licensed under GPLv2 & MIT
|
||||
|
||||
Copyright © 2012-2013 Faisalman <<fyzlman@gmail.com>>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
|
@ -1,14 +1,19 @@
|
||||
// UA-Parser.JS v0.5.15
|
||||
// UAParser.js v0.5.20
|
||||
// Lightweight JavaScript-based User-Agent string parser
|
||||
// https://github.com/faisalman/ua-parser-js
|
||||
//
|
||||
// Copyright © 2012-2013 Faisalman <fyzlman@gmail.com>
|
||||
// Dual licensed under GPLv2 & MIT
|
||||
|
||||
(function (global, undefined) {
|
||||
(function (window, undefined) {
|
||||
|
||||
'use strict';
|
||||
|
||||
//////////////
|
||||
// Constants
|
||||
/////////////
|
||||
|
||||
|
||||
var EMPTY = '',
|
||||
FUNC = 'function',
|
||||
UNDEF = 'undefined',
|
||||
@ -23,6 +28,12 @@
|
||||
MOBILE = 'mobile',
|
||||
TABLET = 'tablet';
|
||||
|
||||
|
||||
///////////////
|
||||
// Map helper
|
||||
//////////////
|
||||
|
||||
|
||||
var mapper = {
|
||||
|
||||
regex : function () {
|
||||
@ -96,6 +107,12 @@
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///////////////
|
||||
// String map
|
||||
//////////////
|
||||
|
||||
|
||||
var maps = {
|
||||
|
||||
browser : {
|
||||
@ -147,6 +164,12 @@
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// Regex map
|
||||
/////////////
|
||||
|
||||
|
||||
var regexes = {
|
||||
|
||||
browser : [[
|
||||
@ -155,7 +178,7 @@
|
||||
/(opera\smini)\/((\d+)?[\w\.-]+)/i, // Opera Mini
|
||||
/(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
|
||||
/(opera)[\/\s]+((\d+)?[\w\.]+)/i // Opera < 9.80
|
||||
|
||||
], [NAME, VERSION, MAJOR], [
|
||||
|
||||
@ -374,26 +397,31 @@
|
||||
]
|
||||
};
|
||||
|
||||
|
||||
/////////////////
|
||||
// Constructor
|
||||
////////////////
|
||||
|
||||
|
||||
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 () {
|
||||
return mapper.regex.apply(this, regexes.browser);
|
||||
};
|
||||
|
||||
this.getDevice = function () {
|
||||
return mapper.regex.apply(this, regexes.device);
|
||||
};
|
||||
|
||||
this.getEngine = function () {
|
||||
return mapper.regex.apply(this, regexes.engine);
|
||||
};
|
||||
|
||||
this.getOS = function () {
|
||||
return mapper.regex.apply(this, regexes.os);
|
||||
};
|
||||
|
||||
this.getResult = function() {
|
||||
return {
|
||||
browser : this.getBrowser(),
|
||||
@ -402,19 +430,22 @@
|
||||
device : this.getDevice()
|
||||
};
|
||||
};
|
||||
|
||||
this.getUA = function() {
|
||||
return ua;
|
||||
};
|
||||
|
||||
this.setUA = function (uastring) {
|
||||
ua = uastring;
|
||||
return this;
|
||||
};
|
||||
|
||||
this.setUA(ua);
|
||||
};
|
||||
|
||||
|
||||
///////////
|
||||
// Export
|
||||
//////////
|
||||
|
||||
|
||||
// check js environment
|
||||
if (typeof exports !== UNDEF && !/\[object\s[DOM]*Window\]/.test(global.toString())) {
|
||||
// nodejs env
|
||||
@ -429,21 +460,22 @@
|
||||
});
|
||||
} else {
|
||||
// browser env
|
||||
global.UAParser = UAParser;
|
||||
window.UAParser = UAParser;
|
||||
// jQuery specific
|
||||
if (typeof global.jQuery !== UNDEF) {
|
||||
if (typeof window.jQuery !== UNDEF) {
|
||||
var parser = new UAParser();
|
||||
global.jQuery.ua = parser.getResult();
|
||||
global.jQuery.ua.get = function() {
|
||||
window.jQuery.ua = parser.getResult();
|
||||
window.jQuery.ua.get = function() {
|
||||
return parser.getUA();
|
||||
};
|
||||
global.jQuery.ua.set = function(uastring) {
|
||||
window.jQuery.ua.set = function(uastring) {
|
||||
parser.setUA(uastring);
|
||||
var result = parser.getResult();
|
||||
for (var prop in result) {
|
||||
global.jQuery.ua[prop] = result[prop];
|
||||
window.jQuery.ua[prop] = result[prop];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
})(this);
|
||||
|
7
src/ua-parser.min.js
vendored
Normal file
7
src/ua-parser.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -1,7 +1,7 @@
|
||||
{
|
||||
"title": "UA-Parser.JS",
|
||||
"name": "ua-parser-js",
|
||||
"version": "0.5.15",
|
||||
"version": "0.5.20",
|
||||
"description": "Lightweight JavaScript-based user-agent string parser",
|
||||
"keywords": [
|
||||
"user-agent",
|
||||
@ -27,5 +27,5 @@
|
||||
],
|
||||
"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"
|
||||
"download": "https://raw.github.com/faisalman/ua-parser-js/master/src/ua-parser.min.js"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user