/// <summary> /// Initializes a new instance of the <see cref="Browser"/> class. /// </summary> /// <param name="ua">The value of an HTTP request User-Agent header from the browser to be identified.</param> public Browser(string ua) { Func <Regex, string> getFirstMatch = regex => { var match = regex.Match(ua); if (match != null && match.Groups.Count > 1) { return(match.Groups[1].Value); } return(string.Empty); }; Func <Regex, string> getSecondMatch = regex => { var match = regex.Match(ua); if (match != null && match.Groups.Count > 2) { return(match.Groups[2].Value); } return(string.Empty); }; var iosDevice = getFirstMatch(iOSDeviceRegex); var likeAndroid = LikeAndroidRegex.IsMatch(ua); var android = !likeAndroid && AndroidRegex.IsMatch(ua); var nexusMobile = NexusMobileRegex.IsMatch(ua); var nexusTablet = !nexusMobile && NexusTabletRegex.IsMatch(ua); var chromeOS = ChromeOSRegex.IsMatch(ua); var silk = SilkRegex.IsMatch(ua); var sailfish = SailfishRegex.IsMatch(ua); var tizen = TizenRegex.IsMatch(ua); var webos = WebOSRegex.IsMatch(ua); var windowsPhone = WindowsPhoneRegex.IsMatch(ua); var samsungBrowser = SamsungBrowserRegex.IsMatch(ua); var windows = !windowsPhone && WindowsRegex.IsMatch(ua); var mac = !iosDevice.IsTruthy() && !silk && MacRegex.IsMatch(ua); var linux = !android && !sailfish && !tizen && !webos && LinuxRegex.IsMatch(ua); var edgeVersion = getFirstMatch(EdgeVersionRegex); var versionIdentifier = getFirstMatch(VersionIdentifierRegex); var tablet = TabletRegex.IsMatch(ua); var mobile = !tablet && MobileRegex.IsMatch(ua); var xbox = XboxRegex.IsMatch(ua); if (OldOperaRegex.IsMatch(ua)) { Name = "Opera"; Opera = true; Version = versionIdentifier.NullIfFalsy() ?? getFirstMatch(OldOperaVersionRegex); } else if (NewOperaRegex.IsMatch(ua)) { Name = "Opera"; Opera = true; Version = getFirstMatch(NewOperaVersionRegex).NullIfFalsy() ?? versionIdentifier; } else if (SamsungBrowserRegex.IsMatch(ua)) { Name = "Samsung Internet for Android"; SamsungBrowser = true; Version = versionIdentifier.NullIfFalsy() ?? getFirstMatch(SamsungBrowserVersionRegex); } else if (CoastRegex.IsMatch(ua)) { Name = "Opera Coast"; Coast = true; Version = versionIdentifier.NullIfFalsy() ?? getFirstMatch(CoastVersionRegex); } else if (YandexBrowserRegex.IsMatch(ua)) { Name = "Yandex Browser"; YandexBrowser = true; Version = versionIdentifier.NullIfFalsy() ?? getFirstMatch(YandexBrowserVersionRegex); } else if (UcBrowserRegex.IsMatch(ua)) { Name = "UC Browser"; UcBrowser = true; Version = getFirstMatch(UcBrowserVersionRegex); } else if (MaxthonRegex.IsMatch(ua)) { Name = "Maxthon"; Maxthon = true; Version = getFirstMatch(MaxthonVersionRegex); } else if (EpiphanyRegex.IsMatch(ua)) { Name = "Epiphany"; Epiphany = true; Version = getFirstMatch(EpiphanyVersionRegex); } else if (PuffinRegex.IsMatch(ua)) { Name = "Puffin"; Puffin = true; Version = getFirstMatch(PuffinVersionRegex); } else if (SleipnirRegex.IsMatch(ua)) { Name = "Sleipnir"; Sleipnir = true; Version = getFirstMatch(SleipnirVersionRegex); } else if (KMeleonRegex.IsMatch(ua)) { Name = "K-Meleon"; KMeleon = true; Version = getFirstMatch(KMeleonVersionRegex); } else if (windowsPhone) { Name = "Windows Phone"; WindowsPhone = true; if (edgeVersion.IsTruthy()) { MsEdge = true; Version = edgeVersion; } else { MsIE = true; Version = getFirstMatch(IEMobileVersionRegex); } } else if (InternetExplorerRegex.IsMatch(ua)) { Name = "Internet Explorer"; MsIE = true; Version = getFirstMatch(InternetExplorerVersionRegex); } else if (chromeOS) { Name = "Chrome"; ChromeOS = true; ChromeBook = true; Chrome = true; Version = getFirstMatch(ChromeOSVersionRegex); } else if (EdgeRegex.IsMatch(ua)) { Name = "Microsoft Edge"; MsEdge = true; Version = edgeVersion; } else if (VivaldiRegex.IsMatch(ua)) { Name = "Vivaldi"; Vivaldi = true; Version = getFirstMatch(VivaldiVersionRegex).NullIfFalsy() ?? versionIdentifier; } else if (sailfish) { Name = "Sailfish"; Sailfish = true; Version = getFirstMatch(SailfishVersionRegex); } else if (SeaMonkeyRegex.IsMatch(ua)) { Name = "SeaMonkey"; SeaMonkey = true; Version = getFirstMatch(SeaMonkeyVersionRegex); } else if (FirefoxRegex.IsMatch(ua)) { Name = "Firefox"; Firefox = true; Version = getFirstMatch(FirefoxVersionRegex); if (FirefoxOSRegex.IsMatch(ua)) { FirefoxOS = true; } } else if (silk) { Name = "Amazon Silk"; Silk = true; Version = getFirstMatch(SilkVersionRegex); } else if (PhantomJsRegex.IsMatch(ua)) { Name = "PhantomJS"; Phantom = true; Version = getFirstMatch(PhantomJsVersionRegex); } else if (SlimerJsRegex.IsMatch(ua)) { Name = "SlimerJS"; Slimer = true; Version = getFirstMatch(SlimerJsVersionRegex); } else if (BlackberryRegex.IsMatch(ua) || RimRegex.IsMatch(ua)) { Name = "Blackberry"; Blackberry = true; Version = versionIdentifier.NullIfFalsy() ?? getFirstMatch(BlackberryVersionRegex); } else if (webos) { Name = "WebOS"; WebOS = true; Version = versionIdentifier.NullIfFalsy() ?? getFirstMatch(WebOSVersionRegex); if (TouchPadRegex.IsMatch(ua)) { TouchPad = true; } } else if (BadaRegex.IsMatch(ua)) { Name = "Bada"; Bada = true; Version = getFirstMatch(BadaVersionRegex); } else if (tizen) { Name = "Tizen"; Tizen = true; Version = getFirstMatch(TizenVersionRegex).NullIfFalsy() ?? versionIdentifier; } else if (QupZillaRegex.IsMatch(ua)) { Name = "QupZilla"; QupZilla = true; Version = getFirstMatch(QupZillaVersionRegex).NullIfFalsy() ?? versionIdentifier; } else if (ChromiumRegex.IsMatch(ua)) { Name = "Chromium"; Chromium = true; Version = getFirstMatch(ChromiumVersionRegex).NullIfFalsy() ?? versionIdentifier; } else if (ChromeRegex.IsMatch(ua)) { Name = "Chrome"; Chrome = true; Version = getFirstMatch(ChromeVersionRegex); } else if (android) { Name = "Android"; Version = versionIdentifier; } else if (SafariRegex.IsMatch(ua)) { Name = "Safari"; Safari = true; if (versionIdentifier.IsTruthy()) { Version = versionIdentifier; } } else if (iosDevice.IsTruthy()) { Name = iosDevice.Equals("iphone", StringComparison.OrdinalIgnoreCase) ? "iPhone" : iosDevice.Equals("ipad", StringComparison.OrdinalIgnoreCase) ? "iPad" : "iPod"; if (versionIdentifier.IsTruthy()) { Version = versionIdentifier; } } else if (GooglebotRegex.IsMatch(ua)) { Name = "Googlebot"; Googlebot = true; Version = getFirstMatch(GooglebotVersionRegex).NullIfFalsy() ?? versionIdentifier; } else { Name = getFirstMatch(UnknownRegex); Version = getSecondMatch(UnknownRegex); } if (!MsEdge && WebkitRegex.IsMatch(ua)) { if (BlinkRegex.IsMatch(ua)) { Name = Name.NullIfFalsy() ?? "Blink"; Blink = true; } else { Name = Name.NullIfFalsy() ?? "Webkit"; Webkit = true; } if (!Version.IsTruthy() && versionIdentifier.IsTruthy()) { Version = versionIdentifier; } } else if (!Opera && GeckoRegex.IsMatch(ua)) { Name = Name.NullIfFalsy() ?? "Gecko"; Gecko = true; Version = Version.NullIfFalsy() ?? getFirstMatch(GeckoVersionRegex); } if (!WindowsPhone && !MsEdge && (android || Silk)) { Android = true; } else if (!WindowsPhone && !MsEdge && iosDevice.IsTruthy()) { if (iosDevice.Equals("iphone", StringComparison.OrdinalIgnoreCase)) { iPhone = true; } else if (iosDevice.Equals("ipad", StringComparison.OrdinalIgnoreCase)) { iPad = true; } else { iPod = true; } iOS = true; } else if (mac) { Mac = true; } else if (xbox) { Xbox = true; } else if (windows) { Windows = true; } else if (linux) { Linux = true; } var osVersion = string.Empty; if (WindowsPhone) { osVersion = getFirstMatch(WindowsPhoneOSVersionRegex); } else if (iosDevice.IsTruthy()) { osVersion = getFirstMatch(iOSOSVersionRegex); osVersion = iOSOSVersionReplaceRegex.Replace(osVersion, "."); } else if (android) { osVersion = getFirstMatch(AndroidOSVersionRegex); } else if (WebOS) { osVersion = getFirstMatch(WebOSOSVersionRegex); } else if (Blackberry) { osVersion = getFirstMatch(BlackberryOSVersionRegex); } else if (Bada) { osVersion = getFirstMatch(BadaOSVersionRegex); } else if (Tizen) { osVersion = getFirstMatch(TizenOSVersionRegex); } if (osVersion.IsTruthy()) { OSVersion = osVersion; } var osMajorVersion = osVersion.Split('.')[0].CoerceToInteger(); if (tablet || nexusTablet || iosDevice.Equals("ipad", StringComparison.OrdinalIgnoreCase) || (android && (osMajorVersion == 3 || (osMajorVersion >= 4 && !mobile))) || Silk) { Tablet = true; } else if (mobile || iosDevice.Equals("iphone", StringComparison.OrdinalIgnoreCase) || iosDevice.Equals("ipod", StringComparison.OrdinalIgnoreCase) || android || nexusMobile || Blackberry || WebOS || Bada) { Mobile = true; } var versionFloat = Version.CoerceToFloat(); if (MsEdge || (MsIE && versionFloat >= 10) || (YandexBrowser && versionFloat >= 15) || (Vivaldi && versionFloat >= 1.0) || (Chrome && versionFloat >= 20) || (SamsungBrowser && versionFloat >= 4) || (Firefox && versionFloat >= 20) || (Safari && versionFloat >= 6) || (Opera && versionFloat >= 10.0) || (iOS && OSVersion.IsTruthy() && int.Parse(OSVersion.Split('.')[0]) >= 6) || (Blackberry && versionFloat >= 10.1) || (Chromium && versionFloat >= 20)) { A = true; } else if ((MsIE && versionFloat < 10) || (Chrome && versionFloat < 20) || (Firefox && versionFloat < 20.0) || (Safari && versionFloat < 6) || (Opera && versionFloat < 10.0) || (iOS && OSVersion.IsTruthy() && int.Parse(OSVersion.Split('.')[0]) < 6) || (Chromium && versionFloat < 20)) { C = true; } else { X = true; } }