utils.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. function roundDown(num) {
  2. if (num < 1000) return num;
  3. const int = Math.floor(Math.log10(num) - 2);
  4. const decimal = int + (int % 3 ? 1 : 0);
  5. const value = Math.floor(num / 10 ** decimal);
  6. return value * 10 ** decimal;
  7. }
  8. function numberFormat(numberState) {
  9. let userLocales;
  10. try {
  11. userLocales = new URL(
  12. Array.from(document.querySelectorAll("head > link[rel='search']"))
  13. ?.find((n) => n?.getAttribute("href")?.includes("?locale="))
  14. ?.getAttribute("href")
  15. )?.searchParams?.get("locale");
  16. } catch {}
  17. const formatter = Intl.NumberFormat(
  18. document.documentElement.lang || userLocales || navigator.language,
  19. {
  20. notation: "compact",
  21. }
  22. );
  23. return formatter.format(roundDown(numberState));
  24. }
  25. function getBrowser() {
  26. if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") {
  27. return chrome;
  28. } else if (
  29. typeof browser !== "undefined" &&
  30. typeof browser.runtime !== "undefined"
  31. ) {
  32. return browser;
  33. } else {
  34. console.log("browser is not supported");
  35. return false;
  36. }
  37. }
  38. export { numberFormat, getBrowser }