Skip to content

Commit

Permalink
prefer navigator.userAgentData for browser detection
Browse files Browse the repository at this point in the history
fixes #764,#1017
  • Loading branch information
fippo committed Jun 13, 2022
1 parent 95d813f commit ac9c676
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions test/unit/detectBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit ac9c676

Please sign in to comment.