Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ua.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type UserAgent struct {
Tablet bool
Desktop bool
Bot bool
TV bool
}

// Constants for browsers and operating systems for easier comparison
Expand Down Expand Up @@ -54,6 +55,7 @@ const (
Mozilla = "Mozilla"
Msie = "MSIE"
SamsungBrowser = "Samsung Browser"
LgBrowser = "LG Browser"

GoogleAdsBot = "Google Ads Bot"
Googlebot = "Googlebot"
Expand Down Expand Up @@ -84,6 +86,10 @@ func Parse(userAgent string) UserAgent {
tokens := parse([]byte(userAgent))
ua.URL = tokens.url

// TV check
ua.TV = tokens.existsAny("SmartTV", "Smart TV", "SMART-TV", "Apple TV", "GoogleTV",
"PhilipsTV", "HbbTV")

// OS lookup
switch {
case tokens.exists(Android):
Expand Down Expand Up @@ -120,7 +126,7 @@ func Parse(userAgent string) UserAgent {
ua.OSVersion = tokens.findMacOSVersion()
ua.Desktop = true

case tokens.exists(Linux):
case tokens.existsAny(Linux, strings.ToUpper(Linux)):
ua.OS = Linux
ua.OSVersion = tokens.get(Linux)
ua.Desktop = true
Expand Down Expand Up @@ -161,7 +167,7 @@ func Parse(userAgent string) UserAgent {

case tokens.exists("Bytespider"):
ua.Name = "Bytespider"
ua.Mobile = tokens.exists("Mobile Safari")
ua.Mobile = tokens.exists(MobileSafari)
ua.Bot = true

case tokens.exists(Applebot):
Expand Down Expand Up @@ -261,6 +267,13 @@ func Parse(userAgent string) UserAgent {
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
ua.OS = Android

case tokens.get(LgBrowser) != "":
ua.Name = LgBrowser
ua.Version = tokens.get(LgBrowser)
if !ua.TV && len(ua.OSVersion) > 0 {
ua.TV = strings.Contains(strings.ToLower(ua.OSVersion), "smarttv")
}

case tokens.get("HeadlessChrome") != "":
ua.Name = HeadlessChrome
ua.Version = tokens.get("HeadlessChrome")
Expand Down
11 changes: 11 additions & 0 deletions ua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ var testTable = [][]string{
//Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5 (Applebot/0.1; +http://www.apple.com/go/applebot)
//Mozilla/5.0 (Macintosh; Intel Mac OS Xt 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.6.0 Chrome/45.0.2454.101 Safari/537.36

{"Mozilla/5.0 (SmartHub; SMART-TV; U; Linux/SmartTV; Maple2012) AppleWebKit/534.7 (KHTML, like Gecko) SmartTV Safari/534.7", "SmartTV Safari", "534.7", "tv", ua.Linux},
{"Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Large Screen Safari/533.4 GoogleTV/ 162671", ua.Chrome, "5.0.375.127", "tv", ua.Linux},
{"Mozilla/5.0 (Linux mips; U;HbbTV/1.1.1 (+RTSP;DMM;Dreambox;0.1a;1.0;) CE-HTML/1.0; en) AppleWebKit/535.19 no/Volksbox QtWebkit/2.2", "HbbTV", "1.1.1", "tv", ua.Linux},
{"Mozilla/5.0 (Linux; NetCast; U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 SmartTV/10.0 Colt/2.0", "SmartTV", "10.0", "tv", ua.Linux},
{"Mozilla/5.0 (SMART-TV; LINUX; Tizen 5.0) AppleWebKit/537.36 (KHTML, like Gecko) Version/5.0 TV Safari/537.36", "TV Safari", "537.36", "tv", ua.Linux},
{"Mozilla/5.0 (SMART-TV; Linux; Tizen 4.0) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/2.1 Chrome/56.0.2924.0 TV Safari/537.36", ua.SamsungBrowser, "2.1", "tv", ua.Android},
{"Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.41 (KHTML, like Gecko) Large Screen Safari/537.41 LG Browser/7.00.00(LGE; WEBOS1; 05.06.10; 1); webOS.TV-2014; LG NetCast.TV-2013 Compatible (LGE, WEBOS1, wireless)", ua.LgBrowser, "7.00.00", "tv", ua.Linux},
}

func TestParse(t *testing.T) {
Expand Down Expand Up @@ -204,6 +211,10 @@ func TestParse(t *testing.T) {
t.Error("\n", ua.String, "should be bot")
fmt.Printf("%+v", ua)
}
if test[3] == "tv" && !ua.TV {
t.Error("\n", ua.String, "should be tv")
fmt.Printf("%+v", ua)
}
}

if len(test) > 4 && test[4] != ua.OS {
Expand Down