Created
February 26, 2025 16:43
-
-
Save scripting/e35256108b5c9ebf6af4cea057c273ef to your computer and use it in GitHub Desktop.
I'm interested in knowing what browsers people who use WordLand are using, so I asked ChatGPT to write some code that makes sense of them in some human-readable way. This is what it came up with. Interested in knowing what people think? I imagine there are packages that do this but I haven't looked yet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseUserAgent(uaString) { | |
let browser = "Unknown Browser", version = "?", os = "Unknown OS"; | |
// OS Detection | |
if (/Windows NT/i.test(uaString)) { | |
os = "Windows"; | |
} else if (/Macintosh/i.test(uaString)) { | |
os = "Mac"; | |
} else if (/Linux/i.test(uaString)) { | |
os = "Linux"; | |
} else if (/Android/i.test(uaString)) { | |
os = "Android"; | |
} else if (/iPhone|iPad/i.test(uaString)) { | |
os = "iOS"; | |
} | |
// Browser Detection with Regular Expressions | |
const browserRegexes = [ | |
{ name: "Brave", regex: /(Chrome)\/([\d.]+)/, checkBrave: true }, // Brave mimics Chrome | |
{ name: "Edge", regex: /(Edg|Edge)\/([\d.]+)/ }, | |
{ name: "Vivaldi", regex: /(Vivaldi)\/([\d.]+)/ }, | |
{ name: "Opera", regex: /(OPR|Opera)\/([\d.]+)/ }, | |
{ name: "Samsung Internet", regex: /(SamsungBrowser)\/([\d.]+)/ }, | |
{ name: "Firefox", regex: /(Firefox)\/([\d.]+)/ }, | |
{ name: "Safari", regex: /(Version)\/([\d.]+).*Safari/ }, | |
{ name: "Chrome", regex: /(Chrome)\/([\d.]+)/ }, | |
]; | |
for (let { name, regex, checkBrave } of browserRegexes) { | |
let match = uaString.match(regex); | |
if (match) { | |
browser = name; | |
version = match[2].split('.')[0]; // Extract major version | |
// Special check for Brave (it disguises itself as Chrome) | |
if (checkBrave && typeof navigator !== "undefined" && navigator.brave) { | |
browser = "Brave"; | |
} | |
break; | |
} | |
} | |
return `${browser} version ${version} on ${os}`; | |
} | |
// Test Cases | |
const uaList = [ | |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36", | |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Edge/90.0.818.62", | |
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/88.0", | |
"Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Version/15.0 Mobile Safari/537.36", | |
"Mozilla/5.0 (Linux; Android 11; SM-G998U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36", | |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Vivaldi/4.1.2369.11 Chrome/92.0.4515.107 Safari/537.36", | |
"Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-G970F) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/14.0 Chrome/87.0.4280.141 Mobile Safari/537.36", | |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) OPR/80.0.4170.72 Safari/537.36" | |
]; | |
uaList.forEach(ua => console.log(parseUserAgent(ua))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Might be interesting to see the bots/crawlers/ai agents etc too... I had to ban BTWebClient from one app as the video downloads from there crashed our server. https://darkvisitors.com/agents/btwebclient