Last active
June 25, 2023 17:08
-
-
Save alekstar79/70d647e4392956d7df00c94ee95ef38f to your computer and use it in GitHub Desktop.
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 getUserAgent({ brands = [] }) | |
{ | |
let agent | |
test: for (const { brand, version } of brands) switch (true) { | |
case ['Chromium','Google Chrome','CriOS'].includes(brand): | |
agent = `Chrome/${version}` | |
break | |
case ['Firefox'].includes(brand): | |
agent = `Firefox/${version}` | |
break test | |
case ['Opera', 'OPR'].includes(brand): | |
agent = `Opera/${version}` | |
break test | |
case ['Microsoft Edge','Edge'].includes(brand): | |
agent = `Edge/${version}` | |
break test | |
case ['Safari'].includes(brand): | |
agent = `Safari/${version}` | |
break test | |
} | |
return agent | |
} | |
export function detectUserAgent() | |
{ | |
if (window.navigator.userAgentData) { | |
return getUserAgent(window.navigator.userAgentData) | |
} | |
return window.navigator.userAgent || '' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment