return-youtube-dislike.script.js 7.2 KB

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