return-youtube-dislike.script.js 7.7 KB

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