Created
October 21, 2019 13:23
-
-
Save waghcwb/b901f84d89d8eebfe0326f38390c03f8 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
/** | |
* This method is used to detect the user browser and environment | |
*/ | |
establishBrowser() { | |
const userAgent = (navigator && navigator.userAgent || '').toLowerCase() | |
const vendor = (navigator && navigator.vendor || '').toLowerCase() | |
const comparator = { | |
'<': function(a, b) { return a < b; }, | |
'<=': function(a, b) { return a <= b; }, | |
'>': function(a, b) { return a > b; }, | |
'>=': function(a, b) { return a >= b; } | |
}; | |
const compareVersion = (version, range) => { | |
const str = (range + ''); | |
const n = +(str.match(/\d+/) || NaN); | |
const op = str.match(/^[<>]=?|/)[0]; | |
return comparator[op] ? comparator[op](version, n) : (version == n || n !== n); | |
}; | |
this.browser = { | |
userAgent, | |
vendor, | |
}; | |
// detect opera | |
this.browser.isOpera = function isOpera(range) { | |
const match = userAgent.match(/(?:^opera.+?version|opr)\/(\d+)/); | |
return match !== null && compareVersion(match[1], range); | |
} | |
// detect chrome | |
this.browser.isChrome = function isChrome(range) { | |
const match = /google inc/.test(vendor) ? userAgent.match(/(?:chrome|crios)\/(\d+)/) : null; | |
return match !== null && !this.isOpera() && compareVersion(match[1], range); | |
} | |
// detect firefox | |
this.browser.isFirefox = function isFirefox(range) { | |
const match = userAgent.match(/(?:firefox|fxios)\/(\d+)/); | |
return match !== null && compareVersion(match[1], range); | |
}; | |
// detect safari | |
this.browser.isSafari = function isSafari(range) { | |
const match = userAgent.match(/version\/(\d+).+?safari/); | |
return match !== null && compareVersion(match[1], range); | |
}; | |
// detect internet explorer | |
this.browser.isIE = function isIE(range) { | |
const match = userAgent.match(/(?:msie |trident.+?; rv:)(\d+)/); | |
return match !== null && compareVersion(match[1], range); | |
}; | |
// detect edge | |
this.browser.isEdge = function isEdge(range) { | |
const match = userAgent.match(/edge\/(\d+)/); | |
return match !== null && compareVersion(match[1], range); | |
}; | |
// detect blink engine | |
this.browser.isBlink = function isBlink() { | |
return (this.isChrome() || this.isOpera()) && !!window.CSS; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment