utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { extConfig } from "./state";
  2. function roundDown(num) {
  3. if (num < 1000) return num;
  4. const int = Math.floor(Math.log10(num) - 2);
  5. const decimal = int + (int % 3 ? 1 : 0);
  6. const value = Math.floor(num / 10 ** decimal);
  7. return value * 10 ** decimal;
  8. }
  9. function numberFormat(numberState) {
  10. let userLocales;
  11. try {
  12. userLocales = new URL(
  13. Array.from(document.querySelectorAll("head > link[rel='search']"))
  14. ?.find((n) => n?.getAttribute("href")?.includes("?locale="))
  15. ?.getAttribute("href")
  16. )?.searchParams?.get("locale");
  17. } catch {}
  18. let numberDisplay;
  19. if (extConfig.numberDisplayRoundDown === false) {
  20. numberDisplay = numberState;
  21. } else {
  22. numberDisplay = roundDown(numberState);
  23. }
  24. return getNumberFormatter(extConfig.numberDisplayFormat).format(
  25. numberDisplay
  26. );
  27. }
  28. function localize(localeString) {
  29. return chrome.i18n.getMessage(localeString);
  30. }
  31. function getNumberFormatter(optionSelect) {
  32. let formatterNotation;
  33. let formatterCompactDisplay;
  34. let userLocales;
  35. try {
  36. userLocales = new URL(
  37. Array.from(document.querySelectorAll("head > link[rel='search']"))
  38. ?.find((n) => n?.getAttribute("href")?.includes("?locale="))
  39. ?.getAttribute("href")
  40. )?.searchParams?.get("locale");
  41. } catch {}
  42. switch (optionSelect) {
  43. case "compactLong":
  44. formatterNotation = "compact";
  45. formatterCompactDisplay = "long";
  46. break;
  47. case "standard":
  48. formatterNotation = "standard";
  49. formatterCompactDisplay = "short";
  50. break;
  51. case "compactShort":
  52. default:
  53. formatterNotation = "compact";
  54. formatterCompactDisplay = "short";
  55. }
  56. const formatter = Intl.NumberFormat(
  57. document.documentElement.lang || userLocales || navigator.language,
  58. {
  59. notation: formatterNotation,
  60. compactDisplay: formatterCompactDisplay,
  61. }
  62. );
  63. return formatter;
  64. }
  65. function getBrowser() {
  66. if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") {
  67. return chrome;
  68. } else if (
  69. typeof browser !== "undefined" &&
  70. typeof browser.runtime !== "undefined"
  71. ) {
  72. return browser;
  73. } else {
  74. console.log("browser is not supported");
  75. return false;
  76. }
  77. }
  78. function getVideoId(url) {
  79. const urlObject = new URL(url);
  80. const pathname = urlObject.pathname;
  81. if (pathname.startsWith("/clip")) {
  82. return document.querySelector("meta[itemprop='videoId']").content;
  83. } else {
  84. if (pathname.startsWith("/shorts")) {
  85. return pathname.slice(8);
  86. }
  87. return urlObject.searchParams.get("v");
  88. }
  89. }
  90. function isInViewport(element) {
  91. const rect = element.getBoundingClientRect();
  92. const height = innerHeight || document.documentElement.clientHeight;
  93. const width = innerWidth || document.documentElement.clientWidth;
  94. return (
  95. rect.top >= 0 &&
  96. rect.left >= 0 &&
  97. rect.bottom <= height &&
  98. rect.right <= width
  99. );
  100. }
  101. function isVideoLoaded() {
  102. const videoId = getVideoId(window.location.href);
  103. return (
  104. document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null ||
  105. // mobile: no video-id attribute
  106. document.querySelector('#player[loading="false"]:not([hidden])') !== null
  107. );
  108. }
  109. function cLog(message, writer) {
  110. message = `[return youtube dislike]: ${message}`;
  111. if (writer) {
  112. writer(message);
  113. } else {
  114. console.log(message);
  115. }
  116. }
  117. function getColorFromTheme(voteIsLike) {
  118. let colorString;
  119. switch (extConfig.colorTheme) {
  120. case "accessible":
  121. if (voteIsLike === true) {
  122. colorString = "dodgerblue";
  123. } else {
  124. colorString = "gold";
  125. }
  126. break;
  127. case "neon":
  128. if (voteIsLike === true) {
  129. colorString = "aqua";
  130. } else {
  131. colorString = "magenta";
  132. }
  133. break;
  134. case "classic":
  135. default:
  136. if (voteIsLike === true) {
  137. colorString = "lime";
  138. } else {
  139. colorString = "red";
  140. }
  141. }
  142. return colorString;
  143. }
  144. export {
  145. numberFormat,
  146. getBrowser,
  147. getVideoId,
  148. isInViewport,
  149. isVideoLoaded,
  150. cLog,
  151. getColorFromTheme,
  152. localize,
  153. };