Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer navigator.userAgentData for browser detection #1146

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ export function detectBrowser(window) {

const {navigator} = window;

// Prefer navigator.userAgentData.
if (navigator.userAgentData && navigator.userAgentData.brands) {
fippo marked this conversation as resolved.
Show resolved Hide resolved
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.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ describe('detectBrowser', () => {
expect(browserDetails.version).toEqual(95);
});

it('detects Chrome if navigator.userAgentData exists', () => {
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).toEqual('chrome');
expect(browserDetails.version).toEqual(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