Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 0fa4cbb

Browse files
committed
Bug 1954645 - add test coverage for the webcompat addon's ua_helpers.js and improve getDeviceAppropriateChromeUA; r?denschub
1 parent cb81c2d commit 0fa4cbb

4 files changed

Lines changed: 459 additions & 25 deletions

File tree

src/injections/js/bug1724868-news.yahoo.co.jp-ua-override.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ console.info(
2020
"The user agent has been overridden for compatibility reasons. See https://webcompat.com/issues/82605 for details."
2121
);
2222

23-
UAHelpers.overrideWithDeviceAppropriateChromeUA({ desktopOS: "nonLinux" });
23+
UAHelpers.overrideWithDeviceAppropriateChromeUA({ OS: "nonLinux" });

src/lib/ua_helpers.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,53 @@ var UAHelpers = {
1515
return `FxQuantum/${UAHelpers.getRunningFirefoxVersion()} `;
1616
},
1717
getDeviceAppropriateChromeUA(config = {}) {
18-
const { version = "130.0.0.0", androidDevice, desktopOS } = config;
19-
const key = `${version}:${androidDevice}:${desktopOS}`;
20-
if (!UAHelpers._deviceAppropriateChromeUAs[key]) {
18+
let { androidVersion, version = "130.0.0.0", phone, tablet, OS } = config;
19+
const key = JSON.stringify(config);
20+
if (config.noCache || !UAHelpers._deviceAppropriateChromeUAs[key]) {
2121
const userAgent =
2222
config.ua ||
2323
(typeof navigator !== "undefined" ? navigator.userAgent : "");
2424
const fxQuantum = config.noFxQuantum
2525
? ""
2626
: UAHelpers.getFxQuantumSegment();
27-
if (userAgent.includes("Android")) {
28-
const RunningAndroidVersion =
29-
userAgent.match(/Android [0-9.]+/) || "Android 6.0";
30-
if (androidDevice) {
27+
const noOSGiven = !OS || OS === "nonLinux";
28+
if (OS === "android" || (noOSGiven && userAgent.includes("Android"))) {
29+
const AndroidVersion = androidVersion
30+
? `Android ${androidVersion}`
31+
: userAgent.match(/Android [0-9.]+/) || "Android 6.0";
32+
if (phone === undefined && tablet === undefined) {
33+
phone = userAgent.includes("Mobile");
34+
tablet = userAgent.includes("Tablet");
35+
}
36+
if (phone === true) {
37+
phone = "Nexus 5 Build/MRA58N";
38+
}
39+
if (tablet === true || (!tablet && !phone)) {
40+
tablet = "Nexus 7 Build/JSS15Q";
41+
}
42+
if (phone) {
3143
UAHelpers._deviceAppropriateChromeUAs[key] =
32-
`Mozilla/5.0 (Linux; ${RunningAndroidVersion}; ${androidDevice}) ${fxQuantum}AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Mobile Safari/537.36`;
44+
`Mozilla/5.0 (Linux; ${AndroidVersion}; ${phone}) ${fxQuantum}AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Mobile Safari/537.36`;
3345
} else {
34-
const ChromePhoneUA = `Mozilla/5.0 (Linux; ${RunningAndroidVersion}; Nexus 5 Build/MRA58N) ${fxQuantum}AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Mobile Safari/537.36`;
35-
const ChromeTabletUA = `Mozilla/5.0 (Linux; ${RunningAndroidVersion}; Nexus 7 Build/JSS15Q) ${fxQuantum}AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Safari/537.36`;
36-
const IsPhone = userAgent.includes("Mobile");
37-
UAHelpers._deviceAppropriateChromeUAs[key] = IsPhone
38-
? ChromePhoneUA
39-
: ChromeTabletUA;
46+
UAHelpers._deviceAppropriateChromeUAs[key] =
47+
`Mozilla/5.0 (Linux; ${AndroidVersion}; ${tablet}) ${fxQuantum}AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Safari/537.36`;
4048
}
4149
} else {
42-
let osSegment = "Windows NT 10.0; Win64; x64";
43-
if (desktopOS === "macOS" || userAgent.includes("Macintosh")) {
50+
const WIN_SEGMENT = "Windows NT 10.0; Win64; x64";
51+
let osSegment;
52+
if (OS === "macOS" || (noOSGiven && userAgent.includes("Macintosh"))) {
4453
osSegment = "Macintosh; Intel Mac OS X 10_15_7";
45-
}
46-
if (
47-
desktopOS !== "nonLinux" &&
48-
(desktopOS === "linux" || userAgent.includes("Linux"))
54+
} else if (
55+
OS === "linux" ||
56+
(noOSGiven && userAgent.includes("Linux"))
4957
) {
50-
osSegment = "X11; Ubuntu; Linux x86_64";
58+
if (OS !== "nonLinux") {
59+
osSegment = "X11; Ubuntu; Linux x86_64";
60+
} else {
61+
osSegment = WIN_SEGMENT;
62+
}
63+
} else {
64+
osSegment = WIN_SEGMENT;
5165
}
5266

5367
UAHelpers._deviceAppropriateChromeUAs[key] =
@@ -110,9 +124,6 @@ var UAHelpers = {
110124

111125
return ua;
112126
},
113-
firefoxPlusChrome(ua = navigator.userAgent, version = "130.0.0.0") {
114-
return `${ua} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Mobile Safari/537.36`;
115-
},
116127
getPrefix(originalUA) {
117128
return originalUA.substr(0, originalUA.indexOf(")") + 1);
118129
},

src/tests/browser/browser.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ https_first_disabled = true
2121
skip-if = ["verify"]
2222

2323
["browser_smartblockembeds.js"]
24+
25+
["browser_ua_helpers.js"]

0 commit comments

Comments
 (0)