random-useragent
Version:
Get a random useragent (with an optional filter)
122 lines (85 loc) • 3.53 kB
Markdown
//badge.fury.io/js/random-useragent.svg)](http://badge.fury.io/js/random-useragent)
[](https://travis-ci.org/skratchdot/random-useragent)
[](https://codeclimate.com/github/skratchdot/random-useragent)
[](https://coveralls.io/github/skratchdot/random-useragent?branch=master)
[](https://david-dm.org/skratchdot/random-useragent)
[](https://david-dm.org/skratchdot/random-useragent#info=devDependencies)
[](https://npmjs.org/package/random-useragent)
Get a random user agent (with an optional filter to select from a specific set of user agents).
## Getting Started
Install the module with: `npm install random-useragent`
```javascript
const randomUseragent = require('random-useragent');
randomUseragent.getRandom(); // gets a random user agent string
```
- [Live example on Tonic](https://tonicdev.com/npm/random-useragent)
Get a random user agent string (optionally using a filter).
Example Result:
```javascript
'Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20121202 Firefox/20.0'
```
#### .getRandomData(filter)
Get a random user agent's parsed data (optionally using a filter).
Example Result:
```javascript
{
"folder": "/Browsers - Windows/Legacy Browsers",
"description": "Firefox 20.0 (Win 8 32)",
"userAgent": "Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20121202 Firefox/20.0",
"appCodename": "",
"appName": "",
"appVersion": "",
"platform": "",
"vendor": "",
"vendorSub": "",
"browserName": "Firefox",
"browserMajor": "20",
"browserVersion": "20.0",
"deviceModel": "",
"deviceType": "",
"deviceVendor": "",
"engineName": "Gecko",
"engineVersion": "20.0",
"osName": "Windows",
"osVersion": "8",
"cpuArchitecture": ""
}
```
Get an array of all the user agent strings (optionally using a filter).
#### .getAllData(filter)
Get an array of all the parsed user agent data (optionally using a filter).
## Examples
Get a random user agent string:
```javascript
randomUseragent.getRandom();
```
Get a random Firefox user agent string:
```javascript
randomUseragent.getRandom(function (ua) {
return ua.browserName === 'Firefox';
});
```
Get a random user agent with a version >= 20:
```javascript
randomUseragent.getRandom(function (ua) {
return parseFloat(ua.browserVersion) >= 20;
});
```
The collection of user agents is pulled from the large, regularly updated xml file
provided by the author of [User Agent Switcher](http://chrispederick.com/work/user-agent-switcher/),
which is located here:
- [Forum Post](http://techpatterns.com/forums/about304.html)
- [Direct Link](http://techpatterns.com/downloads/firefox/useragentswitcher.xml)
- [Source Code](https://github.com/skratchdot/random-useragent)
- [Changelog](https://github.com/skratchdot/random-useragent/blob/master/CHANGELOG.md)
- [Live example on Tonic](https://tonicdev.com/npm/random-useragent)
Copyright (c) 2014 skratchdot
Licensed under the MIT license.
[![NPM version](https: