mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-11-21 13:53:01 +01:00
Merge pull request #593 from cyrildtm/fixUserLocalesUndefined
Restructure userLocales logic
This commit is contained in:
2 files changed
+44
-38
No files matched your search
@@ -461,32 +461,38 @@ 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 {
|
||||
userLocales = document.documentElement.lang;
|
||||
}
|
||||
|
||||
let numberDisplay;
|
||||
if (extConfig.numberDisplayRoundDown === false) {
|
||||
numberDisplay = numberState;
|
||||
} else {
|
||||
numberDisplay = roundDown(numberState);
|
||||
}
|
||||
return getNumberFormatter(extConfig.numberDisplayFormat, userLocales).format(
|
||||
return getNumberFormatter(extConfig.numberDisplayFormat).format(
|
||||
numberDisplay
|
||||
);
|
||||
}
|
||||
|
||||
function getNumberFormatter(optionSelect, userLocales) {
|
||||
function getNumberFormatter(optionSelect) {
|
||||
let userLocales;
|
||||
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";
|
||||
@@ -503,7 +509,7 @@ function getNumberFormatter(optionSelect, userLocales) {
|
||||
}
|
||||
|
||||
const formatter = Intl.NumberFormat(
|
||||
document.documentElement.lang || userLocales || navigator.language,
|
||||
userLocales,
|
||||
{
|
||||
notation: formatterNotation,
|
||||
compactDisplay: formatterCompactDisplay,
|
||||
|
||||
@@ -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,27 @@ function numberFormat(numberState) {
|
||||
);
|
||||
}
|
||||
|
||||
function localize(localeString) {
|
||||
return chrome.i18n.getMessage(localeString);
|
||||
}
|
||||
|
||||
function getNumberFormatter(optionSelect) {
|
||||
let userLocales;
|
||||
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;
|
||||
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 {}
|
||||
|
||||
switch (optionSelect) {
|
||||
case "compactLong":
|
||||
formatterNotation = "compact";
|
||||
@@ -61,7 +57,7 @@ function getNumberFormatter(optionSelect) {
|
||||
}
|
||||
|
||||
const formatter = Intl.NumberFormat(
|
||||
document.documentElement.lang || userLocales || navigator.language,
|
||||
userLocales,
|
||||
{
|
||||
notation: formatterNotation,
|
||||
compactDisplay: formatterCompactDisplay,
|
||||
@@ -70,6 +66,10 @@ function getNumberFormatter(optionSelect) {
|
||||
return formatter;
|
||||
}
|
||||
|
||||
function localize(localeString) {
|
||||
return chrome.i18n.getMessage(localeString);
|
||||
}
|
||||
|
||||
function getBrowser() {
|
||||
if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") {
|
||||
return chrome;
|
||||
|
||||
Reference in new issue
Block a user