-
-
Save atebitftw/8944552ac92538ea164b to your computer and use it in GitHub Desktop.
A helper class to parse the user agent string for basic info.
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
/** | |
* ## Parse the user agent and retrieves information about the browser. ## | |
* This is a singleton class, so multiple "new Browser()" calls | |
* will always return the same object. | |
* | |
* ## Will attempt to provide the following: ## | |
* * Browser Type (Chrome, Firefox, ...) | |
* * Platform (Mobile, Desktop, Tablet) | |
* * Mobile Type (iPhone, Android, ...) | |
* * Major Version | |
*/ | |
class Browser | |
{ | |
//Browser Type | |
static final String DARTIUM = "Dartium"; | |
static final String CHROME = "Chrome"; | |
static final String FIREFOX = "Firefox"; | |
static final String IE = "IE"; | |
static final String OPERA = "Opera"; | |
static final String ANDROID = "Android"; | |
static final String SAFARI = "Safari"; | |
static final String LYNX = "Lynx"; | |
//Platform Type | |
static final String MOBILE = "Mobile"; | |
static final String DESKTOP = "Desktop"; | |
static final String TABLET = "Tablet"; | |
//Mobile Type | |
// ANDROID is used again for Android | |
static final String IPHONE = "iPhone"; | |
static final String IPAD = "iPad"; | |
static final String WINDOWSPHONE = "Windows Phone"; | |
static final String UNKNOWN = "Unknown"; | |
final ua; | |
num version; | |
String browser; | |
String platform; | |
String mobileType; | |
static Browser _ref; | |
factory Browser(){ | |
if (_ref != null) return _ref; | |
_ref = new Browser._internal(); | |
return _ref; | |
} | |
Browser._internal() | |
: | |
ua = window.navigator.userAgent | |
{ | |
browser = _setBrowserType(); | |
platform = _setPlatform(); | |
mobileType = _setMobileType(); | |
version = _getVersion(); | |
} | |
num _getVersion(){ | |
num getMajor(String ver){ | |
if (ver.contains('.')){ | |
return Math.parseInt(ver.substring(0, ver.indexOf('.'))); | |
}else{ | |
return Math.parseInt(ver); | |
} | |
} | |
switch(browser){ | |
case DARTIUM: | |
case CHROME: | |
final s = ua.indexOf('Chrome/') + 7; | |
final e = ua.indexOf(' ', s); | |
return getMajor(ua.substring(s, e)); | |
case ANDROID: | |
final s = ua.indexOf('Android ') + 8; | |
final e = ua.indexOf(' ', s); | |
return getMajor(ua.substring(s, e)); | |
case FIREFOX: | |
final s = ua.indexOf('Firefox/') + 8; | |
final e = ua.indexOf(' ', s); | |
return getMajor(ua.substring(s, e)); | |
} | |
return 0; | |
} | |
String _setMobileType(){ | |
if (platform == UNKNOWN || platform == DESKTOP){ | |
return UNKNOWN; | |
} | |
switch(browser){ | |
case CHROME: | |
case ANDROID: | |
return ANDROID; | |
case SAFARI: | |
if (ua.contains('iPhone') || ua.contains('iPod')){ | |
return IPHONE; | |
} | |
if (ua.contains('iPad')){ | |
return IPAD; | |
} | |
return UNKNOWN; | |
case IE: | |
//TODO: "Surface" tablet | |
return WINDOWSPHONE; | |
case OPERA: | |
if (ua.contains('Android')){ | |
return ANDROID; | |
} | |
if (ua.contains('iPhone')){ | |
return IPHONE; | |
} | |
if (ua.contains('iPad')){ | |
return IPAD; | |
} | |
if (ua.contains('Windows Mobile')){ | |
return WINDOWSPHONE; | |
} | |
return DESKTOP; | |
} | |
return UNKNOWN; | |
} | |
String _setPlatform(){ | |
if (browser == UNKNOWN) return UNKNOWN; | |
switch(browser){ | |
case DARTIUM: | |
return DESKTOP; | |
case ANDROID: | |
return MOBILE; | |
case CHROME: | |
if (ua.contains('<Android Version>')){ | |
return (ua.contains('Mobile') ? MOBILE : TABLET); | |
} | |
return DESKTOP; | |
case SAFARI: | |
if (ua.contains('iPhone') || ua.contains('iPod')){ | |
return MOBILE; | |
} | |
if (ua.contains('iPad')){ | |
return TABLET; | |
} | |
return DESKTOP; | |
case IE: | |
if (ua.contains('Windows Phone')){ | |
return MOBILE; | |
} | |
//TODO: need UA for "Surface" tablet eventually | |
return DESKTOP; | |
case OPERA: | |
if (ua.contains('Opera Tablet')){ | |
return TABLET; | |
} | |
if (ua.contains('Mini') || ua.contains('Mobile')){ | |
return MOBILE; | |
} | |
return DESKTOP; | |
} | |
return UNKNOWN; | |
} | |
String _setBrowserType(){ | |
//source: http://www.zytrax.com/tech/web/browser_ids.htm | |
if (ua.contains('(Dart)')) return DARTIUM; | |
if (ua.contains('Chrome/')) return CHROME; | |
if (ua.contains('Firefox/') || ua.contains('ThunderBrowse')) return FIREFOX; | |
if (ua.contains('MSIE')) return IE; | |
if (ua.contains('Opera')) return OPERA; | |
if (ua.contains('Android')) return ANDROID; | |
if (ua.contains('Safari')) return SAFARI; | |
if (ua.contains('Lynx')) return LYNX; | |
return UNKNOWN; | |
} | |
String toString() => "Browser Info (Type: ${browser}, Version: ${version}, Platform: ${platform}, MobileType: ${mobileType})"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment