Skip to content

Instantly share code, notes, and snippets.

@alekstar79
Last active June 25, 2023 17:08
Show Gist options
  • Save alekstar79/70d647e4392956d7df00c94ee95ef38f to your computer and use it in GitHub Desktop.
Save alekstar79/70d647e4392956d7df00c94ee95ef38f to your computer and use it in GitHub Desktop.
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