This is an extremely complicated and almost completely useless browser sniffing library written in JavaScript. Useless because you shouldn't use browser sniffing. So stop right now and go read something about feature detecting instead. I'm serious. Go away. You'll thank me later.
If you are looking for the PHP version of WhichBrowser, please go to the WhichBrowser/Parser-PHP project.
If you are looking for the server written in PHP that provides a Javascript API for the browser, please go to the WhichBrowser/Server project.
But why almost completely useless and not completely useless?
Well, there is always an exception to the rule. There are valid reasons to do browser sniffing: to improve the user experience or to gather intelligence about which browsers are used on your website. The original author website is html5test.com and he wanted to know which score belongs to which browser. And to do that you need a browser sniffing library.
Why is it extremely complicated?
Because everybody lies. Seriously, there is not a single browser that is completely truthful. Almost all browsers say they are Netscape 5 and almost all WebKit browsers say they are based on Gecko. Even Internet Explorer 11 now no longer claims to be IE at all, but instead an unnamed browser that is like Gecko. And it gets worse. That is why it is complicated.
What kind of information does it give? You get a nice object which has information about the browser, rendering engine, os and device. It gives you names and versions and even device manufacturer and model. And WhichBrowser is pretty tenacious. It gives you info that others don't. For example:
JUC (Linux; U; 2.3.6; zh-cn; GT-I8150; 480*800) UCWEB8.7.4.225/145/800
UC Browser 8.7 on a Samsung Galaxy W running Android 2.3.6
Android is never mentioned
Mozilla/5.0 (Series40; Nokia501/10.0.2; Profile/MIDP-2.1 Configuration/CLDC-1.1) Gecko/20100401 S40OviBrowser/3.0.0.0.73
Nokia Xpress 3.0.0 on a Nokia Asha 501 running Nokia Asha Platform
Despite the useragent header claiming to be a Series40 device, we know it's actually running the Asha Platform and we also know that OviBrowser has been renamed to Nokia Xpress.
Opera/9.80 (X11; Linux zvav; U; zh) Presto/2.8.119 Version/11.10
Opera Mini on a Nokia 5230 running Series60 5.0
The useragent header looks like Opera 11.10 on Linux, but we know it's Opera Mini. We can even figure out the real operating system and device model from other headers.
WhichBrowser requires node 6 or higher.
WhichBrowser has no dependencies, is just plain ES6.
You can install WhichBrowser by NPM and Yarn
npm install which-browser
or
yarn add which-browser
Do not stick to a specific WhichBrowser version, otherwise you will miss all the sniffing engine updates
const WhichBrowser = require('which-browser');
const http = require('http');
const port = 3000;
const server = http.createServer((request, response) => {
const result = new WhichBrowser(request.headers)
response.end(result.toString())
});
server.listen(port, (err) => {
if (err) {
return console.log('Something is broken ¯\\_(ツ)_/¯', err)
}
console.log(`Server is listening on port ${port} ✌(-‿-)✌`)
})The first step require WhichBrowser:
const WhichBrowser = require('which-browser');The second step is to create a new instance of the WhichBrowser class. This object will contain all the information the library could find about the browser. The object has a required parameter, either the headers send by the browser, or a useragent string. Using the request headers is preferable, because it will allow a better detection, but if you have just the useragent string, this will also work.
For example:
const result = new WhichBrowser(request.headers);or:
const result = new WhichBrowser(request.headers['user-agent']));The variable result now contains an object which you can query for information. There are various ways to access the information.
First of all, you can call to toString() function to get a human readable identification:
`You are using ${result.toString()}`;
// You are using Chrome 27 on OS X Mountain Lion 10.8Another possiblity is to query the object:
result.isType('desktop');
// true
result.isType('mobile', 'tablet', 'media', 'gaming:portable');
// false
result.isBrowser('Maxthon', '<', '4.0.5');
// false
result.isOs('iOS', '>=', '8');
// false
result.isOs('OS X');
// true
result.isEngine('Blink');
// trueYou can also access these properties directly:
result.browser.toString();
// Chrome 27
result.engine.toString(