script.js 6.1 KB

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