Created
February 1, 2021 13:38
-
-
Save audunolsen/e6303a3ef0dc47e530f3f83cab954551 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Resources for regular expressions: | |
// github.com/DamonOehlman/detect-browser/blob/master/src/index.ts | |
// https://github.com/faisalman/ua-parser-js/blob/master/src/ua-parser.js | |
const browsers = [ | |
['edge', /Edge\/([0-9\._]+)/], | |
['edge-ios', /EdgiOS\/([0-9\._]+)/], | |
['samsung', /SamsungBrowser\/([0-9\.]+)/], | |
['edge-chromium', /EdgA?\/([0-9\.]+)/], | |
['chrome', /(?!Chrom.*OPR)Chrom(?:e|ium)\/([0-9\.]+)(:?\s|$)/], | |
['firefox', /Firefox\/([0-9\.]+)(?:\s|$)/], | |
['fxios', /FxiOS\/([0-9\.]+)/], | |
['android', /Android\s([0-9\.]+)/], | |
['ios', /Version\/([0-9\._]+).*Mobile.*Safari.*/], | |
['safari', /Version\/([0-9\._]+).*Safari/], | |
['ios-webview', /AppleWebKit\/([0-9\.]+).*Mobile/], | |
['ios-webview', /AppleWebKit\/([0-9\.]+).*Gecko\)$/], | |
]; | |
const operatingSystems = [ | |
['macOS', /(Mac_PowerPC)|(Macintosh)/], | |
['iOS', /iP(hone|od|ad)/], | |
['Android OS', /Android/], | |
['Windows Mobile', /IEMobile/], | |
['Windows', /Windows/], | |
['Chrome OS', /CrOS/], | |
['Linux', /(Linux)|(X11)/], | |
]; | |
export default () => { | |
const ua = navigator?.userAgent || ''; | |
const check = rules => (rules.find(r => r[1].test(ua)) || [])[0]; | |
return { | |
browser: check(browsers), | |
os: check(operatingSystems) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment