diff --git a/src/js/utils.js b/src/js/utils.js index c72343bd..8652c7c9 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -160,6 +160,16 @@ export function detectBrowser(window) { const {navigator} = window; + // Prefer navigator.userAgentData. + if (navigator.userAgentData && navigator.userAgentData.brands) { + const chromium = navigator.userAgentData.brands.find((brand) => { + return brand.brand === 'Chromium'; + }); + if (chromium) { + return {browser: 'chrome', version: chromium.version}; + } + } + if (navigator.mozGetUserMedia) { // Firefox. result.browser = 'firefox'; result.version = extractVersion(navigator.userAgent, diff --git a/test/unit/detectBrowser.js b/test/unit/detectBrowser.js index c6a5266c..508facde 100644 --- a/test/unit/detectBrowser.js +++ b/test/unit/detectBrowser.js @@ -53,6 +53,18 @@ describe('detectBrowser', () => { expect(browserDetails.version).to.equal(95); }); + it('detects Chrome if navigator.userAgentData exist', () => { + navigator.userAgentData = {brands: [{brand: 'Chromium', version: 102}]}; + // Use the wrong UA string for Firefox. + navigator.userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; ' + + 'rv:44.0) Gecko/20100101 Firefox/44.0'; + navigator.mozGetUserMedia = function() {}; + + const browserDetails = detectBrowser(window); + expect(browserDetails.browser).to.equal('chrome'); + expect(browserDetails.version).to.equal(102); + }); + it('detects Safari if window.RTCPeerConnection exists', () => { navigator.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) ' + 'AppleWebKit/604.1.6 (KHTML, like Gecko) Version/10.2 Safari/604.1.6';