forked from keiyoushi/user-agents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
39 lines (31 loc) · 1.21 KB
/
index.mjs
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
import * as cheerio from 'cheerio';
import * as fs from 'fs';
const request = await fetch('https://www.useragents.me');
const document = await request.text();
const $ = cheerio.load(document);
const desktopRegex = new RegExp('^Mozilla\\/5\\.0 \\((Windows NT 1|Macintosh|X11).*$');
const mobileRegex = new RegExp('^Mozilla\\/5\\.0 \\((iPhone|iPad|Android|Linux; Android).*$');
const desktopUserAgents = [];
const mobileUserAgents = [];
$('.ua-textarea').map((_index, el) => {
const userAgent = $(el).text().trim();
if (!userAgent.startsWith('Mozilla/5.0 (')) {
return;
}
if (desktopRegex.test(userAgent)) {
desktopUserAgents.push(userAgent);
} else if (mobileRegex.test(userAgent)) {
mobileUserAgents.push(userAgent)
}
});
const userAgents = {
recommended: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
desktop: desktopUserAgents.sort(),
mobile: mobileUserAgents.sort(),
};
const buildDir = './build';
if (!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir);
}
fs.writeFileSync(`${buildDir}/user-agents.json`, JSON.stringify(userAgents, null, 2));
fs.writeFileSync(`${buildDir}/user-agents.min.json`, JSON.stringify(userAgents));