Smart user-agent parser for Node.
Extends useragent module and provides python-user-agents like functionality. Exposes custom defined functions like isMobile(), isTablet(), isPc()
npm install --save node-user-agents
yarn add node-user-agents
const UserAgent = require('node-user-agents');
const uaString = 'Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19';
const useragent = new UserAgent(uaString);
// logs true
console.log(useragent.isMobile());
While using this module you can still access useragent
module through useragent.userAgent
property, for example console.log(useragent.userAgent.family);
.
Also you can access user agent string via useragent.uaString
There are multiple ways to use this library. You can pass your user-agent string on
initialization or you can use ua.parse(uaString)
method for multiple user-agent
parsing operations.
const UserAgent = require('node-user-agents');
const useragent = new UserAgent();
useragent.parse(uaString); // same result as above
// logs true
console.log(useragent.isMobile());
You can provide an option to this library to change useragent' module behaviour.
- lookup: uses useragent.lookup instead of useragent.parse
- update: updates useragent database with latest version
const UserAgent = require('node-user-agents');
const useragent = new UserAgent(uaString, {lookup: true}); // now use userangets.lookup method
// logs true
console.log(useragent.isMobile());
You can pass your own useragent
instance with the last parameter in the constructor method.
This way instead of using the version shipped with this library you can use your
own modified or latest version of useragent
.
const myownUserAgentLibrary = require('useragent');
const UserAgent = require('node-user-agents');
const useragent = new UserAgent(uaString, {update: true}, myownUserAgentLibrary);
// logs true
console.log(useragent.isMobile());
This module provides these additional methods.
getBrowser()
Returns generic browser nameisTouchCapable()
Returns true if given device is touch capableisMobile()
isTablet()
isPc()
isBot()
getOs()
Returns OS String like Windows, Linux, Mac OS X, OpenBSD, Android, iOSisSmartTv()
getDevice()
Returns device category based on above methods like tablet, mobile, pc, bot