script.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. statsSet = false;
  69. }
  70. }
  71. }
  72. );
  73. chrome.runtime.sendMessage(
  74. extensionId,
  75. {
  76. message: 'set_state',
  77. videoId: getVideoId(window.location.href),
  78. state: getState()
  79. },
  80. function (response) {
  81. cLog('response from api:');
  82. cLog(JSON.stringify(response));
  83. if (response != undefined && !statsSet) {
  84. const formattedDislike = numberFormat(response.dislikes);
  85. // setLikes(response.likes);
  86. setDislikes(formattedDislike);
  87. createRateBar(response.likes, response.dislikes);
  88. } else {
  89. }
  90. }
  91. );
  92. }
  93. function likeClicked () {
  94. // console.log("like" + getState());
  95. // setState();
  96. }
  97. function dislikeClicked () {
  98. // console.log("dislike" + getState());
  99. // setState();
  100. }
  101. function setInitalState () {
  102. setState();
  103. // setTimeout(() => sendVideoIds(), 1500);
  104. }
  105. function getVideoId (url) {
  106. const urlObject = new URL(url);
  107. const videoId = urlObject.searchParams.get('v');
  108. return videoId;
  109. }
  110. function isVideoLoaded () {
  111. const videoId = getVideoId(window.location.href);
  112. return (
  113. document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
  114. );
  115. }
  116. function numberFormat (numberState) {
  117. const userLocales = navigator.language;
  118. const formatter = Intl.NumberFormat(userLocales, { notation: 'compact' });
  119. return formatter.format(numberState);
  120. }
  121. var jsInitChecktimer = null;
  122. function setEventListeners (evt) {
  123. function checkForJS_Finish () {
  124. if (getButtons()?.offsetParent && isVideoLoaded()) {
  125. clearInterval(jsInitChecktimer);
  126. jsInitChecktimer = null;
  127. const buttons = getButtons();
  128. if (!window.returnDislikeButtonlistenersSet) {
  129. buttons.children[0].addEventListener('click', likeClicked);
  130. buttons.children[1].addEventListener('click', dislikeClicked);
  131. window.returnDislikeButtonlistenersSet = true;
  132. }
  133. setInitalState();
  134. }
  135. }
  136. if (window.location.href.indexOf('watch?') >= 0) {
  137. jsInitChecktimer = setInterval(checkForJS_Finish, 111);
  138. }
  139. }
  140. function createRateBar (likes, dislikes) {
  141. var rateBar = document.getElementById(
  142. 'return-youtube-dislike-bar-container'
  143. );
  144. const widthPx =
  145. getButtons().children[0].clientWidth +
  146. getButtons().children[1].clientWidth +
  147. 8;
  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" style="width: ${widthPx}px">
  155. <div class="ryd-tooltip-bar-container">
  156. <div
  157. id="return-youtube-dislike-bar-container"
  158. style="width: 100%; height: 2px;"
  159. >
  160. <div
  161. id="return-youtube-dislike-bar"
  162. style="width: ${widthPercent}%; height: 100%"
  163. ></div>
  164. </div>
  165. </div>
  166. <tp-yt-paper-tooltip position="top" id="ryd-dislike-tooltip" class="style-scope ytd-sentiment-bar-renderer" role="tooltip" tabindex="-1">
  167. <!--css-build:shady-->${likes.toLocaleString()}&nbsp;/&nbsp;${dislikes.toLocaleString()}
  168. </tp-yt-paper-tooltip>
  169. </div>
  170. `
  171. );
  172. } else {
  173. document.getElementById(
  174. "return-youtube-dislike-bar-container"
  175. ).style.width = widthPx + "px";
  176. document.getElementById("return-youtube-dislike-bar").style.width =
  177. widthPercent + "%";
  178. document
  179. .getElementById("ryd-dislike-tooltip")
  180. .firstChild().innerHTML = `${likes.toLocaleString()}&nbsp;/&nbsp;${dislikes.toLocaleString()}</div>`;
  181. }
  182. }
  183. // function sendVideoIds() {
  184. // const ids = Array.from(
  185. // document.getElementsByClassName(
  186. // "yt-simple-endpoint ytd-compact-video-renderer"
  187. // )
  188. // )
  189. // .concat(
  190. // Array.from(
  191. // document.getElementsByClassName("yt-simple-endpoint ytd-thumbnail")
  192. // )
  193. // )
  194. // .filter((x) => x.href && x.href.indexOf("/watch?v=") > 0)
  195. // .map((x) => getVideoId(x.href));
  196. // chrome.runtime.sendMessage(extensionId, {
  197. // message: "send_links",
  198. // videoIds: ids,
  199. // });
  200. // }
  201. setEventListeners();
  202. document.addEventListener('yt-navigate-finish', function (event) {
  203. if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
  204. window.returnDislikeButtonlistenersSet = false;
  205. setEventListeners();
  206. });
  207. // window.onscrollend = () => {
  208. // sendVideoIds();
  209. // };
  210. // setTimeout(() => sendVideoIds(), 1500);
  211. })(document.currentScript.getAttribute('extension-id'));