index.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import * as cheerio from 'cheerio';
  2. import * as fs from 'fs';
  3. const request = await fetch('https://www.useragents.me');
  4. const document = await request.text();
  5. const $ = cheerio.load(document);
  6. const desktopRegex = new RegExp('^Mozilla\\/5\\.0 \\((Windows NT 1|Macintosh|X11).*$');
  7. const mobileRegex = new RegExp('^Mozilla\\/5\\.0 \\((iPhone|iPad|Android|Linux; Android).*$');
  8. const desktopUserAgents = [];
  9. const mobileUserAgents = [];
  10. $('.ua-textarea').map((_index, el) => {
  11. const userAgent = $(el).text().trim();
  12. if (!userAgent.startsWith('Mozilla/5.0 (')) {
  13. return;
  14. }
  15. if (desktopRegex.test(userAgent)) {
  16. desktopUserAgents.push(userAgent);
  17. } else if (mobileRegex.test(userAgent)) {
  18. mobileUserAgents.push(userAgent)
  19. }
  20. });
  21. const userAgents = {
  22. recommended: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
  23. desktop: desktopUserAgents.sort(),
  24. mobile: mobileUserAgents.sort(),
  25. };
  26. const buildDir = './build';
  27. if (!fs.existsSync(buildDir)) {
  28. fs.mkdirSync(buildDir);
  29. }
  30. fs.writeFileSync(`${buildDir}/user-agents.json`, JSON.stringify(userAgents, null, 2));
  31. fs.writeFileSync(`${buildDir}/user-agents.min.json`, JSON.stringify(userAgents));