mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-11-20 17:16:40 +03:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc20c12b63 | ||
|
|
ecf8f7a849 | ||
|
|
3bf60cc830 | ||
|
|
8c94018351 | ||
|
|
9098ad501f | ||
|
|
376541a383 | ||
|
|
9cad6a0f2d | ||
|
|
84a8eedad4 | ||
|
|
30e1e7a98f | ||
|
|
c1df106dca | ||
|
|
edcd5402a6 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ua-parser-js",
|
"name": "ua-parser-js",
|
||||||
"version": "0.5.25",
|
"version": "0.6.0",
|
||||||
"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": ["src/ua-parser.js"],
|
"scripts": ["src/ua-parser.js"],
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"title": "UAParser.js",
|
"title": "UAParser.js",
|
||||||
"name": "ua-parser-js",
|
"name": "ua-parser-js",
|
||||||
"version": "0.5.25",
|
"version": "0.6.0",
|
||||||
"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": [
|
||||||
|
|||||||
37
readme.md
37
readme.md
@@ -73,8 +73,16 @@ Windows [Phone/Mobile], Zenwalk
|
|||||||
# 'os.version' determined dynamically
|
# 'os.version' determined dynamically
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* `getCPU()`
|
||||||
|
* returns `{ architecture: '' }`
|
||||||
|
|
||||||
|
```
|
||||||
|
# Possible 'cpu.architecture'
|
||||||
|
68k, amd64, arm, ia32, ia64, irix, mips, pa-risc, ppc, sparc
|
||||||
|
```
|
||||||
|
|
||||||
* `getResult()`
|
* `getResult()`
|
||||||
* returns `{ browser: {}, device: {}, engine: {}, os: {} }`
|
* returns `{ browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
|
||||||
* `getUA()`
|
* `getUA()`
|
||||||
* returns UA string of current instance
|
* returns UA string of current instance
|
||||||
* `setUA(uastring)`
|
* `setUA(uastring)`
|
||||||
@@ -113,6 +121,9 @@ Windows [Phone/Mobile], Zenwalk
|
|||||||
model: "",
|
model: "",
|
||||||
type: "",
|
type: "",
|
||||||
vendor: ""
|
vendor: ""
|
||||||
|
},
|
||||||
|
cpu: {
|
||||||
|
architecture: ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
@@ -130,6 +141,7 @@ Windows [Phone/Mobile], Zenwalk
|
|||||||
console.log(result.os); // {name: "Ubuntu", version: "11.10"}
|
console.log(result.os); // {name: "Ubuntu", version: "11.10"}
|
||||||
console.log(result.os.version); // "11.10"
|
console.log(result.os.version); // "11.10"
|
||||||
console.log(result.engine.name); // "WebKit"
|
console.log(result.engine.name); // "WebKit"
|
||||||
|
console.log(result.cpu.architecture); // "amd64"
|
||||||
|
|
||||||
// do some other tests
|
// do some other tests
|
||||||
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
|
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
|
||||||
@@ -149,15 +161,6 @@ Windows [Phone/Mobile], Zenwalk
|
|||||||
</html>
|
</html>
|
||||||
```
|
```
|
||||||
|
|
||||||
### Using requirejs
|
|
||||||
|
|
||||||
```js
|
|
||||||
require(['ua-parser'], function(UAParser) {
|
|
||||||
var parser = new UAParser();
|
|
||||||
console.log(parser.getResult());
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using node.js
|
### Using node.js
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -167,7 +170,17 @@ $ npm install ua-parser-js
|
|||||||
```js
|
```js
|
||||||
var UAParser = require('ua-parser-js');
|
var UAParser = require('ua-parser-js');
|
||||||
var parser = new UAParser();
|
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
|
### Using component
|
||||||
@@ -190,7 +203,7 @@ $ bower install ua-parser-js
|
|||||||
|
|
||||||
### Using jQuery.ua
|
### 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
|
```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':
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// UAParser.js v0.5.25
|
// UAParser.js v0.6.0
|
||||||
// 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
|
||||||
//
|
//
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
TYPE = 'type',
|
TYPE = 'type',
|
||||||
VENDOR = 'vendor',
|
VENDOR = 'vendor',
|
||||||
VERSION = 'version',
|
VERSION = 'version',
|
||||||
|
ARCHITECTURE= 'architecture',
|
||||||
CONSOLE = 'console',
|
CONSOLE = 'console',
|
||||||
MOBILE = 'mobile',
|
MOBILE = 'mobile',
|
||||||
TABLET = 'tablet';
|
TABLET = 'tablet';
|
||||||
@@ -38,6 +39,9 @@
|
|||||||
var util = {
|
var util = {
|
||||||
has : function (str1, str2) {
|
has : function (str1, str2) {
|
||||||
return str2.toLowerCase().indexOf(str1.toLowerCase()) !== -1;
|
return str2.toLowerCase().indexOf(str1.toLowerCase()) !== -1;
|
||||||
|
},
|
||||||
|
lowerize : function (str) {
|
||||||
|
return str.toLowerCase();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -78,10 +82,15 @@
|
|||||||
match = matches[++k];
|
match = matches[++k];
|
||||||
q = props[p];
|
q = props[p];
|
||||||
// check if given property is actually array
|
// check if given property is actually array
|
||||||
if (typeof(q) === OBJ_TYPE) {
|
if (typeof(q) === OBJ_TYPE && q.length > 0) {
|
||||||
if (q.length == 2) {
|
if (q.length == 2) {
|
||||||
// assign given value, ignore regex match
|
if (typeof(q[1]) == FUNC_TYPE) {
|
||||||
result[q[0]] = q[1];
|
// 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) {
|
} else if (q.length == 3) {
|
||||||
// check whether function or regex
|
// check whether function or regex
|
||||||
if (typeof(q[1]) === FUNC_TYPE && !(q[1].exec && q[1].test)) {
|
if (typeof(q[1]) === FUNC_TYPE && !(q[1].exec && q[1].test)) {
|
||||||
@@ -91,6 +100,8 @@
|
|||||||
// sanitize match using given regex
|
// sanitize match using given regex
|
||||||
result[q[0]] = match ? match.replace(q[1], q[2]) : undefined;
|
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 {
|
} else {
|
||||||
result[q] = match ? match : undefined;
|
result[q] = match ? match : undefined;
|
||||||
@@ -209,7 +220,7 @@
|
|||||||
// Trident based
|
// Trident based
|
||||||
/(avant\s|iemobile|slim|baidu)(?:browser)?[\/\s]?((\d+)?[\w\.]*)/i,
|
/(avant\s|iemobile|slim|baidu)(?:browser)?[\/\s]?((\d+)?[\w\.]*)/i,
|
||||||
// Avant/IEMobile/SlimBrowser/Baidu
|
// Avant/IEMobile/SlimBrowser/Baidu
|
||||||
/ms(ie)\s((\d+)?[\w\.]+)/i, // Internet Explorer
|
/(?:ms|\()(ie)\s((\d+)?[\w\.]+)/i, // Internet Explorer
|
||||||
|
|
||||||
// Webkit/KHTML based
|
// Webkit/KHTML based
|
||||||
/(rekonq)((?:\/)[\w\.]+)*/i, // Rekonq
|
/(rekonq)((?:\/)[\w\.]+)*/i, // Rekonq
|
||||||
@@ -266,6 +277,25 @@
|
|||||||
], [NAME, VERSION, MAJOR]
|
], [NAME, VERSION, MAJOR]
|
||||||
],
|
],
|
||||||
|
|
||||||
|
cpu : [[
|
||||||
|
|
||||||
|
/(?:(amd|x(?:(?:86|64)[_-])?|wow|win)64)[;\)]/i // AMD64
|
||||||
|
], [[ARCHITECTURE, 'amd64']], [
|
||||||
|
|
||||||
|
/((?:i[346]|x)86)[;\)]/i // IA32
|
||||||
|
], [[ARCHITECTURE, 'ia32']], [
|
||||||
|
|
||||||
|
/((?: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 : [[
|
device : [[
|
||||||
|
|
||||||
/\((ipad|playbook);[\w\s\);-]+(rim|apple)/i // iPad/PlayBook
|
/\((ipad|playbook);[\w\s\);-]+(rim|apple)/i // iPad/PlayBook
|
||||||
@@ -306,7 +336,7 @@
|
|||||||
|
|
||||||
/(htc)[;_\s-]+([\w\s]+(?=\))|\w+)*/i, // HTC
|
/(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|(?=;\s)sony)[_\s-]?([\w-]+)*/i
|
||||||
// Alcatel/GeeksPhone/Huawei/Lenovo/Nexian/Panasonic/Sony
|
// Alcatel/GeeksPhone/Huawei/Lenovo/Nexian/Panasonic/Sony
|
||||||
], [VENDOR, [MODEL, /_/g, ' '], [TYPE, MOBILE]], [
|
], [VENDOR, [MODEL, /_/g, ' '], [TYPE, MOBILE]], [
|
||||||
|
|
||||||
@@ -431,6 +461,9 @@
|
|||||||
this.getBrowser = function () {
|
this.getBrowser = function () {
|
||||||
return mapper.rgx.apply(this, regexes.browser);
|
return mapper.rgx.apply(this, regexes.browser);
|
||||||
};
|
};
|
||||||
|
this.getCPU = function () {
|
||||||
|
return mapper.rgx.apply(this, regexes.cpu);
|
||||||
|
};
|
||||||
this.getDevice = function () {
|
this.getDevice = function () {
|
||||||
return mapper.rgx.apply(this, regexes.device);
|
return mapper.rgx.apply(this, regexes.device);
|
||||||
};
|
};
|
||||||
@@ -445,10 +478,11 @@
|
|||||||
browser : this.getBrowser(),
|
browser : this.getBrowser(),
|
||||||
engine : this.getEngine(),
|
engine : this.getEngine(),
|
||||||
os : this.getOS(),
|
os : this.getOS(),
|
||||||
device : this.getDevice()
|
device : this.getDevice(),
|
||||||
|
cpu : this.getCPU()
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
this.getUA = function() {
|
this.getUA = function () {
|
||||||
return ua;
|
return ua;
|
||||||
};
|
};
|
||||||
this.setUA = function (uastring) {
|
this.setUA = function (uastring) {
|
||||||
@@ -471,14 +505,15 @@
|
|||||||
exports = module.exports = UAParser;
|
exports = module.exports = UAParser;
|
||||||
}
|
}
|
||||||
exports.UAParser = UAParser;
|
exports.UAParser = UAParser;
|
||||||
} else if (typeof(define) === FUNC_TYPE && define.amd) {
|
|
||||||
// requirejs env
|
|
||||||
define(function() {
|
|
||||||
return UAParser;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// browser env
|
// browser env
|
||||||
window.UAParser = UAParser;
|
window.UAParser = UAParser;
|
||||||
|
// requirejs env (optional)
|
||||||
|
if (typeof(define) === FUNC_TYPE && define.amd) {
|
||||||
|
define(function () {
|
||||||
|
return UAParser;
|
||||||
|
});
|
||||||
|
}
|
||||||
// jQuery specific (optional)
|
// jQuery specific (optional)
|
||||||
if (typeof(window.jQuery) !== UNDEF_TYPE) {
|
if (typeof(window.jQuery) !== UNDEF_TYPE) {
|
||||||
var $ = window.jQuery;
|
var $ = window.jQuery;
|
||||||
@@ -487,7 +522,7 @@
|
|||||||
$.ua.get = function() {
|
$.ua.get = function() {
|
||||||
return parser.getUA();
|
return parser.getUA();
|
||||||
};
|
};
|
||||||
$.ua.set = function(uastring) {
|
$.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) {
|
||||||
|
|||||||
4
src/ua-parser.min.js
vendored
4
src/ua-parser.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -239,6 +239,16 @@
|
|||||||
"major" : "7"
|
"major" : "7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"desc" : "IE 11",
|
||||||
|
"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" : "K-Meleon",
|
"desc" : "K-Meleon",
|
||||||
"ua" : "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.5) Gecko/20031016 K-Meleon/0.8.2",
|
"ua" : "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.5) Gecko/20031016 K-Meleon/0.8.2",
|
||||||
|
|||||||
81
test/cpu-test.json
Normal file
81
test/cpu-test.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"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" : "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"
|
||||||
|
}
|
||||||
|
}]
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var UAParser = require('./../src/ua-parser');
|
var UAParser = require('./../src/ua-parser');
|
||||||
var browsers = require('./browser-test.json');
|
var browsers = require('./browser-test.json');
|
||||||
|
var cpus = require('./cpu-test.json');
|
||||||
var devices = require('./device-test.json');
|
var devices = require('./device-test.json');
|
||||||
var engines = require('./engine-test.json');
|
var engines = require('./engine-test.json');
|
||||||
var os = require('./os-test.json');
|
var os = require('./os-test.json');
|
||||||
@@ -12,6 +13,12 @@ var methods = [
|
|||||||
list : browsers,
|
list : browsers,
|
||||||
properties : ['name', 'major', 'version']
|
properties : ['name', 'major', 'version']
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title : 'getCPU',
|
||||||
|
label : 'cpu',
|
||||||
|
list : cpus,
|
||||||
|
properties : ['architecture']
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title : 'getDevice',
|
title : 'getDevice',
|
||||||
label : 'device',
|
label : 'device',
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"title": "UAParser.js",
|
"title": "UAParser.js",
|
||||||
"name": "ua-parser-js",
|
"name": "ua-parser-js",
|
||||||
"version": "0.5.25",
|
"version": "0.6.0",
|
||||||
"description": "Lightweight JavaScript-based user-agent string parser",
|
"description": "Lightweight JavaScript-based user-agent string parser",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"user-agent",
|
"user-agent",
|
||||||
|
|||||||
Reference in New Issue
Block a user