diff --git a/README.md b/README.md index 3388cc0..98180ba 100644 --- a/README.md +++ b/README.md @@ -104,9 +104,6 @@ const isValidBrowser = bowser.compare({ ``` 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 ### new Bowser(`:Object`) Use it to get object with detected flags of your current browser. diff --git a/docs/Bowser.html b/docs/Bowser.html index 5897b25..bc5929f 100644 --- a/docs/Bowser.html +++ b/docs/Bowser.html @@ -22,7 +22,7 @@
@@ -559,7 +559,7 @@ bowser.getResult()
diff --git a/docs/Parser.html b/docs/Parser.html index 952b3cc..61b5139 100644 --- a/docs/Parser.html +++ b/docs/Parser.html @@ -22,7 +22,7 @@
@@ -265,173 +265,6 @@ like Parser#parseBrowser or compare(checkTree) → {Boolean} - - - - - - -
- - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - -
-

Check if parsed browser matches certain conditions

-
- - - - - - - - - -
Example
- -
const browser = new Bowser(UA);
-if (browser.check({chrome: '>118.01.1322' }))
-// or with os
-if (browser.check({windows: { chrome: '>118.01.1322' } }))
-// or with platforms
-if (browser.check({desktop: { chrome: '>118.01.1322' } }))
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
checkTree - - -Object - - - -

It's one or two layered object, -which can include a platform or an OS on the first layer -and should have browsers specs on the bottom-laying layer

- - - - - - - - - - - - - - -
Returns:
- - -
-

whether the browser satisfies the set conditions or not

-
- - - -
-
- Type -
-
- -Boolean - - -
-
- - - - - - - - -

getBrowser() → {Object}

@@ -1334,7 +1167,7 @@ and should have browsers specs on the bottom-laying layer

-

getPlatformType(toLowerCase) → {*}

+

getPlatformType(toLowerCaseopt) → {*}

@@ -1412,8 +1245,12 @@ and should have browsers specs on the bottom-laying layer

Type + Attributes + + Default + Description @@ -1437,8 +1274,24 @@ and should have browsers specs on the bottom-laying layer

+ + + <optional>
+ + + + + + + + + + false + + + @@ -1705,7 +1558,7 @@ and should have browsers specs on the bottom-laying layer

Source:
@@ -2340,6 +2193,177 @@ the OS called "anything" or the platform called "anything"satisfies(checkTree) → {Boolean|undefined} + + + + + + +
+ + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +
+

Check if parsed browser matches certain conditions

+
+ + + + + + + + + +
Example
+ +
const browser = new Bowser(UA);
+if (browser.check({chrome: '>118.01.1322' }))
+// or with os
+if (browser.check({windows: { chrome: '>118.01.1322' } }))
+// or with platforms
+if (browser.check({desktop: { chrome: '>118.01.1322' } }))
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
checkTree + + +Object + + + +

It's one or two layered object, +which can include a platform or an OS on the first layer +and should have browsers specs on the bottom-laying layer

+ + + + + + + + + + + + + + +
Returns:
+ + +
+

Whether the browser satisfies the set conditions or not. +Returns undefined when the browser is no described in the checkTree object.

+
+ + + +
+
+ Type +
+
+ +Boolean +| + +undefined + + +
+
+ + + + + + + + +

test(regex) → {Boolean}

@@ -2504,7 +2528,7 @@ the OS called "anything" or the platform called "anything"
- Documentation generated by JSDoc 3.5.5 on Thu Jul 05 2018 22:44:07 GMT+0300 (EEST) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Sun Jul 08 2018 12:09:12 GMT+0300 (EEST) using the docdash theme.
diff --git a/docs/bowser.js.html b/docs/bowser.js.html index 28cf928..5796c9c 100644 --- a/docs/bowser.js.html +++ b/docs/bowser.js.html @@ -22,7 +22,7 @@
@@ -79,7 +79,7 @@ class Bowser { * @return {ParsedResult} */ static parse(UA) { - return (new Bowser(UA)).getResult(); + return (new Parser(UA)).getResult(); } } @@ -96,7 +96,7 @@ export default Bowser;
- Documentation generated by JSDoc 3.5.5 on Thu Jul 05 2018 22:44:07 GMT+0300 (EEST) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Sun Jul 08 2018 12:09:12 GMT+0300 (EEST) using the docdash theme.
diff --git a/docs/global.html b/docs/global.html index f4036b4..84f748d 100644 --- a/docs/global.html +++ b/docs/global.html @@ -22,7 +22,7 @@
@@ -726,7 +726,7 @@
- Documentation generated by JSDoc 3.5.5 on Thu Jul 05 2018 22:44:07 GMT+0300 (EEST) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Sun Jul 08 2018 12:09:12 GMT+0300 (EEST) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index c3ceabf..11dc9a0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -22,7 +22,7 @@
@@ -45,6 +45,194 @@ +
+

Bowser

A Browser detector. Because sometimes, there is no other way, and not even good modern browsers always provide good feature detection mechanisms.

+

bowser ci

+

Contents

    +
  • Overview
  • +
  • Use cases
  • +
  • API
  • +
  • How can I help?
  • +
+

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

First of all, require the library:

+
const Bowser = require('bowser');

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);
+
+console.log(`The current browser name is "${browser.getBrowserName()}"`);
+// The current browser name is "Internet Explorer"

or

+
const impression = new Impression();
+
+const browser = new Bowser(window.navigator.userAgent);
+const browserInfo = browser.getBrowser();
+impression.brName = browserInfo.name;
+impression.brVer = browserInfo.version;

or

+
const browser = new Bowser(window.navigator.userAgent);
+impression.userTechData = browser.parse();
+console.log(impression.userTechData);
+// 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"
+  }
+}

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:

+
const browser = new Bowser(window.navigator.userAgent);
+const isValidBrowser = bowser.compare({
+  // declare browsers per OS
+  windows: {
+    "internet explorer": ">10",
+  },
+  macos: {
+    safari: ">10.1"
+  },
+
+  // per platform (mobile, desktop or tablet)
+  mobile: {
+    safari: '>9',
+    'android browser': '>3.10'
+  },
+
+  // or in general
+  chrome: ">20.1.1432",
+  firefox: ">31",
+  opera: ">22"
+});

As you can see, settings for any particular OS has more priority and redefines settings of any property.

+

new Bowser(:Object)

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.

+

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).

+
/**
+ * 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

bowser.compareVersions(versions:Array<String>):Number

Use it to compare two versions.

+
bowser.compareVersions(['9.0', '10']);
+// -1

bowser.isUnsupportedBrowser(minVersions:Object, [strictMode]:Boolean, [ua]:string):Boolean

Use it to check if browser is unsupported.

+
bowser.isUnsupportedBrowser({msie: "10"}, window.navigator.userAgent);
+// true / false

See more examples in tests.

+
+

Bowser Flags

Your mileage may vary, but these flags should be set. See Contributing below.

+
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'
  • +
+

For unknown browsers, Bowser makes a best guess from the UA string. So, these may not be set.

+

Rendering engine flags

If detected, one of these flags may be set to true:

+
    +
  • webkit - Chrome 0-27, Android <4.4, iOs, BB, etc.
  • +
  • blink - Chrome >=28, Android >=4.4, Opera, etc.
  • +
  • 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.

+
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
  • +
  • tablet - If a tablet device is detected, the flag tablet is set instead of mobile.
  • +
+

Browser flags

If detected, one of these flags may be set to true. The rendering engine flag is shown in []'s:

+
    +
  • chrome - [webkit|blink]
  • +
  • firefox - [gecko]
  • +
  • msie
  • +
  • msedge
  • +
  • safari - [webkit]
  • +
  • android - native browser - [webkit|blink]
  • +
  • ios - native browser - [webkit]
  • +
  • opera - [blink if >=15]
  • +
  • samsungBrowser - [blink]
  • +
  • phantom - [webkit]
  • +
  • blackberry - native browser - [webkit]
  • +
  • webos - native browser - [webkit]
  • +
  • silk - Amazon Kindle browser - [webkit]
  • +
  • bada - [webkit]
  • +
  • tizen - [webkit]
  • +
  • seamonkey - [gecko]
  • +
  • sailfish - [gecko]
  • +
  • ucbrowser — [webkit]
  • +
  • qupzilla — [webkit]
  • +
  • vivaldi — [blink]
  • +
  • sleipnir — [blink]
  • +
  • kMeleon — [gecko]
  • +
+

For all detected browsers the browser version is set in the version field.

+

OS Flags

If detected, one of these flags may be set to true:

+
    +
  • mac
  • +
  • windows - other than Windows Phone
  • +
  • windowsphone
  • +
  • linux - other than android, chromeos, webos, tizen, and sailfish
  • +
  • chromeos
  • +
  • android
  • +
  • ios - also sets one of iphone/ipad/ipod
  • +
  • blackberry
  • +
  • firefoxos
  • +
  • webos - may also set touchpad
  • +
  • bada
  • +
  • tizen
  • +
  • sailfish
  • +
+

osversion may also be set:

+
    +
  • osversion - for Android, iOS, MacOS, Windows, Windows Phone, WebOS, Bada, and Tizen. If included in UA string.
  • +
+

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.

+

Browser capability grading

One of these flags may be set:

+
    +
  • 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.
  • +
+

There is no b. For unknown browsers, none of these flags may be set.

+

Ender Support

package.json

+
"dependencies": {
+  "bowser": "x.x.x"
+}
if (require('bowser').chrome) {
+  alert('Hello Silicon Valley')
+}

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):

+
$ npm install
+$ make test

Please do not check-in the built files bowser.js and bowser.min.js in pull requests.

+

Adding tests

See the list in src/useragents.js with example user agents and their expected bowser object.

+

Whenever you add support for new browsers or notice a bug / mismatch, please update the list and +check if all tests are still passing.

+

Similar Projects

    +
  • Kong - A C# port of Bowser.
  • +
+

License

Licensed as MIT. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.

+
+ @@ -55,7 +243,7 @@
- Documentation generated by JSDoc 3.5.5 on Thu Jul 05 2018 22:44:07 GMT+0300 (EEST) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Sun Jul 08 2018 12:09:12 GMT+0300 (EEST) using the docdash theme.
diff --git a/docs/parser.js.html b/docs/parser.js.html index c8da09d..ecdb0a7 100644 --- a/docs/parser.js.html +++ b/docs/parser.js.html @@ -22,7 +22,7 @@
@@ -255,10 +255,10 @@ class Parser { /** * Get platform name - * @param {Boolean} toLowerCase + * @param {Boolean} [toLowerCase=false] * @return {*} */ - getPlatformType(toLowerCase) { + getPlatformType(toLowerCase = false) { const { type } = this.getPlatform(); if (toLowerCase) { @@ -360,7 +360,8 @@ class Parser { * which can include a platform or an OS on the first layer * and should have browsers specs on the bottom-laying layer * - * @returns {Boolean} whether the browser satisfies the set conditions or not + * @returns {Boolean|undefined} Whether the browser satisfies the set conditions or not. + * Returns `undefined` when the browser is no described in the checkTree object. * * @example * const browser = new Bowser(UA); @@ -370,25 +371,64 @@ class Parser { * // or with platforms * if (browser.check({desktop: { chrome: '>118.01.1322' } })) */ - compare(checkTree) { - const keysToProcess = Object.keys(checkTree); - return keysToProcess.some((browserAttribute) => { - const objectOrVersion = checkTree[browserAttribute]; - - if (typeof objectOrVersion === 'object') { - return (this.isOs(browserAttribute) || this.isPlatform(browserAttribute)) - && this.compare(objectOrVersion); + satisfies(checkTree) { + const platformsAndOSes = {}; + let platformsAndOSCounter = 0; + const browsers = {}; + let browsersCounter = 0; + + const allDefinitions = Object.keys(checkTree); + + allDefinitions.forEach((key) => { + const currentDefinition = checkTree[key]; + if (typeof currentDefinition === 'string') { + browsers[key] = currentDefinition; + browsersCounter += 1; + } else if (typeof currentDefinition === 'object') { + platformsAndOSes[key] = currentDefinition; + platformsAndOSCounter += 1; } - - return this.isBrowser(browserAttribute) && this.satisfies(objectOrVersion); }); + + if (platformsAndOSCounter > 0) { + const platformsAndOSNames = Object.keys(platformsAndOSes); + const OSMatchingDefinition = platformsAndOSNames.find(name => (this.isOS(name))); + + if (OSMatchingDefinition) { + const osResult = this.satisfies(platformsAndOSes[OSMatchingDefinition]); + + if (osResult !== void 0) { + return osResult; + } + } + + const platformMatchingDefinition = platformsAndOSNames.find(name => (this.isPlatform(name))); + if (platformMatchingDefinition) { + const platformResult = this.satisfies(platformsAndOSes[platformMatchingDefinition]); + + if (platformResult !== void 0) { + return platformResult; + } + } + } + + if (browsersCounter > 0) { + const browserNames = Object.keys(browsers); + const matchingDefinition = browserNames.find(name => (this.isBrowser(name))); + + if (matchingDefinition !== void 0) { + return this.compareVersion(browsers[matchingDefinition]); + } + } + + return undefined; } isBrowser(browserName) { return this.getBrowserName(true) === String(browserName).toLowerCase(); } - satisfies(version) { + compareVersion(version) { let expectedResult = 0; let comparableVersion = version; @@ -404,7 +444,7 @@ class Parser { return compareVersions(this.getBrowserVersion(), comparableVersion) === expectedResult; } - isOs(osName) { + isOS(osName) { return this.getOSName(true) === String(osName).toLowerCase(); } @@ -419,7 +459,7 @@ class Parser { * @returns {Boolean} */ is(anything) { - return this.isBrowser(anything) || this.isOs(anything) || this.isPlatform(anything); + return this.isBrowser(anything) || this.isOS(anything) || this.isPlatform(anything); } } @@ -436,7 +476,7 @@ export default Parser;
- Documentation generated by JSDoc 3.5.5 on Thu Jul 05 2018 22:44:07 GMT+0300 (EEST) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Sun Jul 08 2018 12:09:12 GMT+0300 (EEST) using the docdash theme.
diff --git a/docs/utils.js.html b/docs/utils.js.html index e2d5cf3..a6490dc 100644 --- a/docs/utils.js.html +++ b/docs/utils.js.html @@ -22,7 +22,7 @@
@@ -117,6 +117,7 @@ * @return {Number} comparison result: -1 when versionA is lower, * 1 when versionA is bigger, 0 when both equal */ + /* eslint consistent-return: 1 */ static compareVersions(versionA, versionB) { // 1) get common precision for both versions, for example for "10.0" and "9" it should be 2 let precision = Math.max( @@ -185,7 +186,7 @@ module.exports = Utils;
- Documentation generated by JSDoc 3.5.5 on Thu Jul 05 2018 22:44:07 GMT+0300 (EEST) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Sun Jul 08 2018 12:09:12 GMT+0300 (EEST) using the docdash theme.
diff --git a/jsdoc.json b/jsdoc.json index 278ce2b..3ab6b9a 100644 --- a/jsdoc.json +++ b/jsdoc.json @@ -3,7 +3,7 @@ "allowUnknownTags": true }, "source": { - "include": "src", + "include": ["src", "README.md"], "includePattern": ".js$", "excludePattern": "(node_modules/|docs)" },