Created
March 21, 2020 23:00
-
-
Save mikehwagz/f96eb8b833ddc361e67ac90bf568c1eb 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
import { DetectUA } from 'detect-ua' | |
export default function sniffer() { | |
const ua = navigator.userAgent.toLowerCase() | |
const { | |
browser, | |
isMobile, | |
isTablet, | |
isDesktop, | |
isMacOS, | |
isWindows, | |
isiOS, | |
isAndroid, | |
} = new DetectUA(ua) | |
const isFb = checkFb() | |
const isIg = checkIg() | |
const isTwitter = checkTwitter() | |
const isPinterest = checkPinterest() | |
const isApp = isFb || isIg || isTwitter || isPinterest | |
const device = { | |
browser, | |
isMobile, | |
isTablet, | |
isDesktop, | |
isMacOS, | |
isWindows, | |
isiOS, | |
isAndroid, | |
isFb, | |
isIg, | |
isTwitter, | |
isPinterest, | |
isApp, | |
} | |
return { | |
device, | |
addClasses(el = document.documentElement) { | |
const entries = Object.entries(device) | |
const classnames = [] | |
for (let i = 0; i < entries.length; i++) { | |
const [key, val] = entries[i] | |
val && | |
classnames.push( | |
`is-${(key === 'browser' ? val.name : key.slice(2)).toLowerCase()}`, | |
) | |
} | |
el.classList.add(...classnames) | |
}, | |
} | |
function checkFb() { | |
return ua.indexOf('fban') > -1 || ua.indexOf('fbav') > -1 | |
} | |
function checkIg() { | |
return ua.indexOf('instagram') > -1 | |
} | |
function checkTwitter() { | |
return ua.indexOf('twitter') > -1 | |
} | |
function checkPinterest() { | |
return ua.indexOf('pinterest') > -1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment