1
0
mirror of https://github.com/lancedikson/bowser synced 2024-10-27 20:34:22 +00:00
lancedikson_bowser/README.md

295 lines
7.7 KiB
Markdown
Raw Normal View History

2014-03-01 18:22:06 +00:00
## Bowser
2011-04-27 23:25:55 +00:00
A Browser detector. Because sometimes, there is no other way, and not even good modern browsers always provide good feature detection mechanisms.
2011-04-27 22:14:35 +00:00
2018-07-05 18:18:04 +00:00
[![bowser ci](https://secure.travis-ci.org/lancedikson/bowser.png)](https://travis-ci.org/lancedikson/bowser/)
2014-02-22 21:34:00 +00:00
2017-10-18 08:47:53 +00:00
# Contents
- Overview
- Use cases
- API
- How can I help?
2011-04-27 22:14:35 +00:00
2017-10-18 08:47:53 +00:00
# Overview
The library is made to help to detect what browser your user has and gives you a convenient API
to filter the users somehow depending on their browsers.
# Use cases
2018-07-05 18:18:04 +00:00
First of all, require the library:
2017-10-18 08:47:53 +00:00
```
2018-07-05 18:18:04 +00:00
const Bowser = require('bowser');
2017-10-18 08:47:53 +00:00
```
## Browser props detection
Often we need to pick users' browser properties such as the name,
the version, the rendering engine and so on. Here is an example how to make it with Bowser:
```
const browser = new Bowser(window.navigator.userAgent);
2018-07-05 18:18:04 +00:00
console.log(`The current browser name is "${browser.getBrowserName()}"`);
// The current browser name is "Internet Explorer"
2017-10-18 08:47:53 +00:00
```
or
```
const impression = new Impression();
const browser = new Bowser(window.navigator.userAgent);
const browserInfo = browser.getBrowser();
impression.brName = browserInfo.name;
impression.brVer = browserInfo.version;
```
2011-04-27 22:14:35 +00:00
2017-10-18 08:47:53 +00:00
or
```
const browser = new Bowser(window.navigator.userAgent);
impression.userTechData = browser.parse();
console.log(impression.userTechData);
2018-07-05 18:18:04 +00:00
// outputs
{
browser: {
name: "Internet Explorer"
version: "11.0"
},
os: {
name: "Windows"
version: "NT 6.3"
versionName: "8.1"
},
platform: {
type: "desktop"
},
engine: {
name: "Trident"
version: "7.0"
}
}
2017-10-18 08:47:53 +00:00
```
## Filtering browsers
You could want to filter some particular browsers to provide any special
support for them or make any workarounds.
It could look like this:
```
2018-07-05 18:18:04 +00:00
const browser = new Bowser(window.navigator.userAgent);
const isValidBrowser = bowser.compare({
// declare browsers per OS
2017-10-18 08:47:53 +00:00
windows: {
2018-07-05 18:18:04 +00:00
"internet explorer": ">10",
2017-10-18 08:47:53 +00:00
},
macos: {
2018-07-05 18:18:04 +00:00
safari: ">10.1"
2017-10-18 08:47:53 +00:00
},
2018-07-05 18:18:04 +00:00
// per platform (mobile, desktop or tablet)
mobile: {
safari: '>9',
'android browser': '>3.10'
2017-10-18 08:47:53 +00:00
},
2018-07-05 18:18:04 +00:00
// or in general
chrome: ">20.1.1432",
firefox: ">31",
opera: ">22"
});
2017-10-18 08:47:53 +00:00
```
As you can see, settings for any particular OS has more priority and redefines settings of `any` property.
Read more details in the [API section]()
# API
2016-05-06 18:29:22 +00:00
2018-07-05 18:18:04 +00:00
### new Bowser(`:Object`)
2016-06-30 17:52:55 +00:00
Use it to get object with detected flags of your current browser.
### bowser._detect(ua `:String`)`:Object`
Use it to get object with detected flags from User Agent string.
2016-07-26 11:05:51 +00:00
### bowser.check(minVersions`:Object`, strictMode`:Boolean`, [ua]`:String`)`:Boolean`
Use it to check if browser is supported. In default non-strict mode any browser family not present in `minVersions` will pass the check (like Chrome in the third call in the sample bellow). When strict mode is enabled then any not specified browser family in `minVersions` will cause `check` to return `false` (in the sample it is the fourth call, the last one).
2016-06-30 17:52:55 +00:00
``` js
/**
* in case of using IE10
*/
bowser.check({msie: "11"}); // true
bowser.check({msie: "9.0"}); // false
/**
* specific user agent
*/
bowser.check({chrome: "45"}, window.navigator.userAgent); // true
/**
* but false in strict mode
*/
bowser.check({chrome: "45"}, true, window.navigator.userAgent); // false
2016-06-30 17:52:55 +00:00
```
### bowser.compareVersions(versions`:Array<String>`)`:Number`
Use it to compare two versions.
``` js
2016-08-25 12:01:03 +00:00
bowser.compareVersions(['9.0', '10']);
2016-06-30 17:52:55 +00:00
// -1
```
### bowser.isUnsupportedBrowser(minVersions`:Object`, [strictMode]`:Boolean`, [ua]`:string`)`:Boolean`
Use it to check if browser is unsupported.
``` js
2016-08-25 12:01:03 +00:00
bowser.isUnsupportedBrowser({msie: "10"}, window.navigator.userAgent);
2016-06-30 17:52:55 +00:00
// true / false
```
See more examples in [tests](test/test.js).
---
2015-11-06 17:52:11 +00:00
## Bowser Flags
Your mileage may vary, but these flags should be set. See Contributing below.
2011-04-27 22:14:35 +00:00
2015-11-06 17:52:11 +00:00
``` js
alert('Hello ' + bowser.name + ' ' + bowser.version);
```
### All detected browsers
These flags are set for all detected browsers:
* `name` - A human readable name for this browser. E.g. 'Chrome', ''
* `version` - Version number for the browser. E.g. '32.0'
2015-11-06 17:58:50 +00:00
For unknown browsers, Bowser makes a best guess from the UA string. So, these may not be set.
2015-11-06 17:52:11 +00:00
### Rendering engine flags
2015-11-06 17:52:11 +00:00
If detected, one of these flags may be set to true:
2016-05-09 07:24:55 +00:00
* `webkit` - Chrome 0-27, Android <4.4, iOs, BB, etc.
* `blink` - Chrome >=28, Android >=4.4, Opera, etc.
2015-11-06 17:52:11 +00:00
* `gecko` - Firefox, etc.
* `msie` - IE <= 11
* `msedge` - IE > 11
Safari, Chrome and some other minor browsers will report that they have `webkit` engines.
Firefox and Seamonkey will report that they have `gecko` engines.
``` js
if (bowser.webkit) {
// do stuff with safari & chrome & opera & android & blackberry & webos & silk
}
```
### Device flags
If detected, one of these flags may be set to true:
* `mobile` - All detected mobile OSes are additionally flagged `mobile`, **unless it's a tablet**
2015-11-06 17:58:50 +00:00
* `tablet` - If a tablet device is detected, the flag `tablet` is **set instead of `mobile`**.
2015-11-06 17:52:11 +00:00
2015-11-06 17:58:50 +00:00
### Browser flags
If detected, one of these flags may be set to true. The rendering engine flag is shown in []'s:
2015-11-06 17:52:11 +00:00
2016-05-09 07:24:55 +00:00
* `chrome` - [`webkit`|`blink`]
2015-11-06 17:52:11 +00:00
* `firefox` - [`gecko`]
* `msie`
2015-05-16 01:02:18 +00:00
* `msedge`
2015-11-06 17:52:11 +00:00
* `safari` - [`webkit`]
2016-05-09 07:24:55 +00:00
* `android` - native browser - [`webkit`|`blink`]
2015-11-06 17:58:50 +00:00
* `ios` - native browser - [`webkit`]
2016-05-09 07:24:55 +00:00
* `opera` - [`blink` if >=15]
* `samsungBrowser` - [`blink`]
2015-11-06 17:52:11 +00:00
* `phantom` - [`webkit`]
2015-11-06 17:58:50 +00:00
* `blackberry` - native browser - [`webkit`]
* `webos` - native browser - [`webkit`]
* `silk` - Amazon Kindle browser - [`webkit`]
2015-11-06 17:52:11 +00:00
* `bada` - [`webkit`]
* `tizen` - [`webkit`]
* `seamonkey` - [`gecko`]
* `sailfish` - [`gecko`]
* `ucbrowser` — [`webkit`]
* `qupzilla` — [`webkit`]
2016-05-09 07:24:55 +00:00
* `vivaldi` — [`blink`]
2016-05-09 07:18:11 +00:00
* `sleipnir` — [`blink`]
* `kMeleon` — [`gecko`]
For all detected browsers the browser version is set in the `version` field.
2015-11-06 17:52:11 +00:00
### OS Flags
If detected, one of these flags may be set to true:
2014-02-21 14:09:58 +00:00
* `mac`
2015-11-06 17:52:11 +00:00
* `windows` - other than Windows Phone
* `windowsphone`
* `linux` - other than `android`, `chromeos`, `webos`, `tizen`, and `sailfish`
* `chromeos`
* `android`
2015-11-06 17:52:11 +00:00
* `ios` - also sets one of `iphone`/`ipad`/`ipod`
* `blackberry`
* `firefoxos`
2015-11-06 17:52:11 +00:00
* `webos` - may also set `touchpad`
* `bada`
* `tizen`
* `sailfish`
2014-02-21 14:09:58 +00:00
2015-11-06 17:52:11 +00:00
`osversion` may also be set:
2014-02-21 14:09:58 +00:00
2017-05-17 21:45:04 +00:00
* `osversion` - for Android, iOS, MacOS, Windows, Windows Phone, WebOS, Bada, and Tizen. If included in UA string.
2011-04-27 22:14:35 +00:00
2015-11-06 17:52:11 +00:00
iOS is always reported as `ios` and additionally as `iphone`/`ipad`/`ipod`, whichever one matches best.
If WebOS device is an HP TouchPad the flag `touchpad` is additionally set.
2011-04-27 22:14:35 +00:00
2015-11-06 17:52:11 +00:00
### Browser capability grading
2015-11-06 17:58:50 +00:00
One of these flags may be set:
2015-11-06 17:52:11 +00:00
* `a` - This browser has full capabilities
* `c` - This browser has degraded capabilities. Serve simpler version
* `x` - This browser has minimal capabilities and is probably not well detected.
2015-11-06 17:58:50 +00:00
There is no `b`. For unknown browsers, none of these flags may be set.
2011-04-27 22:14:35 +00:00
2014-03-01 18:22:06 +00:00
### Ender Support
2011-04-27 22:14:35 +00:00
2014-03-01 18:22:06 +00:00
`package.json`
2011-04-27 22:14:35 +00:00
2014-03-01 18:22:06 +00:00
``` json
"dependencies": {
"bowser": "x.x.x"
}
```
2011-04-27 22:14:35 +00:00
``` js
2014-03-01 18:22:06 +00:00
if (require('bowser').chrome) {
alert('Hello Silicon Valley')
}
```
2014-03-01 18:22:06 +00:00
### Contributing
If you'd like to contribute a change to bowser, modify the files in `src/`, then run the following (you'll need node + npm installed):
2013-12-13 12:17:10 +00:00
2014-03-01 18:22:06 +00:00
``` sh
$ npm install
$ make test
```
2013-12-13 12:17:10 +00:00
2014-03-01 18:22:06 +00:00
Please do not check-in the built files `bowser.js` and `bowser.min.js` in pull requests.
2013-12-13 12:17:10 +00:00
2014-03-01 18:22:06 +00:00
### Adding tests
See the list in `src/useragents.js` with example user agents and their expected bowser object.
2013-12-13 12:17:10 +00:00
Whenever you add support for new browsers or notice a bug / mismatch, please update the list and
check if all tests are still passing.
2015-07-26 01:55:21 +00:00
### Similar Projects
* [Kong](https://github.com/BigBadBleuCheese/Kong) - A C# port of Bowser.
2015-07-26 01:55:21 +00:00
### License
Licensed as MIT. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.