-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (62 loc) · 1.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var useragents = require('./useragents.json')
function UseragentBrowserFamilies() {
var ret = []
for(var i in useragents.useragents)
{
var agent = useragents.useragents[i]
if (!ret.includes(agent.agent.family)) ret.push(agent.agent.family)
}
return ret
}
function UseragentByFamily(browserFamily,osFamily=false){
var amount = 0
if (!browserFamily) return false
for(var i in useragents.useragents)
amount+= (useragents.useragents[i].agent.family.toLowerCase()===browserFamily.toLowerCase() && (osFamily==false || useragents.useragents[i].agent.os.family.toLowerCase()===osFamily.toLowerCase())) ?1:0
if (amount===0) return null
var r = amount
while(r==amount)
r = parseInt((Math.random()*amount).toFixed(0))
var id = 0
for(var i in useragents.useragents)
{
var agent = useragents.useragents[i]
if (agent.agent.family.toLowerCase()===browserFamily.toLowerCase()
&& (osFamily==false || agent.agent.os.family.toLowerCase()===osFamily.toLowerCase())) {
// console.log(id,r)
if (id==r) {
agent.ua = i; return agent;
}
id++
}
}
return null
}
function randomUseragent(byPopularity=true) {
// Randomly
if (!byPopularity) {
var r = (Math.random()*Object.keys(useragents.useragents).length).toFixed(0)
var ky = Object.keys(useragents.useragents)[r]
var ua = useragents.useragents[ky]
ua.ua = ky
return ua
}
// byPopularity
var rnd = Math.random()*useragents.totalPopularity
var total = 0
for(var i in useragents.useragents) {
var agent = useragents.useragents[i]
if (rnd>total && rnd<=total+agent.popularity) {
agent.ua = i
return agent
}
total+=agent.popularity
}
return false
}
module.exports={
useragents,
randomUseragent,
UseragentByFamily,
UseragentBrowserFamilies
}