Documentation ¶
Index ¶
- Constants
- type UserAgent
- func (ua UserAgent) IsAndroid() bool
- func (ua UserAgent) IsChrome() bool
- func (ua UserAgent) IsChromeOS() bool
- func (ua UserAgent) IsEdge() bool
- func (ua UserAgent) IsFacebookbot() bool
- func (ua UserAgent) IsFirefox() bool
- func (ua UserAgent) IsGooglebot() bool
- func (ua UserAgent) IsIOS() bool
- func (ua UserAgent) IsInternetExplorer() bool
- func (ua UserAgent) IsLinux() bool
- func (ua UserAgent) IsMacOS() bool
- func (ua UserAgent) IsOpera() bool
- func (ua UserAgent) IsOperaMini() bool
- func (ua UserAgent) IsSafari() bool
- func (ua UserAgent) IsTwitterbot() bool
- func (ua UserAgent) IsUnknown() bool
- func (ua UserAgent) IsWindows() bool
- func (ua UserAgent) OSVersionNoFull() string
- func (ua UserAgent) OSVersionNoShort() string
- func (ua UserAgent) VersionNoFull() string
- func (ua UserAgent) VersionNoShort() string
- type VersionNo
Examples ¶
Constants ¶
const ( Windows = "Windows" WindowsPhone = "Windows Phone" Android = "Android" MacOS = "macOS" IOS = "iOS" Linux = "Linux" FreeBSD = "FreeBSD" ChromeOS = "ChromeOS" BlackBerry = "BlackBerry" Opera = "Opera" OperaMini = "Opera Mini" OperaTouch = "Opera Touch" Chrome = "Chrome" HeadlessChrome = "Headless Chrome" Firefox = "Firefox" InternetExplorer = "Internet Explorer" Safari = "Safari" Edge = "Edge" Vivaldi = "Vivaldi" Konqueror = "Konqueror" Epiphany = "Epiphany" GoogleAdsBot = "Google Ads Bot" Googlebot = "Googlebot" Twitterbot = "Twitterbot" FacebookExternalHit = "facebookexternalhit" Applebot = "Applebot" Bingbot = "Bingbot" FacebookApp = "Facebook App" InstagramApp = "Instagram App" TiktokApp = "TikTok App" )
Constants for browsers and operating systems for easier comparison
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type UserAgent ¶
type UserAgent struct { VersionNo VersionNo OSVersionNo VersionNo URL string String string Name string Version string OS string OSVersion string Device string Mobile bool Tablet bool Desktop bool Bot bool }
UserAgent struct containing all data extracted from parsed user-agent string
func Parse ¶
Parse user agent string returning UserAgent struct
Example ¶
package main import ( "fmt" "strings" ua "github.com/pingooio/stdx/useragent" ) func main() { userAgents := []string{ // Mac "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 OPR/46.0.2597.57", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.91 Safari/537.36 Vivaldi/1.92.917.39", // Windows "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2; GWX:RED)", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) NS8/0.9.6", // iPhone "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) CriOS/60.0.3112.89 Mobile/14F89 Safari/602.1", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) OPiOS/14.0.0.104835 Mobile/13E233 Safari/9537.53", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) FxiOS/8.1.1b4948 Mobile/14F89 Safari/603.2.4", // iPad "Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1", "Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/58.0.3029.113 Mobile/14F89 Safari/602.1", "Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) FxiOS/8.1.1b4948 Mobile/14F89 Safari/603.2.4", // Andorid "Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36", "Mozilla/5.0 (Android 4.3; Mobile; rv:54.0) Gecko/54.0 Firefox/54.0", "Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36 OPR/42.9.2246.119956", "Opera/9.80 (Android; Opera Mini/28.0.2254/66.318; U; en) Presto/2.12.423 Version/12.16", "Mozilla/5.0 (Linux; U; Android 4.3; en-us; GT-I9300 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", "Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG SM-A310F/A310FXXU2BQB1 Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/5.4 Chrome/51.0.2704.106 Mobile Safari/537.36", } for _, s := range userAgents { ua := ua.Parse(s) fmt.Println() fmt.Println(ua.String) fmt.Println(strings.Repeat("=", len(ua.String))) fmt.Println("Name:", ua.Name, "v", ua.Version) fmt.Println("OS:", ua.OS, "v", ua.OSVersion) fmt.Println("Device:", ua.Device) if ua.Mobile { fmt.Println("(Mobile)") } if ua.Tablet { fmt.Println("(Tablet)") } if ua.Desktop { fmt.Println("(Desktop)") } if ua.Bot { fmt.Println("(Bot)") } if ua.URL != "" { fmt.Println(ua.URL) } } }
Output:
func (UserAgent) IsChromeOS ¶
IsChromeOS shorthand function to check if OS == CrOS
func (UserAgent) IsFacebookbot ¶
IsFacebookbot shorthand function to check if Name == FacebookExternalHit
func (UserAgent) IsGooglebot ¶
IsGooglebot shorthand function to check if Name == Googlebot
func (UserAgent) IsInternetExplorer ¶
IsInternetExplorer shorthand function to check if Name == Internet Explorer
func (UserAgent) IsOperaMini ¶
IsOperaMini shorthand function to check if Name == Opera Mini
func (UserAgent) IsTwitterbot ¶
IsTwitterbot shorthand function to check if Name == Twitterbot
func (UserAgent) IsUnknown ¶
IsUnknown returns true if the package can't determine the user agent reliably. Fields like Name, OS, etc. might still have values.
func (UserAgent) OSVersionNoFull ¶
OSVersionNoFull returns OS version string in format <Major>.<Minor>.<Patch>
func (UserAgent) OSVersionNoShort ¶
OSVersionNoShort returns OS version string in format <Major>.<Minor>
func (UserAgent) VersionNoFull ¶
VersionNoFull returns version string in format <Major>.<Minor>.<Patch>
func (UserAgent) VersionNoShort ¶
VersionNoShort return version string in format <Major>.<Minor>