diff options
Diffstat (limited to 'Extensions/combined/src/utils.js')
-rw-r--r-- | Extensions/combined/src/utils.js | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/Extensions/combined/src/utils.js b/Extensions/combined/src/utils.js index a3fe790..05dafbf 100644 --- a/Extensions/combined/src/utils.js +++ b/Extensions/combined/src/utils.js @@ -9,15 +9,6 @@ function roundDown(num) { } function numberFormat(numberState) { - let userLocales; - try { - userLocales = new URL( - Array.from(document.querySelectorAll("head > link[rel='search']")) - ?.find((n) => n?.getAttribute("href")?.includes("?locale=")) - ?.getAttribute("href") - )?.searchParams?.get("locale"); - } catch {} - let numberDisplay; if (extConfig.numberDisplayRoundDown === false) { numberDisplay = numberState; @@ -29,22 +20,29 @@ function numberFormat(numberState) { ); } -function localize(localeString) { - return chrome.i18n.getMessage(localeString); -} - function getNumberFormatter(optionSelect) { - let formatterNotation; - let formatterCompactDisplay; let userLocales; - try { - userLocales = new URL( - Array.from(document.querySelectorAll("head > link[rel='search']")) - ?.find((n) => n?.getAttribute("href")?.includes("?locale=")) - ?.getAttribute("href") - )?.searchParams?.get("locale"); - } catch {} + if (document.documentElement.lang) { + userLocales = document.documentElement.lang; + } else if (navigator.language) { + userLocales = navigator.language; + } else { + try { + userLocales = new URL( + Array.from(document.querySelectorAll("head > link[rel='search']")) + ?.find((n) => n?.getAttribute("href")?.includes("?locale=")) + ?.getAttribute("href") + )?.searchParams?.get("locale"); + } catch { + cLog( + "Cannot find browser locale. Use en as default for number formatting." + ); + userLocales = "en"; + } + } + let formatterNotation; + let formatterCompactDisplay; switch (optionSelect) { case "compactLong": formatterNotation = "compact"; @@ -60,16 +58,17 @@ function getNumberFormatter(optionSelect) { formatterCompactDisplay = "short"; } - const formatter = Intl.NumberFormat( - document.documentElement.lang || userLocales || navigator.language, - { - notation: formatterNotation, - compactDisplay: formatterCompactDisplay, - } - ); + const formatter = Intl.NumberFormat(userLocales, { + notation: formatterNotation, + compactDisplay: formatterCompactDisplay, + }); return formatter; } +function localize(localeString) { + return chrome.i18n.getMessage(localeString); +} + function getBrowser() { if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") { return chrome; |