mobiledetect

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2022 License: MIT Imports: 5 Imported by: 0

README

Go MobileDetect

Library for detecting mobile devices and tablets

Go Mobile Detect is a lightweight Go package imported from PHP for detecting mobile devices (including tablets).

Go Reference Go GitHub GitHub go.mod Go version (branch) GitHub release (latest by date) GitHub tag (latest by date)

What is it?

It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

The package is imported from MobileDetect which was originally written in PHP.

Go/Golang parser for user agent strings README

Installation
go get -u -v github.com/houseme/mobiledetect 
Updates

Version 1.2.1

Why is it useful?

There are different ways of using the package:

Go/Golang package for parsing user agent strings

Package ua.New(userAgent string) function parses browser's and bot's user agents strings and determins:

  • User agent name and version (Chrome, Firefox, Googlebot, etc.)
  • Operating system name and version (Windows, Android, iOS etc.)
  • Device type (mobile, desktop, tablet, bot)
  • Device name if available (iPhone, iPad, Huawei VNS-L21)
  • URL provided by the bot (http://www.google.com/bot.html etc.)
Status
Still need some work on detecting Android device names.

Fill free to report an issue for any User-Agent string not recognized or misinterpreted.
Example for parsing user agent strings
package main

import (
    "fmt"
    "strings"

    "github.com/houseme/mobiledetect/ua"
)

func main() {
    userAgents := []string{
        "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 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
        "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.2.4 (KHTML, like Gecko) FxiOS/8.1.1b4948 Mobile/14F89 Safari/603.2.4",
        "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 (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",
    }

    for _, s := range userAgents {
        ua := ua.New(s)
        fmt.Println()
        fmt.Println(ua.UA())
        fmt.Println(strings.Repeat("=", len(ua.UA())))
        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())
        }
        fmt.Printf("%v\n", ua.Mobile())   // => true
        fmt.Printf("%v\n", ua.Bot())      // => false
        fmt.Printf("%v\n", ua.Mozilla())  // => "5.0"
        fmt.Printf("%v\n", ua.Model())    // => "Nexus One"
    
        fmt.Printf("%v\n", ua.Platform()) // => "Linux"
        fmt.Printf("%v\n", ua.OS())       // => "Android 2.3.7"
    
        name, version := ua.Engine()
        fmt.Printf("%v\n", name)          // => "AppleWebKit"
        fmt.Printf("%v\n", version)       // => "533.1"
    
        name, version = ua.Browser()
        fmt.Printf("%v\n", name)          // => "Android"
        fmt.Printf("%v\n", version)       // => "4.0"
    
        // Let's see an example with a bot.
    
        ua.Parse("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
    
        fmt.Printf("%v\n", ua.Bot())      // => true
    
        name, version = ua.Browser()
        fmt.Printf("%v\n", name)          // => Googlebot
        fmt.Printf("%v\n", version)       // => 2.1
    }
}
License

Go Mobile Detect is an open-source script released under MIT License. thanks for Shaked and serbanghita.

Documentation

Index

Constants

View Source
const (

	// MobileGradeA .
	MobileGradeA = "A"
	// MobileGradeB .
	MobileGradeB = "B"
	// MobileGradeC .
	MobileGradeC = "C"
)
View Source
const (
	PropMobile = iota
	PropBuild
	PropVersion
	PropVendorid
	PropIpad
	PropIphone
	PropIpod
	PropKindle
	PropChrome
	PropCoast
	PropDolfin
	PropFirefox
	PropFennec
	PropIe
	PropNetfront
	PropNokiabrowser
	PropOpera
	PropOperaMini
	PropOperaMobi
	PropUcBrowser
	PropMqqbrowser
	PropMicromessenger
	PropBaiduboxapp
	PropBaidubrowser
	PropSafari
	PropSkyfire
	PropTizen
	PropWebkit
	PropGecko
	PropTrident
	PropPresto
	PropIos
	PropAndroid
	PropBlackberry
	PropBrew
	PropJava
	PropWindowsPhoneOs
	PropWindowsPhone
	PropWindowsCe
	PropWindowsNt
	PropSymbian
	PropWebos
)
View Source
const (
	IPHONE = iota
	BLACKBERRY
	PIXEL
	HTC
	NEXUS
	DELL
	MOTOROLA
	SAMSUNG
	LG
	SONY
	ASUS
	XIAOMI
	NOKIA
	MICROMAX
	PALM
	VERTU
	PANTECH
	FLY
	WIKO
	IMOBILE
	SIMVALLEY
	WOLFGANG
	ALCATEL
	NINTENDO
	AMOI
	INQ
	ONEPLUS
	GENERICPHONE

	IPAD = iota
	NEXUSTABLET
	GOOGLETABLET
	SAMSUNGTABLET
	KINDLE
	SURFACETABLET
	HPTABLET
	ASUSTABLET
	BLACKBERRYTABLET
	HTCTABLET
	MOTOROLATABLET
	NOOKTABLET
	ACERTABLET
	TOSHIBATABLET
	LGTABLET
	FUJITSUTABLET
	PRESTIGIOTABLET
	LENOVOTABLET
	DELLTABLET
	YARVIKTABLET
	MEDIONTABLET
	ARNOVATABLET
	INTENSOTABLET
	IRUTABLET
	MEGAFONTABLET
	EBODATABLET
	ALLVIEWTABLET
	ARCHOSTABLET
	AINOLTABLET
	NOKIALUMIATABLET
	SONYTABLET
	PHILIPSTABLET
	CUBETABLET
	COBYTABLET
	MIDTABLET
	MSITABLET
	SMITTABLET
	ROCKCHIPTABLET
	FLYTABLET
	BQTABLET
	HUAWEITABLET
	NECTABLET
	PANTECHTABLET
	BRONCHOTABLET
	VERSUSTABLET
	ZYNCTABLET
	POSITIVOTABLET
	NABITABLET
	KOBOTABLET
	DANEWTABLET
	TEXETTABLET
	PLAYSTATIONTABLET
	TREKSTORTABLET
	PYLEAUDIOTABLET
	ADVANTABLET
	DANYTECHTABLET
	GALAPADTABLET
	MICROMAXTABLET
	KARBONNTABLET
	ALLFINETABLET
	PROSCANTABLET
	YONESTABLET
	CHANGJIATABLET
	GUTABLET
	POINTOFVIEWTABLET
	OVERMAXTABLET
	HCLTABLET
	DPSTABLET
	VISTURETABLET
	CRESTATABLET
	MEDIATEKTABLET
	CONCORDETABLET
	GOCLEVERTABLET
	MODECOMTABLET
	VONINOTABLET
	ECSTABLET
	STOREXTABLET
	VODAFONETABLET
	ESSENTIELBTABLET
	ROSSMOORTABLET
	IMOBILETABLET
	TOLINOTABLET
	AUDIOSONICTABLET
	AMPETABLET
	SKKTABLET
	TECNOTABLET
	JXDTABLET
	IJOYTABLET
	FX2TABLET
	XOROTABLET
	VIEWSONICTABLET
	VERIZONTABLET
	ODYSTABLET
	CAPTIVATABLET
	ICONBITTABLET
	TECLASTTABLET
	ONDATABLET
	JAYTECHTABLET
	BLAUPUNKTTABLET
	DIGMATABLET
	EVOLIOTABLET
	LAVATABLET
	AOCTABLET
	MPMANTABLET
	CELKONTABLET
	WOLDERTABLET
	MEDIACOMTABLET
	MITABLET
	NIBIRUTABLET
	NEXOTABLET
	LEADERTABLET
	UBISLATETABLET
	POCKETBOOKTABLET
	KOCASOTABLET
	HISENSETABLET
	HUDL
	TELSTRATABLET
	GENERICTABLET

	ANDROIDOS = iota
	BLACKBERRYOS
	PALMOS
	SYMBIANOS
	WINDOWSMOBILEOS
	WINDOWSPHONEOS
	IOS
	IPADOS
	SAILFISHOS
	MEEGOOS
	MAEMOOS
	JAVAOS
	WEBOS
	BADAOS
	BREWOS

	CHROME = iota
	DOLFIN
	OPERA
	SKYFIRE
	EDGE
	IE
	FIREFOX
	BOLT
	TEASHARK
	BLAZER
	SAFARI
	TIZEN
	WECHAT
	UCBROWSER
	BAIDUBOXAPP
	BAIDUBROWSER
	DIIGOBROWSER
	PUFFIN
	MERCURY
	OBIGOBROWSER
	NETFRONT
	GENERICBROWSER
	PALEMOON

	BOT = iota
	MOBILEBOT
	DESKTOPMODE
	TV
	WEBKIT
	CONSOLE
	WATCH
)

Variables

This section is empty.

Functions

func Device

func Device(r *http.Request) string

Device Vars returns the route variables for the current request, if any.

func Handler

func Handler(h DeviceHandler, rules *rules) http.Handler

Handler .

func HandlerMux

func HandlerMux(s *http.ServeMux, rules *rules) http.Handler

HandlerMux .

func NewRules

func NewRules() *rules

NewRules creates a object with all rules necessary to figure out a browser from a User Agent string

Types

type DeviceHandler

type DeviceHandler interface {
	Mobile(w http.ResponseWriter, r *http.Request, m *MobileDetect)
	Tablet(w http.ResponseWriter, r *http.Request, m *MobileDetect)
	Desktop(w http.ResponseWriter, r *http.Request, m *MobileDetect)
}

DeviceHandler .

type MobileDetect

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

MobileDetect holds the structure to figure out a browser from a UserAgent string and methods necessary to make it happen

func New

func New(r *http.Request, rules *rules) *MobileDetect

New creates the MobileDetect object

func (*MobileDetect) CheckHTTPHeadersForMobile

func (md *MobileDetect) CheckHTTPHeadersForMobile() bool

CheckHTTPHeadersForMobile looks for mobile rules to confirm if the browser is a mobile browser

func (*MobileDetect) Is

func (md *MobileDetect) Is(key interface{}) bool

Is It is recommended to use IsKey instead

func (*MobileDetect) IsKey

func (md *MobileDetect) IsKey(key int) bool

IsKey Is compared the detected browser with a "rule" from the existing rules list

func (*MobileDetect) IsMobile

func (md *MobileDetect) IsMobile() bool

IsMobile is a specific case to detect only mobile browsers.

func (*MobileDetect) IsTablet

func (md *MobileDetect) IsTablet() bool

IsTablet is a specific case of detect only tablet browsers on tablets. Do not overlap with IsTablet

func (*MobileDetect) MobileGrade

func (md *MobileDetect) MobileGrade() string

MobileGrade returns a graduation similar to jQuery's Graded Browse Support

func (*MobileDetect) PreCompileRegexRules

func (md *MobileDetect) PreCompileRegexRules() *MobileDetect

PreCompileRegexRules .

func (*MobileDetect) SetHTTPHeaders

func (md *MobileDetect) SetHTTPHeaders(httpHeaders map[string]string) *MobileDetect

SetHTTPHeaders .

func (*MobileDetect) SetUserAgent

func (md *MobileDetect) SetUserAgent(userAgent string) *MobileDetect

SetUserAgent .

func (*MobileDetect) Version

func (md *MobileDetect) Version(propertyName interface{}) string

Version It is recommended to use VersionKey instead

func (*MobileDetect) VersionFloat

func (md *MobileDetect) VersionFloat(propertyName interface{}) float64

VersionFloat It is recommended to use VersionFloatKey instead

func (*MobileDetect) VersionFloatKey

func (md *MobileDetect) VersionFloatKey(propertyVal int) float64

VersionFloatKey VersionFloat does the same as Version, but returns a float number good for version comparison

func (*MobileDetect) VersionKey

func (md *MobileDetect) VersionKey(propertyVal int) string

VersionKey Version detects the browser version returning as string

Directories

Path Synopsis
mux
ua

Jump to

Keyboard shortcuts

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