2010-09-15 00:58:59 +00:00
|
|
|
/*
|
|
|
|
|
* User-agent detection
|
|
|
|
|
*/
|
2010-09-15 02:34:31 +00:00
|
|
|
jQuery.client = new ( function() {
|
|
|
|
|
|
|
|
|
|
/* Private Members */
|
|
|
|
|
|
|
|
|
|
var profile;
|
|
|
|
|
|
|
|
|
|
/* Public Functions */
|
|
|
|
|
|
2010-09-15 00:58:59 +00:00
|
|
|
/**
|
|
|
|
|
* Returns an object containing information about the browser
|
|
|
|
|
*
|
|
|
|
|
* The resulting client object will be in the following format:
|
|
|
|
|
* {
|
2010-09-20 19:20:55 +00:00
|
|
|
* 'name': 'firefox',
|
2010-09-15 00:58:59 +00:00
|
|
|
* 'layout': 'gecko',
|
2010-10-29 15:27:53 +00:00
|
|
|
* 'layoutVersion': '20101026',
|
2010-09-20 19:20:55 +00:00
|
|
|
* 'platform': 'linux'
|
2010-09-15 00:58:59 +00:00
|
|
|
* 'version': '3.5.1',
|
|
|
|
|
* 'versionBase': '3',
|
|
|
|
|
* 'versionNumber': 3.5,
|
|
|
|
|
* }
|
|
|
|
|
*/
|
2010-09-15 02:34:31 +00:00
|
|
|
this.profile = function() {
|
2010-09-15 00:58:59 +00:00
|
|
|
// Use the cached version if possible
|
2010-09-15 02:34:31 +00:00
|
|
|
if ( typeof profile === 'undefined' ) {
|
2010-09-15 00:58:59 +00:00
|
|
|
|
|
|
|
|
/* Configuration */
|
|
|
|
|
|
|
|
|
|
// Name of browsers or layout engines we don't recognize
|
|
|
|
|
var uk = 'unknown';
|
|
|
|
|
// Generic version digit
|
|
|
|
|
var x = 'x';
|
|
|
|
|
// Strings found in user agent strings that need to be conformed
|
|
|
|
|
var wildUserAgents = [ 'Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3'];
|
|
|
|
|
// Translations for conforming user agent strings
|
|
|
|
|
var userAgentTranslations = [
|
|
|
|
|
// Tons of browsers lie about being something they are not
|
|
|
|
|
[/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
|
|
|
|
|
// Chrome lives in the shadow of Safari still
|
|
|
|
|
['Chrome Safari', 'Chrome'],
|
|
|
|
|
// KHTML is the layout engine not the browser - LIES!
|
|
|
|
|
['KHTML', 'Konqueror'],
|
|
|
|
|
// Firefox nightly builds
|
|
|
|
|
['Minefield', 'Firefox'],
|
|
|
|
|
// This helps keep differnt versions consistent
|
|
|
|
|
['Navigator', 'Netscape'],
|
|
|
|
|
// This prevents version extraction issues, otherwise translation would happen later
|
|
|
|
|
['PLAYSTATION 3', 'PS3'],
|
|
|
|
|
];
|
|
|
|
|
// Strings which precede a version number in a user agent string - combined and used as match 1 in
|
|
|
|
|
// version detectection
|
|
|
|
|
var versionPrefixes = [
|
|
|
|
|
'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror', 'lynx',
|
|
|
|
|
'msie', 'safari', 'ps3'
|
|
|
|
|
];
|
|
|
|
|
// Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
|
2010-10-01 19:20:27 +00:00
|
|
|
var versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)';
|
2010-09-15 00:58:59 +00:00
|
|
|
// Names of known browsers
|
2010-09-20 19:20:55 +00:00
|
|
|
var names = [
|
2010-09-15 00:58:59 +00:00
|
|
|
'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera', 'safari', 'ipod',
|
|
|
|
|
'iphone', 'blackberry', 'ps3'
|
|
|
|
|
];
|
|
|
|
|
// Tanslations for conforming browser names
|
2010-09-20 19:20:55 +00:00
|
|
|
var nameTranslations = [];
|
2010-09-15 00:58:59 +00:00
|
|
|
// Names of known layout engines
|
2010-09-20 19:20:55 +00:00
|
|
|
var layouts = ['gecko', 'konqueror', 'msie', 'opera', 'webkit'];
|
2010-09-15 00:58:59 +00:00
|
|
|
// Translations for conforming layout names
|
|
|
|
|
var layoutTranslations = [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']];
|
2010-10-29 15:27:53 +00:00
|
|
|
// Names of supported layout engines for version number
|
|
|
|
|
var layoutVersions = ['applewebkit', 'gecko'];
|
2010-09-15 00:58:59 +00:00
|
|
|
// Names of known operating systems
|
2010-09-20 19:20:55 +00:00
|
|
|
var platforms = ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'];
|
2010-09-15 00:58:59 +00:00
|
|
|
// Translations for conforming operating system names
|
2010-09-20 19:20:55 +00:00
|
|
|
var platformTranslations = [['sunos', 'solaris']];
|
2010-09-15 00:58:59 +00:00
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
// Performs multiple replacements on a string
|
|
|
|
|
function translate( source, translations ) {
|
|
|
|
|
for ( var i = 0; i < translations.length; i++ ) {
|
|
|
|
|
source = source.replace( translations[i][0], translations[i][1] );
|
|
|
|
|
}
|
|
|
|
|
return source;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Pre-processing */
|
|
|
|
|
|
2010-10-29 15:27:53 +00:00
|
|
|
var userAgent = navigator.userAgent, match, name = uk, layout = uk, layoutversion = uk, platform = uk, version = x;
|
2010-09-15 00:58:59 +00:00
|
|
|
if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( userAgent ) ) {
|
|
|
|
|
// Takes a userAgent string and translates given text into something we can more easily work with
|
|
|
|
|
userAgent = translate( userAgent, userAgentTranslations );
|
|
|
|
|
}
|
|
|
|
|
// Everything will be in lowercase from now on
|
|
|
|
|
userAgent = userAgent.toLowerCase();
|
|
|
|
|
|
|
|
|
|
/* Extraction */
|
|
|
|
|
|
2010-09-20 19:20:55 +00:00
|
|
|
if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( userAgent ) ) {
|
|
|
|
|
name = translate( match[1], nameTranslations );
|
2010-09-15 00:58:59 +00:00
|
|
|
}
|
2010-09-20 19:20:55 +00:00
|
|
|
if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( userAgent ) ) {
|
2010-09-15 00:58:59 +00:00
|
|
|
layout = translate( match[1], layoutTranslations );
|
|
|
|
|
}
|
2010-10-29 15:27:53 +00:00
|
|
|
if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( navigator.userAgent.toLowerCase() ) ) {
|
|
|
|
|
layoutversion = parseInt(match[2]);
|
|
|
|
|
}
|
2010-09-20 19:20:55 +00:00
|
|
|
if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( navigator.platform.toLowerCase() ) ) {
|
|
|
|
|
platform = translate( match[1], platformTranslations );
|
2010-09-15 00:58:59 +00:00
|
|
|
}
|
|
|
|
|
if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( userAgent ) ) {
|
|
|
|
|
version = match[3];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Edge Cases -- did I mention about how user agent string lie? */
|
|
|
|
|
|
|
|
|
|
// Decode Safari's crazy 400+ version numbers
|
|
|
|
|
if ( name.match( /safari/ ) && version > 400 ) {
|
|
|
|
|
version = '2.0';
|
|
|
|
|
}
|
|
|
|
|
// Expose Opera 10's lies about being Opera 9.8
|
|
|
|
|
if ( name === 'opera' && version >= 9.8) {
|
|
|
|
|
version = userAgent.match( /version\/([0-9\.]*)/i )[1] || 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Caching */
|
|
|
|
|
|
2010-09-15 02:34:31 +00:00
|
|
|
profile = {
|
2010-09-20 19:20:55 +00:00
|
|
|
'name': name,
|
2010-09-15 00:58:59 +00:00
|
|
|
'layout': layout,
|
2010-10-29 15:27:53 +00:00
|
|
|
'layoutVersion': layoutversion,
|
2010-09-20 19:20:55 +00:00
|
|
|
'platform': platform,
|
2010-09-15 00:58:59 +00:00
|
|
|
'version': version,
|
|
|
|
|
'versionBase': ( version !== x ? new String( version ).substr( 0, 1 ) : x ),
|
|
|
|
|
'versionNumber': ( parseFloat( version, 10 ) || 0.0 )
|
|
|
|
|
};
|
|
|
|
|
}
|
2010-09-15 02:34:31 +00:00
|
|
|
return profile;
|
|
|
|
|
};
|
|
|
|
|
|
2010-09-15 00:58:59 +00:00
|
|
|
/**
|
|
|
|
|
* Checks the current browser against a support map object to determine if the browser has been black-listed or
|
|
|
|
|
* not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
|
|
|
|
|
* element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
|
|
|
|
|
*
|
|
|
|
|
* A browser map is in the following format:
|
|
|
|
|
* {
|
|
|
|
|
* 'ltr': {
|
|
|
|
|
* // Multiple rules with configurable operators
|
|
|
|
|
* 'msie': [['>=', 7], ['!=', 9]],
|
|
|
|
|
* // Blocked entirely
|
|
|
|
|
* 'iphone': false
|
|
|
|
|
* },
|
|
|
|
|
* 'rtl': {
|
|
|
|
|
* // Test against a string
|
|
|
|
|
* 'msie': [['!==', '8.1.2.3']],
|
|
|
|
|
* // RTL rules do not fall through to LTR rules, you must explicity set each of them
|
|
|
|
|
* 'iphone': false
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @param map Object of browser support map
|
|
|
|
|
*
|
|
|
|
|
* @return Boolean true if browser known or assumed to be supported, false if blacklisted
|
|
|
|
|
*/
|
2010-09-15 02:34:31 +00:00
|
|
|
this.test = function( map ) {
|
|
|
|
|
var profile = jQuery.client.profile();
|
|
|
|
|
var dir = jQuery( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
|
2010-09-15 00:58:59 +00:00
|
|
|
// Check over each browser condition to determine if we are running in a compatible client
|
2010-09-20 19:20:55 +00:00
|
|
|
if ( typeof map[dir] !== 'object' || map[dir][profile.name] !== 'object' ) {
|
2010-09-15 00:58:59 +00:00
|
|
|
// Unknown, so we assume it's working
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-09-20 19:20:55 +00:00
|
|
|
var name = map[dir][profile.name];
|
|
|
|
|
for ( var condition in name ) {
|
|
|
|
|
var op = name[condition][0];
|
|
|
|
|
var val = name[condition][1];
|
2010-09-15 00:58:59 +00:00
|
|
|
if ( val === false ) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if ( typeof val == 'string' ) {
|
2010-09-15 02:03:24 +00:00
|
|
|
if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
|
2010-09-15 00:58:59 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else if ( typeof val == 'number' ) {
|
2010-09-15 02:03:24 +00:00
|
|
|
if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
|
2010-09-15 00:58:59 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-09-15 02:34:31 +00:00
|
|
|
} )();
|