Created
July 27, 2020 19:54
-
-
Save matthewpizza/2c62c4a94a4e957f506f55e2694780f7 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
const browserName = { | |
browsers: [ | |
'Chrome', | |
'Firefox', | |
'Opera', | |
'Safari', | |
'MobileChrome', | |
'MobileSafari', | |
], | |
toString() { | |
const browser = this.browsers.find(browser => this[`is${browser}`]()); | |
if (!browser) { | |
return 'Unknown'; | |
} | |
return browser; | |
}, | |
isChrome() { | |
const { navigator: { userAgent, vendor } } = window; | |
return ( | |
userAgent.includes('Chrome') | |
&& !this.isOpera() | |
&& vendor === 'Google Inc.' | |
); | |
}, | |
isFirefox() { | |
const { navigator: { userAgent, vendor } } = window; | |
return ( | |
userAgent.includes('Firefox') | |
&& !userAgent.includes('like Gecko') | |
&& !vendor.length | |
); | |
}, | |
isOpera() { | |
const { navigator: { userAgent } } = window; | |
return ( | |
userAgent.includes('OPR') | |
); | |
}, | |
isSafari() { | |
const { navigator: { userAgent, vendor } } = window; | |
return ( | |
userAgent.includes('Safari') | |
&& !this.isChrome() | |
&& vendor === 'Apple Computer, Inc.' | |
) | |
}, | |
isMobileChrome() { | |
const { navigator: { userAgent } } = window; | |
return ( | |
// TODO: This is only for iOS | |
userAgent.includes('CriOS') | |
); | |
}, | |
isMobileSafari() { | |
const { navigator: { userAgent } } = window; | |
return ( | |
this.isSafari() | |
&& userAgent.includes('Mobile') | |
&& !this.isMobileChrome() | |
); | |
}, | |
}; | |
export default browserName; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment