Created
June 3, 2014 20:45
-
-
Save elgalu/82929eeeef4a80e01835 to your computer and use it in GitHub Desktop.
Easily ask for current browser capability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
browser.getCapabilities().then(function(s) { | |
var browserName, browserVersion; | |
var shortName, shortVersion; | |
var ie, ff, ch, sa; | |
var platform; | |
platform = s.caps_.platform; | |
browserName = s.caps_.browserName; | |
browserVersion = s.caps_.version; | |
shortVersion = browserVersion.split('.')[0]; | |
ie = /i.*explore/.test(browserName); | |
ff = /firefox/.test(browserName); | |
ch = /chrome/.test(browserName); | |
sa = /safari/.test(browserName); | |
if (ie) { | |
shortName = 'ie'; | |
} else if (ff) { | |
shortName = 'ff'; | |
} else if (ch) { | |
shortName = 'ch'; | |
} else if (sa) { | |
shortName = 'sa'; | |
} else { | |
throw new Exception('Unsupported browser: '+browserName); | |
}; | |
// Returns one of these: ['ch', 'ff', 'sa', 'ie'] | |
browser.getShortBrowserName = function() { | |
return shortName; | |
}; | |
// Returns one of these: ['ch33', 'ff27', 'sa7', 'ie11', 'ie10', 'ie9'] | |
browser.getShortNameVersionAll = function() { | |
return shortName + shortVersion; | |
}; | |
// Returns one of these: ['ch', 'ff', 'sa', 'ie11', 'ie10', 'ie9'] | |
browser.getShortNameVersion = function() { | |
if (ie) { | |
return shortName + shortVersion; | |
} else { | |
return shortName; | |
}; | |
}; | |
// Return if current browser is IE, optionally specifying if it is a particular IE version | |
browser.isInternetExplorer = function(ver) { | |
if (ver == null) { | |
return ie; | |
} else { | |
return ie && ver.toString() === shortVersion; | |
} | |
}; | |
browser.isSafari = function() { | |
return sa; | |
}; | |
browser.isFirefox = function() { | |
return ff; | |
}; | |
// Return if current browser is Chrome, optionally specifying if it is a particular Chrome version | |
browser.isChrome = function(ver) { | |
if (ver == null) { | |
return ch; | |
} else { | |
return ch && ver.toString() === shortVersion; | |
} | |
}; | |
browser.inWindows = function() { | |
return /^WIN|XP/.test(platform); | |
}; | |
browser.inOSX = function() { | |
return /^MAC/.test(platform); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment