Skip to content

Latest commit

 

History

History
93 lines (66 loc) · 2.89 KB

README.md

File metadata and controls

93 lines (66 loc) · 2.89 KB

npm version Build Status

Node User Agents

Smart user-agent parser for Node.

Extends useragent module and provides python-user-agents like functionality. Exposes custom defined functions like isMobile(), isTablet(), isPc()

Installation:

npm install --save node-user-agents
yarn add node-user-agents

Usage:

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());

Options

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());

Your own useragentLib

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());

Api:

This module provides these additional methods.

  • getBrowser() Returns generic browser name
  • isTouchCapable() Returns true if given device is touch capable
  • isMobile()
  • isTablet()
  • isPc()
  • isBot()
  • getOs() Returns OS String like Windows, Linux, Mac OS X, OpenBSD, Android, iOS
  • isSmartTv()
  • getDevice() Returns device category based on above methods like tablet, mobile, pc, bot