JavaScript User Agents API to fetch and filter by browser class type [closed]

一曲冷凌霜 提交于 2019-12-24 00:52:01

问题


Is there any user agent retrieval API? So that to filter by browser class type, like mobile, desktop, bot, etc. From this web site - that claims to have the largest database of User Agents, being collected since 2006, I wrote this simple tool to get User Agents from it (via the browser at this time):

function UserAgents () {
 var COL_UA=1; // for search results use 2
 var COL_CLASS=3; // for search results use 4
 this.list=[];
 this.Class='';
 this.parse = function(Class) {
  this.list=[];
  var self=this;
  this.Class=Class;
  $('tr').each(function(index,item) {
    var td=$(this).find('td');
    var uac=$( td[COL_CLASS] ).text();
    var ua = $( td[COL_UA] ).text()
    var regex = new RegExp(Class);
    if( regex.test(uac) ) { self.list.push(ua); }
  })
 }
 this.download = function() {
  if(this.list.length==0) return;
  var pp = document.createElement('a');
  pp.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify( this.list )));
  pp.setAttribute('download', "useragents_" + this.Class + ".txt");
  pp.click();
}
}

So from the console it's possible to query mobile user agents:

var ua=new UserAgents();
ua.parse('mobil');
ua.list
Array[400]
"Mozilla/5.0 (Linux; Android 5.0; LG-D855 Build/LRX21R.A1450702344; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/47.0.2526.100 Mobile Safari/537.36 ACHEETAHI/2100502020"
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"Mozilla/5.0 (Linux; Android 4.4.2; D5103 Build/18.1.A.1.23) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.95 Mobile Safari/537.36"

and for desktop user agents:

ua.list
Array[200]
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36 OPR/34.0.2036.50 (Edition Campaign 09)"
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 AOL/9.8 AOLBuild/4346.2019.US Safari/537.36"
ua.list[ Math.floor(Math.random() * ua.list.length) ]

or both mobile and desktop:

ua.parse('mobil|desktop')
ua.list
Array[600]

you can get bot user agents:

ua.parse('bot')
ua.list
Array[400]
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"baiduSpider"
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"Mozilla/5.0 (Android; Tablet; rv:10.0.4) Gecko/10.0.4 Firefox/10.0.4 Fennec/10.0.4 slurp"
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"Mozilla/5.0 (Macintosh; Intel Mac OS X) Excel/14.59.0"
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"Mozilla/5.0 (Windows Phone 8.0; Trident/7.0; rv:11.0; IEMobile/11.0; ARM; Touch; NOKIA; Nokia) like Gecko BingPreview/1.0b"
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"Image Hunter 2.5.0 (iPad; iPhone OS 6.1.3; en_US)"
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Fetcher/0.1)"
ua.list[ Math.floor(Math.random() * ua.list.length) ]
"yacybot (/global; amd64 Linux 2.6.32-042stab108.2; java 1.7.0_91; America/en) http://yacy.net/bot.html"

As soon you parse a class type, you can grab them to file:

ua.download()

This will download a file named useragents_classtype.txt locally.

Is there any other way to do this?

来源:https://stackoverflow.com/questions/36510058/javascript-user-agents-api-to-fetch-and-filter-by-browser-class-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!