script.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. (function (extensionId) {
  2. function getButtons() {
  3. return document
  4. .getElementById("menu-container")
  5. ?.querySelector("#top-level-buttons-computed");
  6. }
  7. function getLikeButton() {
  8. return getButtons().children[0];
  9. }
  10. function getDislikeButton() {
  11. return getButtons().children[1];
  12. }
  13. function isVideoLiked() {
  14. return getLikeButton().classList.contains("style-default-active");
  15. }
  16. function isVideoDisliked() {
  17. return getDislikeButton().classList.contains("style-default-active");
  18. }
  19. function isVideoNotLiked() {
  20. return getLikeButton().classList.contains("style-text");
  21. }
  22. function isVideoNotDisliked() {
  23. return getDislikeButton().classList.contains("style-text");
  24. }
  25. function getState() {
  26. if (isVideoLiked()) {
  27. return "liked";
  28. }
  29. if (isVideoDisliked()) {
  30. return "disliked";
  31. }
  32. return "neutral";
  33. }
  34. function setLikes(likesCount) {
  35. getButtons().children[0].querySelector("#text").innerText = likesCount;
  36. }
  37. function setDislikes(dislikesCount) {
  38. getButtons().children[1].querySelector("#text").innerText = dislikesCount;
  39. }
  40. function setState() {
  41. chrome.runtime.sendMessage(
  42. extensionId,
  43. {
  44. message: "set_state",
  45. videoId: getVideoId(window.location.href),
  46. state: getState(),
  47. },
  48. function (response) {
  49. if (response != undefined) {
  50. const formattedDislike = numberFormat(response.dislikes);
  51. // setLikes(response.likes);
  52. setDislikes(formattedDislike);
  53. createRateBar(response.likes, response.dislikes);
  54. } else {
  55. }
  56. }
  57. );
  58. }
  59. function likeClicked() {
  60. // console.log("like" + getState());
  61. // setState();
  62. }
  63. function dislikeClicked() {
  64. // console.log("dislike" + getState());
  65. // setState();
  66. }
  67. function setInitalState() {
  68. setState();
  69. setTimeout(() => sendVideoIds(), 1500);
  70. }
  71. function getVideoId(url) {
  72. const urlObject = new URL(url);
  73. const videoId = urlObject.searchParams.get("v");
  74. return videoId;
  75. }
  76. function isVideoLoaded() {
  77. const videoId = getVideoId(window.location.href);
  78. return (
  79. document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
  80. );
  81. }
  82. function numberFormat(numberState) {
  83. const userLocales = navigator.language;
  84. const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
  85. return formatter.format(numberState);
  86. }
  87. var jsInitChecktimer = null;
  88. function setEventListeners(evt) {
  89. function checkForJS_Finish() {
  90. if (getButtons()?.offsetParent && isVideoLoaded()) {
  91. clearInterval(jsInitChecktimer);
  92. jsInitChecktimer = null;
  93. const buttons = getButtons();
  94. if (!window.returnDislikeButtonlistenersSet) {
  95. buttons.children[0].addEventListener("click", likeClicked);
  96. buttons.children[1].addEventListener("click", dislikeClicked);
  97. window.returnDislikeButtonlistenersSet = true;
  98. }
  99. setInitalState();
  100. }
  101. }
  102. if (window.location.href.indexOf("watch?") >= 0) {
  103. jsInitChecktimer = setInterval(checkForJS_Finish, 111);
  104. }
  105. }
  106. function createRateBar(likes, dislikes) {
  107. var rateBar = document.getElementById(
  108. "return-youtube-dislike-bar-container"
  109. );
  110. const widthPx =
  111. getButtons().children[0].clientWidth +
  112. getButtons().children[1].clientWidth;
  113. const widthPercent =
  114. likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
  115. if (!rateBar) {
  116. document.getElementById("menu-container").insertAdjacentHTML(
  117. "beforeend",
  118. `<div id="return-youtube-dislike-bar-container"
  119. style="width: ${widthPx}px;
  120. height: 3px; margin-left: 6px;">
  121. <div id="return-youtube-dislike-bar" style="width: ${widthPercent}%; height: 100%" ></div>
  122. </div>`
  123. );
  124. } else {
  125. document.getElementById(
  126. "return-youtube-dislike-bar-container"
  127. ).style.width = widthPx + "px";
  128. document.getElementById("return-youtube-dislike-bar").style.width =
  129. widthPercent + "%";
  130. }
  131. }
  132. function sendVideoIds() {
  133. const ids = Array.from(
  134. document.getElementsByClassName(
  135. "yt-simple-endpoint ytd-compact-video-renderer"
  136. )
  137. )
  138. .concat(
  139. Array.from(
  140. document.getElementsByClassName("yt-simple-endpoint ytd-thumbnail")
  141. )
  142. )
  143. .filter((x) => x.href && x.href.indexOf("/watch?v=") > 0)
  144. .map((x) => getVideoId(x.href));
  145. chrome.runtime.sendMessage(extensionId, {
  146. message: "send_links",
  147. videoIds: ids,
  148. });
  149. }
  150. setEventListeners();
  151. document.addEventListener("yt-navigate-finish", function (event) {
  152. if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
  153. window.returnDislikeButtonlistenersSet = false;
  154. setEventListeners();
  155. });
  156. window.onscrollend = () => {
  157. sendVideoIds();
  158. };
  159. setTimeout(() => sendVideoIds(), 1500);
  160. })(document.currentScript.getAttribute("extension-id"));