Skip to content

Instantly share code, notes, and snippets.

@matthewpizza
Created July 27, 2020 19:54
Show Gist options
  • Save matthewpizza/2c62c4a94a4e957f506f55e2694780f7 to your computer and use it in GitHub Desktop.
Save matthewpizza/2c62c4a94a4e957f506f55e2694780f7 to your computer and use it in GitHub Desktop.
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