mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-11-22 10:06:06 +03:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5e4d686e3e | ||
|
|
f1065dc4f8 | ||
|
|
42c16b2fc9 |
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"title" : "UA-Parser.js",
|
||||
"name" : "ua-parser-js",
|
||||
"version" : "0.1.2",
|
||||
"version" : "0.2.1",
|
||||
"author" : {
|
||||
"name" : "Faisal Salman",
|
||||
"email" : "fyzlman@gmail.com",
|
||||
|
||||
11
readme.md
11
readme.md
@@ -2,10 +2,10 @@
|
||||
|
||||
JavaScript-based user-agent parser
|
||||
|
||||
* Author : Faisalman <<fyzlman@gmail.com>>
|
||||
* Author : Faisalman <<fyzlman@gmail.com>>
|
||||
* Home : http://faisalman.github.com/ua-parser-js
|
||||
* Source : https://github.com/faisalman/ua-parser-js
|
||||
* License : GPLv2
|
||||
* Source : https://github.com/faisalman/ua-parser-js
|
||||
* License : GPLv2
|
||||
|
||||
## Features
|
||||
|
||||
@@ -14,6 +14,7 @@ Get detailed type and version of web browser, layout engine, and operating syste
|
||||
## Methods
|
||||
|
||||
* `getBrowser([uastring])`
|
||||
* `getDevice([uastring])`
|
||||
* `getEngine([uastring])`
|
||||
* `getOS([uastring])`
|
||||
* `setUA(uastring)`
|
||||
@@ -35,6 +36,10 @@ Get detailed type and version of web browser, layout engine, and operating syste
|
||||
version: "",
|
||||
major: ""
|
||||
},
|
||||
device: {
|
||||
name: "",
|
||||
version: ""
|
||||
},
|
||||
engine: {
|
||||
name: "",
|
||||
version: ""
|
||||
|
||||
122
ua-parser.js
122
ua-parser.js
@@ -1,4 +1,4 @@
|
||||
// UA-Parser.js v0.1.2
|
||||
// UA-Parser.js v0.2.1
|
||||
// Lightweight JavaScript-based user-agent parser
|
||||
// https://github.com/faisalman/ua-parser-js
|
||||
//
|
||||
@@ -16,7 +16,7 @@ function UAParser (uastring) {
|
||||
for (i = 1; i < arguments.length; i += 2) {
|
||||
var regex = arguments[i];
|
||||
var props = arguments[i + 1];
|
||||
var isMatch = false;
|
||||
var isMatchFound = false;
|
||||
for (j = 0; j < regex.length; j++) {
|
||||
var match = regex[j].exec(ua);
|
||||
//console.log(match);
|
||||
@@ -28,50 +28,56 @@ function UAParser (uastring) {
|
||||
result[props[k][0]] = props[k][1];
|
||||
l -= 1;
|
||||
} else if (typeof props[k] === 'object' && props[k].length === 3) {
|
||||
result[props[k][0]] = (!!match[k + l]) ? match[k + l].replace(props[k][1], props[k][2]) : 'unknown';
|
||||
result[props[k][0]] = (!!match[k + l]) ? match[k + l].replace(props[k][1], props[k][2]) : undefined;
|
||||
} else {
|
||||
result[props[k]] = (!!match[k + l]) ? match[k + l] : 'unknown';
|
||||
result[props[k]] = (!!match[k + l]) ? match[k + l] : undefined;
|
||||
}
|
||||
};
|
||||
isMatch = true;
|
||||
}
|
||||
isMatchFound = true;
|
||||
break;
|
||||
}
|
||||
};
|
||||
if (!isMatch) {
|
||||
result = {};
|
||||
for (j in props) {
|
||||
if (props.hasOwnProperty(j)) {
|
||||
result[props[j]] = 'unknown';
|
||||
}
|
||||
};
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
};
|
||||
if (!isMatchFound) {
|
||||
result = {};
|
||||
for (k in props) {
|
||||
if (props.hasOwnProperty(k)) {
|
||||
if (typeof props[k] == 'object') {
|
||||
result[props[k][0]] = undefined;
|
||||
} else {
|
||||
result[props[k]] = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
var mapper = {
|
||||
win: function (str, match) {
|
||||
switch (match.toLowerCase()) {
|
||||
case 'nt 5.0':
|
||||
return '2000';
|
||||
case 'nt 5.1':
|
||||
case 'nt 5.2':
|
||||
return 'XP';
|
||||
case 'nt 6.0':
|
||||
return 'Vista';
|
||||
case 'nt 6.1':
|
||||
return '7';
|
||||
case 'nt 6.2':
|
||||
return '8';
|
||||
default:
|
||||
return match;
|
||||
};
|
||||
os : {
|
||||
win: function (str, match) {
|
||||
switch (match.toLowerCase()) {
|
||||
case 'nt 5.0':
|
||||
return '2000';
|
||||
case 'nt 5.1':
|
||||
case 'nt 5.2':
|
||||
return 'XP';
|
||||
case 'nt 6.0':
|
||||
return 'Vista';
|
||||
case 'nt 6.1':
|
||||
return '7';
|
||||
case 'nt 6.2':
|
||||
return '8';
|
||||
default:
|
||||
return match;
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.getBrowser = function(uastring) {
|
||||
this.getBrowser = function (uastring) {
|
||||
|
||||
return regxMap(uastring || ua, [
|
||||
|
||||
@@ -92,8 +98,8 @@ function UAParser (uastring) {
|
||||
/(chromium|flock|rockmelt|midori|epiphany)\/((\d+)?[\w\.]+)/i, // Chromium/Flock/RockMelt/Midori/Epiphany
|
||||
/(chrome|omniweb|arora|dolfin)\/((\d+)?[\w\.]+)/i, // Chrome/OmniWeb/Arora/Dolphin
|
||||
], ['name', 'version', 'major'], [
|
||||
/android.+(crmo)\/((\d+)?[\w\.]+)/i, // Chrome for Android
|
||||
], [['name', /.+/g, 'Chrome'], 'version', 'major'], [
|
||||
/android.+crmo\/((\d+)?[\w\.]+)/i, // Chrome for Android
|
||||
], [['name', 'Chrome'], 'version', 'major'], [
|
||||
/(mobile\ssafari|safari|konqueror)\/((\d+)?[\w\.]+)/i, // Safari/Konqueror
|
||||
/(applewebkit|khtml)\/((\d+)?[\w\.]+)/i,
|
||||
|
||||
@@ -108,7 +114,7 @@ function UAParser (uastring) {
|
||||
], ['name', 'version', 'major']);
|
||||
};
|
||||
|
||||
this.getEngine = function(uastring) {
|
||||
this.getEngine = function (uastring) {
|
||||
|
||||
return regxMap(uastring || ua, [
|
||||
|
||||
@@ -121,13 +127,13 @@ function UAParser (uastring) {
|
||||
], ['version', 'name']);
|
||||
};
|
||||
|
||||
this.getOS = function(uastring) {
|
||||
this.getOS = function (uastring) {
|
||||
|
||||
return regxMap(uastring || ua, [
|
||||
|
||||
// Windows based
|
||||
/(windows\sphone\sos|windows)\s+([\w\.\s]+)*/i, // Windows
|
||||
], ['name', ['version', /(nt\s[\d\.]+)/gi, mapper.win]], [
|
||||
], ['name', ['version', /(nt\s[\d\.]+)/gi, mapper.os.win]], [
|
||||
|
||||
// Mobile/Embedded OS
|
||||
/(blackberry).+version\/([\w\.]+)/i, // Blackberry
|
||||
@@ -165,12 +171,48 @@ function UAParser (uastring) {
|
||||
], ['name', 'version']);
|
||||
};
|
||||
|
||||
this.getDevice = function (uastring) {
|
||||
|
||||
return regxMap(uastring || ua, [
|
||||
|
||||
/\((ip[honead]+|playbook);/i, // iPod/iPhone/iPad/PlayBook
|
||||
/(blackberry)[\s-]?(\w+)/i, // BlackBerry
|
||||
/(blackberry|benq|nokia|palm(?=\-)|sonyericsson|acer|asus|dell|nexus|zte)[\s_-]?([\w-]+)*/i,
|
||||
// BenQ/Nokia/Palm/Sony-Ericsson/Acer/Asus/Dell/Nexus/ZTE
|
||||
/(hp)\s([\w\s]+)/i, // HP iPAQ
|
||||
/(hp).+(touchpad)/i, // HP TouchPad
|
||||
/(kindle)\/([\w\.]+)/i, // Kindle
|
||||
/(lg)[e;\s-]+(\w+)*/i, // LG
|
||||
/(nintendo|playstation)\s([wids3portable]+)/i // Nintendo/Playstation
|
||||
], ['name', 'version'], [
|
||||
|
||||
/(htc)[;_\s-]+([\w\s]+(?=\))|[\w]+)*/i, // HTC
|
||||
/(zte)-([\w]+)*/i
|
||||
], ['name', ['version', /_/g, ' ']], [
|
||||
|
||||
/\s(milestone|mz601|droid[2x]?|xoom)[globa\s]*\sbuild\//i, // Motorola
|
||||
/mot[\s-]?(\w+)*/i
|
||||
], [['name', 'Motorola'], 'version'], [
|
||||
|
||||
/(s[cgp]h-\w+|gt-\w+|galaxy\snexus)/i, // Samsung
|
||||
/sam[sung]*[\s-]*(\w+-?[\w-]*)*/i,
|
||||
/sec-(sgh\w+)/i
|
||||
], [['name', 'Samsung'], 'version'], [
|
||||
|
||||
/(transfo[prime\s]{4,10}\s\w+|(?:android).*eeepc)/i // Asus
|
||||
], [['name', 'Asus'], 'version'], [
|
||||
|
||||
/sie-(\w+)*/i // Siemens
|
||||
], [['name', 'Siemens'], 'version']);
|
||||
};
|
||||
|
||||
this.setUA = function (uastring) {
|
||||
ua = uastring || ua;
|
||||
return this.result = {
|
||||
browser : this.getBrowser(),
|
||||
engine : this.getEngine(),
|
||||
os : this.getOS()
|
||||
os : this.getOS(),
|
||||
device : this.getDevice()
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user