Skip to content

Initial v0.0.0 Release #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 103 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
d0652ce
feat: add version comparision utility
dineshgowda24 Sep 16, 2023
5ca7b62
feat: add base matcher
dineshgowda24 Sep 16, 2023
ca7c4ff
spec: Add seed user agents
dineshgowda24 Sep 16, 2023
6fcc757
feat: Add Alipay matcher
dineshgowda24 Sep 16, 2023
227e118
feat: add BlackBerry matcher
dineshgowda24 Sep 16, 2023
bcf48f1
feat: add DuckDuckGo matcher
dineshgowda24 Sep 16, 2023
fa57dc4
feat: add Electron matcher
dineshgowda24 Sep 16, 2023
f539ef5
feat: add GSA matcher
dineshgowda24 Sep 16, 2023
ad20a9a
feat: add Instagram matcher
dineshgowda24 Sep 16, 2023
69ad4b6
feat: add Konqueror matcher
dineshgowda24 Sep 16, 2023
6fcdbd6
feat: add Maxthon matcher
dineshgowda24 Sep 16, 2023
886a9f4
feat: add MiuiBrowser matcher
dineshgowda24 Sep 16, 2023
d9505f3
feat: add Nokia matcher
dineshgowda24 Sep 16, 2023
40c4816
feat: add Opera matcher
dineshgowda24 Sep 16, 2023
bec92f1
feat: add PaleMoon matcher
dineshgowda24 Sep 16, 2023
59072fb
feat: add Puffin matcher
dineshgowda24 Sep 16, 2023
af56294
feat: add QQ matcher
dineshgowda24 Sep 16, 2023
54fce01
feat: add SamsungBrowser matcher
dineshgowda24 Sep 17, 2023
03f3bef
feat: add Snapchat matcher
dineshgowda24 Sep 17, 2023
49314bf
feat: add UCBrowser matcher
dineshgowda24 Sep 17, 2023
d3f76c9
feat: add VivoBrowser matcher
dineshgowda24 Sep 17, 2023
5056ab1
feat: add Yandex matcher
dineshgowda24 Sep 17, 2023
aa22b7b
feat: add YaaniBrowser matcher
dineshgowda24 Sep 17, 2023
9c22bb2
spec: add yml to load test platforms
dineshgowda24 Sep 17, 2023
dad9ba8
feat: add base struct for platforms
dineshgowda24 Sep 17, 2023
ed6385b
feat: add AdobeAir platform
dineshgowda24 Sep 17, 2023
e196040
feat: add Android platform
dineshgowda24 Sep 17, 2023
6896b17
feat: add BlackBerry platform
dineshgowda24 Sep 17, 2023
f3ad87c
feat: add ChromeOS platform
dineshgowda24 Sep 17, 2023
5da2883
feat: add FirefoxOs platform
dineshgowda24 Sep 17, 2023
cb82835
feat: add IOS platform
dineshgowda24 Sep 17, 2023
68f36c3
feat: add KaiOS platform
dineshgowda24 Sep 17, 2023
74ba7df
feat: add Linux platform
dineshgowda24 Sep 17, 2023
209dad0
feat: add Mac platform
dineshgowda24 Sep 17, 2023
2346229
feat: add WindowsMobile platform
dineshgowda24 Sep 17, 2023
6af840d
feat: add WindowsPhone platform
dineshgowda24 Sep 17, 2023
eb79d30
feat: add Windows platform
dineshgowda24 Sep 17, 2023
8ad473e
feat: add WatchOS platform
dineshgowda24 Sep 17, 2023
787b3a6
feat: add Unknown platform
dineshgowda24 Sep 18, 2023
d3ad0d6
feat: add HuaweiBrowser matcher
dineshgowda24 Sep 18, 2023
9afc061
feat: add SogouBrowser matcher
dineshgowda24 Sep 18, 2023
53546e2
feat: add Sputnik matcher
dineshgowda24 Sep 18, 2023
37f0f15
feat: add MicroMessenger matcher
dineshgowda24 Sep 18, 2023
725cd88
spec: add yml to load test devices
dineshgowda24 Sep 18, 2023
ab180c0
feat: add base struct for devices
dineshgowda24 Sep 18, 2023
36d409f
feat: add Blackberry device
dineshgowda24 Sep 18, 2023
0599359
feat: add ps3 device
dineshgowda24 Sep 18, 2023
2399147
feat: add ps4 device
dineshgowda24 Sep 18, 2023
a2d1ddc
feat: add ps5 device
dineshgowda24 Sep 18, 2023
917f7dc
feat: add KindleFire device
dineshgowda24 Sep 18, 2023
3541ead
feat: add Kindle device
dineshgowda24 Sep 18, 2023
e4b50c6
feat: add iPod touch device
dineshgowda24 Sep 18, 2023
1ae4ccd
feat: add PS vita device
dineshgowda24 Oct 2, 2023
3674c1a
feat: add Playstation Portable device
dineshgowda24 Oct 2, 2023
2407fe5
feat: add Nintendo Switch device
dineshgowda24 Oct 2, 2023
426e2bb
feat: add TV device
dineshgowda24 Oct 2, 2023
17a6c08
feat: add default device matcher
dineshgowda24 Oct 2, 2023
f07ad5f
feat: add Nintendo WiiU device
dineshgowda24 Oct 3, 2023
274490d
feat: add Nintendo Wii device
dineshgowda24 Oct 3, 2023
25ab6e5
feat: add Xbox360 device
dineshgowda24 Oct 3, 2023
26b1904
feat: add Xbox One device
dineshgowda24 Oct 3, 2023
47cc2e5
feat: add Ipad device
dineshgowda24 Oct 3, 2023
470dbfe
feat: add Iphone device
dineshgowda24 Oct 3, 2023
e7a5c3e
feat: add Android device
dineshgowda24 Oct 3, 2023
a1219c2
feat: add Safari matcher
dineshgowda24 Oct 3, 2023
17b1d70
feat: add Weibo matcher
dineshgowda24 Oct 3, 2023
0518004
feat: add Otter matcher
dineshgowda24 Oct 3, 2023
557d833
feat: add Chrome matcher
dineshgowda24 Oct 4, 2023
3389c58
feat: add Browser
dineshgowda24 Oct 4, 2023
8cc48de
feat: add browser identifer functions
dineshgowda24 Oct 4, 2023
5e40c6f
feat: Add device struct
dineshgowda24 Oct 6, 2023
2c1ab68
feat: add device to browser
dineshgowda24 Oct 6, 2023
22b4d18
feat: add Platform to browser
dineshgowda24 Oct 7, 2023
60deef8
feat: add Surface device
dineshgowda24 Oct 9, 2023
b0f11f4
feat: add `IsMobile` and `IsTablet`
dineshgowda24 Oct 9, 2023
e76a4a4
refactor: update convey message
dineshgowda24 Oct 9, 2023
2ede650
fix: add mobile useragents
dineshgowda24 Oct 9, 2023
d158265
feat: Add windows related helper functions
dineshgowda24 Oct 9, 2023
00fde16
docs: update platform docs
dineshgowda24 Oct 9, 2023
cd82b98
fix: init platform user agents
dineshgowda24 Oct 9, 2023
31cdaa1
spec: add specs for `IsAndroidApp`
dineshgowda24 Oct 9, 2023
0109a24
feat: add Unknown browser matcher
dineshgowda24 Oct 9, 2023
1bb5ab0
feat: add `IsUnknown` and `IsBrowserKnown` helpers
dineshgowda24 Oct 9, 2023
0b8c85e
refactor: Rename dir `_test` to `test`
dineshgowda24 Oct 10, 2023
3eb84d7
feat: Add internet explorer browser
dineshgowda24 Oct 10, 2023
19f9156
feat: add Ms Edge browser
dineshgowda24 Oct 10, 2023
d8cbf57
refactor: change method ordering as per usage
dineshgowda24 Oct 10, 2023
b2d1575
feat: add Samsung device
dineshgowda24 Oct 10, 2023
cc65991
fix: auto register matchers
dineshgowda24 Oct 11, 2023
79fee30
fix: invoke `register` in `NewPlatform`
dineshgowda24 Oct 11, 2023
fa65a14
feat: add `BotMatcher`
dineshgowda24 Oct 11, 2023
3505520
feat: Add `Empty` bots matcher
dineshgowda24 Oct 11, 2023
8454f4c
feat: add `Keword` bot matcher
dineshgowda24 Oct 11, 2023
d399eec
refactor: sort yml files
dineshgowda24 Oct 11, 2023
1b355bb
doc: add logo
dineshgowda24 Oct 11, 2023
92a176f
feat: add `Known` bots matcher
dineshgowda24 Oct 12, 2023
43e9425
fix: change the name of empty and known bot
dineshgowda24 Oct 12, 2023
8b370f4
feat: Add `Bot` to browser
dineshgowda24 Oct 12, 2023
eef7333
fix: update broken specs
dineshgowda24 Oct 12, 2023
cde8656
docs: add contributing guide
dineshgowda24 Oct 12, 2023
2fad404
docs: update read me
dineshgowda24 Oct 12, 2023
714180f
fix: add `Version` func
dineshgowda24 Oct 12, 2023
53c7b6f
docs: update readme usage
dineshgowda24 Oct 12, 2023
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
51 changes: 51 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Contributing

## Requirements

- [Go](https://golang.org/dl/) 1.19 or higher

## Getting Started

1. Fork the repository and clone it locally
2. Run `go mod download` to download dependencies
3. Run `go test ./...` to run all tests

## Testing

The tests are located in the `matchers`, `devices`, `platforms` and `browser` packages. The tests are using the `assets/test` directory to load the user agents data.

Package follows BDD style test cases. The tests are using the [GoConvey](https://github.com/smartystreets/goconvey) library to write BDD style tests.

## Adding a new browser support

Every browser implements the `BrowserMatcher` interface.

1. Add the file to the `matchers` package.
2. Add the new browser the list of matchers in `browser.go`. Make sure the order of the matchers is correct. The order of the matchers is important because the first matcher that returns `true` will be used.
3. Add specific tests for the new browser in the `matchers` package. Make sure to test all the possible user agents for the browser. You can use [this](https://developers.whatismybrowser.com/useragents/explore/) website to find user agents for the browser.
4. Add the user agents data to [matchers.yml](assets/test/matchers.yml) file.
5. Add all necerssary functions to `Browser` struct in `browser.go` file.

## Adding a new device support

Every device implements the `DeviceMatcher` interface.

1. Add the file to the `devices` package.
2. Add the new device to list of `DeviceMatcher` in `device.go`. Make sure the order of the matchers is correct. The order of the matchers is important because the first matcher that returns `true` will be used.
3. Add specific tests for the new device in the `devices` package. Make sure to test all the possible user agents for the device. You can use [this](https://developers.whatismybrowser.com/useragents/explore/) website to find user agents for the device.
4. Add the user agents data to [devices.yml](assets/test/devices.yml) file.
5. Add all necerssary functions to `Device` struct in `device.go` file.

## Adding a new platform support

Every platform implements the `PlatformMatcher` interface.

1. Add the file to the `platforms` package.
2. Add the new platform to list of `PlatformMatcher` in `platform.go`. Make sure the order of the matchers is correct. The order of the matchers is important because the first matcher that returns `true` will be used.
3. Add specific tests for the new platform in the `platforms` package. Make sure to test all the possible user agents for the platform. You can use [this](https://developers.whatismybrowser.com/useragents/explore/) website to find user agents for the platform.
4. Add the user agents data to [platforms.yml](assets/test/platforms.yml) file.
5. Add all necerssary functions to `Platform` struct in `platform.go` file.

## Adding a new known bot support

1. Add the new bot to [known_bots.yml](assets/internal/known_bots.yml).
135 changes: 116 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,157 @@
# Browser: A browser detection in Go (Golang)
# Browser

![](logo.png)

## Why?

I wanted a package to detect the browser, device and platform from the user agent string. I found nothing similar in Go (Golang), so I wrote my package.
> *Why not?*

### Inspiration
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:

This package is inspired by the ruby gem [fnando/browser](https://github.com/fnando/browser). I have used the ruby gem in my previous projects and I really liked it. All the credit goes to the author of the ruby gem who has done a great job. After much procrastination, I finally decided to write a similar package in Go.
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.

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

## Inspiration

I have tried to keep the architecture as similar as possible to the ruby gem. But made changes where I felt it was necessary as per the Go standards.
The ruby gem [fnando/browser](https://github.com/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

The package is divided into three parts:
I have kept the design as similar as possible to the gem but made changes where I felt necessary per the Go.

- **browser**: This package contains the code to detect the browser.
- **device**: This package contains the code to detect the device from the user agent string.
- **platform**: This package contains the code to detect the platform from the user agent string.
The following are the sub-packages:

### Browser
- **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.

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

`BrowserMatchers` interface matches the user agent string with the browser. Any new browser can be added by implementing the `BrowserMatchers` interface.
### Matchers

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

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

### Device
### Devices

`DeviceMatchers` interface matches the user agent string with the device. Any new device can be added by implementing the `DeviceMatchers` interface.
`DeviceMatcher` interface matches the user agent string with the device. Implement the `DeviceMatcher` interface to add a new device.

```go
type DeviceMatcher interface {
Matcher
}
```

### Platform
### Platforms

`PlatformMatchers` interface matches the user agent string with the platform. Any new platform can be added by implementing the `PlatformMatchers` interface.
`PlatformMatcher` interface matches the user agent string with the platform. Implement the `PlatformMatcher` interface to add a new device.

```go
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.

```go
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
go get github.com/dineshgowda24/browser
```

### Browser Detection

```go
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

```go
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](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](LICENSE) file for details.
Loading