skratchdot / random-useragent

Get a random user agent (with an optional filter to select from a specific set of user agents).
MIT License
253 stars 37 forks source link

UA mobile #4

Closed kripul closed 8 years ago

kripul commented 8 years ago

Cant I generate random UA for mobile?

skratchdot commented 8 years ago

@kripul - Yes you can.

Here's an example: https://runkit.com/57700072b1bc291300690032/58028c2e82bd9d0014eef993

var random_useragent = require('random-useragent');
var result = random_useragent.getRandom((ua) => ua.deviceType === 'mobile');
console.log(result);

You can make the filter as specific or loose as you want (for instance, only returning iphones or androids, etc.

Change the getRandom() to getRandomData() to see what all is available.

kripul commented 8 years ago

Is avaible only for mobile? I try with 'desktop' give me null result.

skratchdot commented 8 years ago

@kripul - currently I'm using ua-parser-js to provide some extra info, so you can filter on data it returns. From that lib's readme: Possible 'device.type': console, mobile, tablet, smarttv, wearable, embedded, so there is not "desktop" available.

I might add the data from the following lib as well: useragent-parser-js, but that would increase the size of my data file.

To better answer your question, all this library does is pick a random value from: https://github.com/skratchdot/random-useragent/blob/master/useragent-data.json

So you could write a filter that looks something like this:

randomUseragent.getRandom((ua) => {
  return [
    '/Browsers - Mac',
    '/Browsers - Windows'
  ].indexOf(ua.folder) === 0
});