browser

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 12 Imported by: 0

README

browser MIT Go Reference Go report card Build Status Coverage X

Why?

Why not?

You need to detect the browser, device and platform from the user agent string in many use cases. I have personally come across the following use cases:

  1. You want to render different HTML for different browsers, devices or platforms.
  2. You want to render OG tags for scraping bots and social media sites.
  3. You want to log the browser, device or platform for your analytics.
  4. You want your backend to behave differently when a Google bot crawls your site for SEO.

I wanted a relatively extensible package that I could use in all the above use cases. So, I decided to write this package.

Inspiration

The ruby gem fnando/browser inspires this package. I have used the gem in some of my previous projects and liked it. All the credit goes to the author of the ruby gem, who has done a great job.

Design

I have kept the design as similar as possible to the gem but made changes where I felt necessary per the Go.

The following are the sub-packages:

  • matchers: This package defines all the browser matchers.
  • devices: This package defines all the device matchers.
  • platforms: This package defines all the platform matchers.
  • bots: This package defines all the bots matchers.

A Matcher interface defines a matching behaviour for a user agent string.

type Matchers interface {
    Match() bool    // Match returns true if the user agent string matches the matcher.
    Name() string   // Name returns the name of the matcher.
}
Matchers

BrowserMatcher interface matches the user agent string with the browser. Implement the BrowserMatcher interface to add a new browser.

type BrowserMatcher interface {
    Matcher
    Version() string // Version returns the full version of the browser.
}
Devices

DeviceMatcher interface matches the user agent string with the device. Implement the DeviceMatcher interface to add a new device.

type DeviceMatcher interface {
    Matcher
}
Platforms

PlatformMatcher interface matches the user agent string with the platform. Implement the PlatformMatcher interface to add a new device.

type PlatformMatcher interface {
    Matcher
    Version() string // Version returns the version of the platform.
}
Bots

BotMatcher interface matches the user agent string with the bot. Implement the BotMatcher interface to add a new bot.

type BotMatcher interface {
    Matcher
}
Browser Struct

Browser struct abstracts a lot of functionality. It uses the BrowserMatcher, DeviceMatcher, PlatformMatcher and BotMatcher interfaces to match the user agent string with the browser, device, platform and bot respectively. All the matchers are executed in the order they are defined in the Browser struct. The first matcher that returns true will be used.

A ton of helper functions are defined in the Browser struct to make it easy to use.

Usage

go get github.com/dineshgowda24/browser
Browser Detection
b, err := browser.NewBrowser("Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36")
 if err != nil {
  // handle error
 }

 // browser level information
 fmt.Println(b.Name())           // Chrome
 fmt.Println(b.Version())        // 90.0.4430.210
 fmt.Println(b.ShortVersion())   // 90
 fmt.Println(b.IsBrowserKnown()) // true
 fmt.Println(b.IsChrome())       // true

 // device level information
 fmt.Println(b.Device().Name())      // Samsung SM-A205U
 fmt.Println(b.Device().IsTablet())  // false
 fmt.Println(b.Device().IsSamsung()) // true

 // platform level information
 fmt.Println(b.Platform().Name())         // Android
 fmt.Println(b.Platform().Version())      // 10
 fmt.Println(b.Platform().IsAndroidApp()) // false

 // bot level information
 fmt.Println(b.Bot().Name())  // ""
 fmt.Println(b.Bot().IsBot()) // false
Bot Detection
b, err := browser.NewBrowser("APIs-Google (https://developers.google.com/webmasters/APIs-Google.html)")
if err != nil {
   // handle error
}

// browser level information
fmt.Println(b.Name())           // Unknown Browser
fmt.Println(b.Version())        // 0.0
fmt.Println(b.ShortVersion())   // 0
fmt.Println(b.IsBrowserKnown()) // false
fmt.Println(b.IsUnknown())      // true

// bot level information
fmt.Println(b.Bot().Name())  // "APIs-Google"
fmt.Println(b.Bot().IsBot()) // true
fmt.Println(b.Bot().Why())   // *bots.Known

Contributing

If you want to contribute to this project, please read the CONTRIBUTING.md file.

Issues

If you find any issues with this package, please raise an issue. I will fix it as soon as possible. Please read the Contributing section if you want to resolve the issue and contribute.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoUserAgent           = fmt.Errorf("no user agent provided")
	ErrUserAgentSizeExceeded = fmt.Errorf("user agent size exceeds limit of %d", userAgentSizeLimit)
)

Functions

This section is empty.

Types

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

Bot is a struct that contains information about the user agent's bot

func NewBot

func NewBot(userAgent string) (*Bot, error)

NewBot returns a new Bot It will return an error if the bot exceptions file cannot be read It will return an error if the bot exceptions file cannot be unmarshalled

func (*Bot) IsBot

func (b *Bot) IsBot() bool

IsBot returns true if the user agent is a bot

func (*Bot) Name

func (b *Bot) Name() string

Name returns the name of the bot

func (*Bot) Why

func (b *Bot) Why() string

Why returns the reason why the user agent is a bot It will return an empty string if the user agent is not a bot It will return the type of the bot matcher detected

type BotMatcher

type BotMatcher interface {
	Matcher
}

BotMatcher is the interface for bot matchers

type Browser

type Browser struct {
	// contains filtered or unexported fields
}

Browser is a struct that contains information about the user agent's browser. It contains information about the browser's name, version, platform and device.

func NewBrowser

func NewBrowser(userAgent string) (*Browser, error)

NewBrowser creates a new browser based on the user agent string. It builds the browser by matching the user agent string with the known browsers.

UserAgentSizeError is returned if the user agent string is larger than the limit.

func (*Browser) Bot

func (b *Browser) Bot() *Bot

Bot returns the bot of the browser.

func (*Browser) Device

func (b *Browser) Device() *Device

Device returns the device of the browser. It can used to further query information about the device.

func (*Browser) IsAlipay

func (b *Browser) IsAlipay() bool

IsAliPay returns true if the browser is AliPay.

https://www.alipay.com/

func (*Browser) IsBlackBerry

func (b *Browser) IsBlackBerry() bool

IsBlackBerry returns true if the browser is BlackBerry.

https://www.blackberry.com/

func (*Browser) IsBrowserKnown

func (b *Browser) IsBrowserKnown() bool

IsBrowserKnown returns true if a match was found for the browser.

func (*Browser) IsChrome

func (b *Browser) IsChrome() bool

IsChrome returns true if the browser is Chrome.

https://www.google.com/chrome/

func (*Browser) IsDuckDuckGo

func (b *Browser) IsDuckDuckGo() bool

IsDuckDuckGo returns true if the browser is DuckDuckGo.

https://duckduckgo.com/

func (*Browser) IsEdge

func (b *Browser) IsEdge() bool

IsEdge returns true if the browser is Edge.

func (*Browser) IsElectron

func (b *Browser) IsElectron() bool

IsElectron returns true if the browser is Electron.

https://www.electronjs.org/

func (*Browser) IsGoogleSearchApp

func (b *Browser) IsGoogleSearchApp() bool

IsGoogleSearchApp returns true if the browser is GoogleSearchApp.

https://www.google.com/search/about/

func (*Browser) IsHuaweiBrowser

func (b *Browser) IsHuaweiBrowser() bool

IsHuaweiBrowser returns true if the browser is HuaweiBrowser.

https://consumer.huawei.com/en/mobileservices/browser/

func (*Browser) IsInstagram

func (b *Browser) IsInstagram() bool

IsInstagram returns true if the browser is Instagram.

https://www.instagram.com/

func (*Browser) IsInternetExplorer

func (b *Browser) IsInternetExplorer() bool

IsInternetExplorer returns true if the browser is Internet Explorer.

func (*Browser) IsKonqueror

func (b *Browser) IsKonqueror() bool

IsKonqueror returns true if the browser is Konqueror.

https://konqueror.org/

func (*Browser) IsMaxthon

func (b *Browser) IsMaxthon() bool

IsMaxthon returns true if the browser is Maxthon.

https://www.maxthon.com/

func (*Browser) IsMicroMessenger

func (b *Browser) IsMicroMessenger() bool

IsMicroMessenger returns true if the browser is MicroMessenger.

func (*Browser) IsMiuiBrowser

func (b *Browser) IsMiuiBrowser() bool

IsMiuiBrowser returns true if the browser is MiuiBrowser.

func (*Browser) IsNokia

func (b *Browser) IsNokia() bool

IsNokia returns true if the browser is Nokia.

https://www.nokia.com/

func (*Browser) IsOpera

func (b *Browser) IsOpera() bool

IsOpera returns true if the browser is Opera.

https://www.opera.com/

func (*Browser) IsOtter

func (b *Browser) IsOtter() bool

IsOtter returns true if the browser is Otter.

https://otter-browser.org/

func (*Browser) IsPaleMoon

func (b *Browser) IsPaleMoon() bool

IsPaleMoon returns true if the browser is PaleMoon.

https://www.palemoon.org/

func (*Browser) IsPuffin

func (b *Browser) IsPuffin() bool

IsPuffin returns true if the browser is Puffin.

https://www.puffin.com/

func (*Browser) IsQQ

func (b *Browser) IsQQ() bool

IsQQ returns true if the browser is QQ.

https://browser.qq.com/

func (*Browser) IsSafari

func (b *Browser) IsSafari() bool

IsSafari returns true if the browser is Safari.

https://www.apple.com/safari/

func (*Browser) IsSafariWebappMode

func (b *Browser) IsSafariWebappMode() bool

IsSafariWebappMode returns true if the browser is Safari Webapp Mode. It is true for iOS devices which have AppleWebKit in the user agent string.

func (*Browser) IsSamsungBrowser

func (b *Browser) IsSamsungBrowser() bool

IsSamsungBrowser returns true if the browser is SamsungBrowser.

func (*Browser) IsSnapchat

func (b *Browser) IsSnapchat() bool

IsSnapchat returns true if the browser is Snapchat.

https://www.snapchat.com/

func (*Browser) IsSougouBrowser

func (b *Browser) IsSougouBrowser() bool

IsSougouBrowser returns true if the browser is SougouBrowser.

https://www.sogou.com/

func (*Browser) IsSputnik

func (b *Browser) IsSputnik() bool

IsSputnik returns true if the browser is Sputnik.

func (*Browser) IsUCBrowser

func (b *Browser) IsUCBrowser() bool

IsUCBrowser returns true if the browser is UCBrowser.

https://www.ucweb.com/

func (*Browser) IsUnknown

func (b *Browser) IsUnknown() bool

IsUnknown returns true if the browser is Unknown. Browsers are considered unknown when they do not match any of the known browsers.

func (*Browser) IsVivoBrowser

func (b *Browser) IsVivoBrowser() bool

IsVivoBrowser returns true if the browser is VivoBrowser.

https://www.vivo.com/

func (*Browser) IsWechat

func (b *Browser) IsWechat() bool

IsWechat returns true if the browser is Wechat. Wechat is an alias for MicroMessenger.

func (*Browser) IsWeibo

func (b *Browser) IsWeibo() bool

IsWeibo returns true if the browser is Weibo.

https://weibo.com/

func (*Browser) IsYaaniBrowser

func (b *Browser) IsYaaniBrowser() bool

IsYaaniBrowser returns true if the browser is YaaniBrowser.

func (*Browser) IsYandex

func (b *Browser) IsYandex() bool

IsYandex returns true if the browser is Yandex.

https://yandex.com/

func (*Browser) Name

func (b *Browser) Name() string

Name returns the name of the matched browser.

func (*Browser) Platform

func (b *Browser) Platform() *Platform

Platform returns the platform of the browser. It can used to further query information about the platform.

func (*Browser) ShortVersion

func (b *Browser) ShortVersion() string

ShortVersion returns the short version of the version of the matched browser. Below are some examples of how the short version is returned:

12.1.1 => 12

04.34 => 04

func (*Browser) Version

func (b *Browser) Version() string

Version returns the full version of the matched browser. Examples: - 12.1.1 - 04.34

type BrowserMatcher

type BrowserMatcher interface {
	Matcher          // BrowserMatcher implements Matcher interface.
	Version() string // Version returns the full version of the browser.
}

BrowserMatcher is an interface for browser matchers.

type Device

type Device struct {
	// contains filtered or unexported fields
}

Device is a struct that contains information about the user agent's device

func NewDevice

func NewDevice(userAgent string) (*Device, error)

NewDevice creates a new device based on the user agent string

func (*Device) IsBlackberryPlaybook

func (d *Device) IsBlackberryPlaybook() bool

IsBlackberryPlaybook returns true if the device is a Blackberry Playbook

func (*Device) IsConsole

func (d *Device) IsConsole() bool

IsConsole returns true if the device is a console This includes PlayStation, Xbox, and Nintendo devices

func (*Device) IsIPad

func (d *Device) IsIPad() bool

IsIPad returns true if the device is an iPad

func (*Device) IsIPhone

func (d *Device) IsIPhone() bool

IsIPhone returns true if the device is an iPhone

func (*Device) IsIPodTouch

func (d *Device) IsIPodTouch() bool

IsIPodTouch returns true if the device is an iPod touch

func (*Device) IsKindle

func (d *Device) IsKindle() bool

IsKindle returns true if the device is a Kindle This includes Kindle Fire

func (*Device) IsKindleFire

func (d *Device) IsKindleFire() bool

IsKindleFire returns true if the device is a Kindle Fire

func (*Device) IsMobile

func (d *Device) IsMobile() bool

IsMobile returns true if the device is a mobile device

func (*Device) IsNintendo

func (d *Device) IsNintendo() bool

IsNintendo returns true if the device is a Nintendo This includes Wii, Wii U, and Switch

func (*Device) IsNintendoSwitch

func (d *Device) IsNintendoSwitch() bool

IsNintendoSwitch returns true if the device is a Nintendo Switch IsNintendoSwitch is an alias for IsSwitch

func (*Device) IsNintendoWii

func (d *Device) IsNintendoWii() bool

IsNintendoWii returns true if the device is a Nintendo Wii IsNintendoWii is an alias for IsWii

func (*Device) IsNintendoWiiU

func (d *Device) IsNintendoWiiU() bool

IsNintendoWiiU returns true if the device is a Nintendo Wii U IsNintendoWiiU is an alias for IsWiiU

func (*Device) IsPSP

func (d *Device) IsPSP() bool

IsPSP returns true if the device is a PlayStation Portable

func (*Device) IsPlayStation

func (d *Device) IsPlayStation() bool

IsPlayStation returns true if the device is a PlayStation This includes PlayStation 3, PlayStation 4, and PlayStation 5

func (*Device) IsPs3

func (d *Device) IsPs3() bool

IsPs3 returns true if the device is a PlayStation 3

func (*Device) IsPs4

func (d *Device) IsPs4() bool

IsPs4 returns true if the device is a PlayStation 4

func (*Device) IsPs5

func (d *Device) IsPs5() bool

IsPs5 returns true if the device is a PlayStation 5

func (*Device) IsSamsung

func (d *Device) IsSamsung() bool

IsSamsung returns true if the device is a Samsung device

func (*Device) IsSurface

func (d *Device) IsSurface() bool

IsSurface returns true if the device is a Surface

func (*Device) IsSwitch

func (d *Device) IsSwitch() bool

IsSwitch returns true if the device is a Switch

func (*Device) IsTV

func (d *Device) IsTV() bool

IsTV returns true if the device is a TV

func (*Device) IsTablet

func (d *Device) IsTablet() bool

IsTablet returns true if the device is a tablet This includes iPad, PlayBook, Surface, and Android based tablets

func (*Device) IsWii

func (d *Device) IsWii() bool

IsWii returns true if the device is a Wii

func (*Device) IsWiiU

func (d *Device) IsWiiU() bool

IsWiiU returns true if the device is a Wii U

func (*Device) IsXbox

func (d *Device) IsXbox() bool

IsXbox returns true if the device is an Xbox This includes Xbox 360 and Xbox One

func (*Device) IsXbox360

func (d *Device) IsXbox360() bool

IsXbox360 returns true if the device is an Xbox 360

func (*Device) IsXboxOne

func (d *Device) IsXboxOne() bool

IsXboxOne returns true if the device is an Xbox One

func (*Device) Name

func (d *Device) Name() string

Name returns the name of the device

type DeviceMatcher

type DeviceMatcher interface {
	Matcher
}

DeviceMatcher is an interface for device matchers

type Matcher

type Matcher interface {
	Match() bool
	Name() string
}

Matcher is an interface for user agent matchers. A matcher is used to detect a match from the user agent string.

type Platform

type Platform struct {
	// contains filtered or unexported fields
}

Platform is a struct that contains information about the user agent's platforms.

func NewPlatform

func NewPlatform(userAgent string) (*Platform, error)

NewPlatform creates a new platform based on the user agent string.

UserAgentSizeError is returned if the user agent string is larger than the limit.

func (*Platform) IsAdobeAir

func (p *Platform) IsAdobeAir() bool

IsAdobeAir returns true if the user agent string matches Adobe AIR.

func (*Platform) IsAdobeAirVersionCompatible

func (p *Platform) IsAdobeAirVersionCompatible(version string) bool

func (*Platform) IsAndroid

func (p *Platform) IsAndroid() bool

IsAndroid returns true if the user agent string matches Android.

func (*Platform) IsAndroidApp

func (p *Platform) IsAndroidApp() bool

IsAndroidApp returns true if the platform is Android app and the user agent string contains wv. https://developer.chrome.com/docs/multidevice/user-agent/#webview_user_agent

func (*Platform) IsAndroidWebview

func (p *Platform) IsAndroidWebview() bool

IsAndroidWebview is an alias for IsAndroidApp.

func (*Platform) IsBlackBerry

func (p *Platform) IsBlackBerry() bool

IsBlackBerry returns true if platform is BlackBerry.

func (*Platform) IsChromeOS

func (p *Platform) IsChromeOS() bool

IsChromeOS returns true if the user agent string matches Chrome OS.

func (*Platform) IsChromeOSVersionCompatible

func (p *Platform) IsChromeOSVersionCompatible(version string) bool

IsChromeOSVersionCompatible returns true if the user agent string matches Chrome OS and the version is greater than or equal to the specified version.

func (*Platform) IsIOS

func (p *Platform) IsIOS() bool

IsIOS returns true if the user agent string matches iOS.

func (*Platform) IsIOSApp

func (p *Platform) IsIOSApp() bool

IsIOSApp returns true if the platform is iOS app and the user agent string does not contain Safari.

func (*Platform) IsIOSWebview

func (p *Platform) IsIOSWebview() bool

IsIOSWebview is an alias for IsIOSApp.

func (*Platform) IsKaiOS

func (p *Platform) IsKaiOS() bool

IsKaiOS returns true if the user agent string matches KaiOS.

func (*Platform) IsLinux

func (p *Platform) IsLinux() bool

IsLinux returns true if the platform is Linux.

func (*Platform) IsWatchOS

func (p *Platform) IsWatchOS() bool

IsWatchOS returns true if the user agent string matches Watch OS.

func (*Platform) IsWindows

func (p *Platform) IsWindows() bool

IsWindows returns true if the platform is Windows.

func (*Platform) IsWindows10

func (p *Platform) IsWindows10() bool

IsWindows10 returns true if the platform is Windows 10.

func (*Platform) IsWindows7

func (p *Platform) IsWindows7() bool

IsWindows7 returns true if the platform is Windows 7.

func (*Platform) IsWindows8

func (p *Platform) IsWindows8() bool

IsWindows8 returns true if the platform is Windows 8.

func (*Platform) IsWindows8_1

func (p *Platform) IsWindows8_1() bool

IsWindows8_1 returns true if the platform is Windows 8.1.

func (*Platform) IsWindowsMobile

func (p *Platform) IsWindowsMobile() bool

IsWindowsMobile returns true if the platform is Windows Mobile.

func (*Platform) IsWindowsPhone

func (p *Platform) IsWindowsPhone() bool

IsWindowsPhone returns true if the platform is Windows Phone.

func (*Platform) IsWindowsRT

func (p *Platform) IsWindowsRT() bool

IsWindowsRT returns true if the platform is Windows RT.

func (*Platform) IsWindowsTouchScreenDesktop

func (p *Platform) IsWindowsTouchScreenDesktop() bool

IsWindowsTouchScreenDesktop returns true if the platform is Windows 8 and the user agent string contains Touch.

func (*Platform) IsWindowsVista

func (p *Platform) IsWindowsVista() bool

IsWindowsVista returns true if the platform is Windows Vista.

func (*Platform) IsWindowsWOW64

func (p *Platform) IsWindowsWOW64() bool

func (*Platform) IsWindowsX64

func (p *Platform) IsWindowsX64() bool

IsWindowsX64 returns true if the platform is Windows x64.

func (*Platform) IsWindowsX64Inclusive

func (p *Platform) IsWindowsX64Inclusive() bool

func (*Platform) IsWindowsXP

func (p *Platform) IsWindowsXP() bool

IsWindowsXP returns true if the platform is Windows XP.

func (*Platform) Name

func (p *Platform) Name() string

Name returns the name of the platform.

func (*Platform) Version

func (p *Platform) Version() string

Version returns the version of the platform.

type PlatformMatcher

type PlatformMatcher interface {
	Matcher
	Version() string // Version returns the version of the platforms.
}

PlatformMatcher is an interface for user agent platform matchers. A platform matcher is used to detect the platform from the user agent string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL