update Ox.parseUserAgent

This commit is contained in:
rlx 2012-03-21 06:54:02 +00:00
parent f8dcca9fe4
commit 61f9496f34

View file

@ -246,65 +246,136 @@ Ox.parseSRT = function(str, fps) {
}); });
}; };
Ox.parseUserAgent = function(str) { Ox.parseUserAgent = function(userAgent) {
var names = { var aliases = {
chromeframe: 'Internet Explorer (Chrome Frame)', browser: {
'iPhone OS': 'iOS', 'Firefox': /(Fennec|Firebird|Iceweasel|Minefield|Namoroka|Phoenix|SeaMonkey|Shiretoko)/
Fennec: 'Mobile Firefox', },
Mobile: 'Mobile Safari', system: {
MSIE: 'Internet Explorer', 'BSD': /(FreeBSD|NetBSD|OpenBSD)/,
'Linux': /(CrOS|MeeGo|webOS)/,
'Unix': /(AIX|HP-UX|IRIX|SunOS)/
}
},
names = {
browser: {
'chromeframe': 'Chrome Frame',
'MSIE': 'Internet Explorer'
},
system: {
'iPhone OS': 'iOS',
'Macintosh': 'Mac OS X'
}
}, },
regexps = { regexps = {
browser: [ browser: [
/(Camino)\/(\d+)/,
/(chromeframe)\/(\d+)/, /(chromeframe)\/(\d+)/,
/(Chrome)\/(\d+)/, /(Chrome)\/(\d+)/,
/(Fennec)\/(\d+)/, /(Epiphany)\/(\d+)/,
/(Firefox)\/(\d+)/, /(Firefox)\/(\d+)/,
/(MSIE)\/(\d+)/, /(Galeon)\/(\d+)/,
/(Googlebot)\/(\d+)/,
/(Konqueror)\/(\d+)/,
/(MSIE) (\d+)/,
/(Netscape)\d?\/(\d+)/,
/(NokiaBrowser)\/(\d+)/,
/(Opera) (\d+)/,
/(Opera)\/.+Version\/(\d+)/, /(Opera)\/.+Version\/(\d+)/,
/Version\/(\d+).+(Mobile)\/.+Safari/,
/Version\/(\d+).+(Safari)/ /Version\/(\d+).+(Safari)/
], ],
system: [ system: [
/(Android) (\d+)/,
/(BeOS)/,
/(BlackBerry) (\d+)/,
/(Darwin)/,
/(BSD) (FreeBSD|NetBSD|OpenBSD)/,
/(iPhone OS) (\d+)/, /(iPhone OS) (\d+)/,
/ (Linux) /, /(Linux).+(CentOS|CrOS|Debian|Fedora|Gentoo|Mandriva|MeeGo|Mint|Red Hat|SUSE|Ubuntu|webOS)/,
/(Linux)/,
/(Mac OS X) (10.\d)/, /(Mac OS X) (10.\d)/,
/(Windows) (NT \d\.\d)/ /(Mac OS X)/,
/(Macintosh)/,
/(SymbianOS)\/(\d+)/,
/(SymbOS)/,
/(OS\/2)/,
/(Unix) (AIX|HP-UX|IRIX|SunOS)/,
/(Unix)/,
/(Windows) (NT \d\.\d)/,
/(Windows) (95|98|2000|2003|ME|NT|XP)/, // Opera
/(Windows).+(Win 9x 4\.90)/, // Firefox
/(Windows).+(Win9\d)/, // Firefox
/(Windows).+(WinNT4.0)/ // Firefox
] ]
}, },
userAgent = {
browser: {name: '', version: ''},
system: {name: '', version: ''}
},
versions = { versions = {
'10.3': 'Panther', browser: {},
'10.4': 'Tiger', system: {
'10.5': 'Leopard', '10.2': '10.2 (Jaguar)',
'10.6': 'Snow Leopard', '10.3': '10.3 (Panther)',
'10.7': 'Lion', '10.4': '10.4 (Tiger)',
'NT 5.0': '2000', '10.5': '10.5 (Leopard)',
'NT 5.1': 'XP', '10.6': '10.6 (Snow Leopard)',
'NT 5.2': '2003', '10.7': '10.7 (Lion)',
'NT 6.0': 'Vista', 'CrOS': 'Chrome OS',
'NT 6.1': '7' 'NT 4.0': 'NT 4.0 (Windows NT)',
} 'NT 4.1': 'NT 4.1 (Windows 98)',
'Win 9x 4.90': 'NT 4.9 (Windows ME)',
'NT 5.0': 'NT 5.0 (Windows 2000)',
'NT 5.1': 'NT 5.1 (Windows XP)',
'NT 5.2': 'NT 5.2 (Windows 2003)',
'NT 6.0': 'NT 6.0 (Windows Vista)',
'NT 6.1': 'NT 6.1 (Windows 7)',
'NT 6.2': 'NT 6.2 (Windows 8)',
'95': 'NT 4.0 (Windows 95)',
'NT': 'NT 4.0 (Windows NT)',
'98': 'NT 4.1 (Windows 98)',
'ME': 'NT 4.9 (Windows ME)',
'2000': 'NT 5.0 (Windows 2000)',
'2003': 'NT 5.2 (Windows 2003)',
'XP': 'NT 5.1 (Windows XP)',
'Win95': 'NT 4.0 (Windows 95)',
'WinNT4.0': 'NT 4.0 (Windows NT)',
'Win98': 'NT 4.1 (Windows 98)'
}
},
userAgentData = {};
Ox.forEach(regexps, function(regexps, key) { Ox.forEach(regexps, function(regexps, key) {
regexps.forEach(function(regexp) { userAgentData[key] = {name: '', string: '', version: ''};
var matches = str.match(regexp), name, swap, version; Ox.forEach(aliases[key], function(regexp, alias) {
userAgent = userAgent.replace(
regexp, key == 'browser' ? alias : alias + ' $1'
);
});
Ox.forEach(regexps, function(regexp) {
var matches = userAgent.match(regexp),
name, string, swap, version;
if (matches) { if (matches) {
matches[2] = matches[2] || ''; matches[2] = matches[2] || '';
swap = matches[1].match(/^\d+$/); swap = matches[1].match(/^\d+$/);
name = matches[swap ? 2 : 1]; name = matches[swap ? 2 : 1];
version = matches[swap ? 1 : 2].replace('_', '.'); version = matches[swap ? 1 : 2].replace('_', '.');
userAgent[key] = { name = names[key][name] || name,
version = versions[key][version] || version;
string = name + (
version
? ' ' + (
['BSD', 'Linux', 'Unix'].indexOf(name) > -1
? '(' + version + ')'
: version
)
: ''
);
userAgentData[key] = {
name: names[name] || name, name: names[name] || name,
string: string,
version: versions[version] || version version: versions[version] || version
}; };
return false; return false;
} }
}); });
}); });
return userAgent; return userAgentData;
}; };
/*@ /*@