diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /browser/extensions/webcompat/lib/ua_helpers.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/extensions/webcompat/lib/ua_helpers.js')
-rw-r--r-- | browser/extensions/webcompat/lib/ua_helpers.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/browser/extensions/webcompat/lib/ua_helpers.js b/browser/extensions/webcompat/lib/ua_helpers.js new file mode 100644 index 0000000000..2cc848f8b0 --- /dev/null +++ b/browser/extensions/webcompat/lib/ua_helpers.js @@ -0,0 +1,99 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +/* globals exportFunction, module */ + +var UAHelpers = { + _deviceAppropriateChromeUAs: {}, + getDeviceAppropriateChromeUA(config = {}) { + const { version = "103.0.5060.71", androidDevice, desktopOS } = config; + const key = `${version}:${androidDevice}:${desktopOS}`; + if (!UAHelpers._deviceAppropriateChromeUAs[key]) { + const userAgent = + typeof navigator !== "undefined" ? navigator.userAgent : ""; + const RunningFirefoxVersion = (userAgent.match(/Firefox\/([0-9.]+)/) || [ + "", + "58.0", + ])[1]; + + if (userAgent.includes("Android")) { + const RunningAndroidVersion = + userAgent.match(/Android [0-9.]+/) || "Android 6.0"; + if (androidDevice) { + UAHelpers._deviceAppropriateChromeUAs[ + key + ] = `Mozilla/5.0 (Linux; ${RunningAndroidVersion}; ${androidDevice}) FxQuantum/${RunningFirefoxVersion} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Mobile Safari/537.36`; + } else { + const ChromePhoneUA = `Mozilla/5.0 (Linux; ${RunningAndroidVersion}; Nexus 5 Build/MRA58N) FxQuantum/${RunningFirefoxVersion} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Mobile Safari/537.36`; + const ChromeTabletUA = `Mozilla/5.0 (Linux; ${RunningAndroidVersion}; Nexus 7 Build/JSS15Q) FxQuantum/${RunningFirefoxVersion} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Safari/537.36`; + const IsPhone = userAgent.includes("Mobile"); + UAHelpers._deviceAppropriateChromeUAs[key] = IsPhone + ? ChromePhoneUA + : ChromeTabletUA; + } + } else { + let osSegment = "Windows NT 10.0; Win64; x64"; + if (desktopOS === "macOS" || userAgent.includes("Macintosh")) { + osSegment = "Macintosh; Intel Mac OS X 10_15_7"; + } + if ( + desktopOS !== "nonLinux" && + (desktopOS === "linux" || userAgent.includes("Linux")) + ) { + osSegment = "X11; Ubuntu; Linux x86_64"; + } + + UAHelpers._deviceAppropriateChromeUAs[ + key + ] = `Mozilla/5.0 (${osSegment}) FxQuantum/${RunningFirefoxVersion} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Safari/537.36`; + } + } + return UAHelpers._deviceAppropriateChromeUAs[key]; + }, + getPrefix(originalUA) { + return originalUA.substr(0, originalUA.indexOf(")") + 1); + }, + overrideWithDeviceAppropriateChromeUA(config) { + const chromeUA = UAHelpers.getDeviceAppropriateChromeUA(config); + Object.defineProperty(window.navigator.wrappedJSObject, "userAgent", { + get: exportFunction(() => chromeUA, window), + set: exportFunction(function () {}, window), + }); + }, + capVersionTo99(originalUA) { + const ver = originalUA.match(/Firefox\/(\d+\.\d+)/); + if (!ver || parseFloat(ver[1]) < 100) { + return originalUA; + } + return originalUA + .replace(`Firefox/${ver[1]}`, "Firefox/99.0") + .replace(`rv:${ver[1]}`, "rv:99.0"); + }, + capRvTo109(originalUA) { + const ver = originalUA.match(/rv:(\d+\.\d+)/); + if (!ver || parseFloat(ver[1]) <= 109) { + return originalUA; + } + return originalUA.replace(`rv:${ver[1]}`, "rv:109.0"); + }, + capVersionToNumber(originalUA, cap = 120) { + const ver = originalUA.match(/Firefox\/(\d+\.\d+)/); + if (!ver || parseFloat(ver[1]) <= cap) { + return originalUA; + } + const capped = `Firefox/${cap}.0`; + return originalUA.replace(`Firefox/${ver[1]}`, capped); + }, + getWindowsUA(originalUA) { + const rv = originalUA.match("rv:[0-9]+.[0-9]+")[0]; + const ver = originalUA.match("Firefox/[0-9]+.[0-9]+")[0]; + return `Mozilla/5.0 (Windows NT 10.0; Win64; x64; ${rv}) Gecko/20100101 ${ver}`; + }, +}; + +if (typeof module !== "undefined") { + module.exports = UAHelpers; +} |