Merge pull request #366 from oSumAtrIX/format-dislikes

Format numbers the same way as the extension for the UserScript
This commit is contained in:
Dmitrii Selivanov authored and GitHub committed 2021-12-27 13:30:45 +03:00
commit 2475a701a4
1 file changed
+9 -9
@@ -311,22 +311,22 @@ function roundDown(num) {
} }
function numberFormat(numberState) { function numberFormat(numberState) {
let localeURL = Array.from(document.querySelectorAll("head > link[rel='search']")) let userLocales;
?.find((n) => n?.getAttribute("href")?.includes("?locale=")) try {
?.getAttribute("href"); userLocales = new URL(
Array.from(document.querySelectorAll("head > link[rel='search']"))
const userLocales = localeURL ? new URL(localeURL)?.searchParams?.get("locale") : document.body.lang; ?.find((n) => n?.getAttribute("href")?.includes("?locale="))
?.getAttribute("href")
)?.searchParams?.get("locale");
} catch {}
const formatter = Intl.NumberFormat( const formatter = Intl.NumberFormat(
document.documentElement.lang || userLocales || navigator.language, document.documentElement.lang || userLocales || navigator.language,
{ {
notation: "compact", notation: "compact",
minimumFractionDigits: 1,
maximumFractionDigits: 1,
} }
); );
return formatter.format(roundDown(numberState)).replace(/\.0|,0/, ""); return formatter.format(roundDown(numberState));
} }
function setEventListeners(evt) { function setEventListeners(evt) {