Return Youtube Dislike.user.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // ==UserScript==
  2. // @name Return YouTube Dislike
  3. // @namespace https://www.returnyoutubedislike.com/
  4. // @version 0.5
  5. // @description Return of the YouTube Dislike, Based off https://www.returnyoutubedislike.com/
  6. // @author Anarios & JRWR
  7. // @match *://*.youtube.com/*
  8. // @compatible chrome
  9. // @compatible firefox
  10. // @compatible opera
  11. // @compatible safari
  12. // @compatible edge
  13. // @downloadURL https://github.com/Anarios/return-youtube-dislike/raw/main/Extensions/UserScript/Return%20Youtube%20Dislike.user.js
  14. // @updateURL https://github.com/Anarios/return-youtube-dislike/raw/main/Extensions/UserScript/Return%20Youtube%20Dislike.user.js
  15. // @grant GM.xmlHttpRequest
  16. // ==/UserScript==
  17. function cLog(text, subtext = '') {
  18. subtext = subtext.trim() === '' ? '' : `(${subtext})`;
  19. console.log(`[Return Youtube Dislikes] ${text} ${subtext}`);
  20. }
  21. function doXHR(opts) {
  22. if (typeof GM_xmlhttpRequest === 'function') {
  23. return GM_xmlhttpRequest(opts);
  24. }
  25. if (typeof GM !== 'undefined') /*This will prevent from throwing "Uncaught ReferenceError: GM is not defined"*/ {
  26. if (typeof GM.xmlHttpRequest === 'function') {
  27. return GM.xmlHttpRequest(opts);
  28. }
  29. }
  30. console.warn('Unable to detect UserScript plugin, falling back to native XHR.');
  31. const xhr = new XMLHttpRequest();
  32. xhr.open(opts.method, opts.url, true);
  33. xhr.onload = () => opts.onload({
  34. response: JSON.parse(xhr.responseText),
  35. });
  36. xhr.onerror = err => console.error('XHR Failed', err);
  37. xhr.send();
  38. }
  39. function getButtons() {
  40. if (document.getElementById("menu-container").offsetParent === null) {
  41. return document.querySelector(
  42. "ytd-menu-renderer.ytd-watch-metadata > div"
  43. );
  44. } else {
  45. return document
  46. .getElementById("menu-container")
  47. ?.querySelector("#top-level-buttons-computed");
  48. }
  49. }
  50. function getLikeButton() {
  51. return getButtons().children[0];
  52. }
  53. function getDislikeButton() {
  54. return getButtons().children[1];
  55. }
  56. function isVideoLiked() {
  57. return getLikeButton().classList.contains("style-default-active");
  58. }
  59. function isVideoDisliked() {
  60. return getDislikeButton().classList.contains("style-default-active");
  61. }
  62. function isVideoNotLiked() {
  63. return getLikeButton().classList.contains("style-text");
  64. }
  65. function isVideoNotDisliked() {
  66. return getDislikeButton().classList.contains("style-text");
  67. }
  68. function getState() {
  69. if (isVideoLiked()) {
  70. return "liked";
  71. }
  72. if (isVideoDisliked()) {
  73. return "disliked";
  74. }
  75. return "neutral";
  76. }
  77. function setLikes(likesCount) {
  78. getButtons().children[0].querySelector("#text").innerText = likesCount;
  79. }
  80. function setDislikes(dislikesCount) {
  81. getButtons().children[1].querySelector("#text").innerText = dislikesCount;
  82. }
  83. function createRateBar(likes, dislikes) {
  84. var rateBar = document.getElementById(
  85. "return-youtube-dislike-bar-container"
  86. );
  87. const widthPx =
  88. getButtons().children[0].clientWidth +
  89. getButtons().children[1].clientWidth +
  90. 8;
  91. const widthPercent =
  92. likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
  93. if (!rateBar) {
  94. document.getElementById("menu-container").insertAdjacentHTML(
  95. "beforeend",
  96. `
  97. <div class="ryd-tooltip" style="width: ${widthPx}px">
  98. <div class="ryd-tooltip-bar-container">
  99. <div
  100. id="return-youtube-dislike-bar-container"
  101. style="width: 100%; height: 2px;"
  102. >
  103. <div
  104. id="return-youtube-dislike-bar"
  105. style="width: ${widthPercent}%; height: 100%"
  106. ></div>
  107. </div>
  108. </div>
  109. <tp-yt-paper-tooltip position="top" id="ryd-dislike-tooltip" class="style-scope ytd-sentiment-bar-renderer" role="tooltip" tabindex="-1">
  110. <!--css-build:shady-->${likes.toLocaleString()}&nbsp;/&nbsp;${dislikes.toLocaleString()}
  111. </tp-yt-paper-tooltip>
  112. </div>
  113. `
  114. );
  115. } else {
  116. document.getElementById(
  117. "return-youtube-dislike-bar-container"
  118. ).style.width = widthPx + "px";
  119. document.getElementById("return-youtube-dislike-bar").style.width =
  120. widthPercent + "%";
  121. document.querySelector(
  122. "#ryd-dislike-tooltip > #tooltip"
  123. ).innerHTML = `${likes.toLocaleString()}&nbsp;/&nbsp;${dislikes.toLocaleString()}`;
  124. }
  125. }
  126. function setState() {
  127. cLog('Fetching votes...');
  128. doXHR({
  129. method: "GET",
  130. responseType: "json",
  131. url:
  132. "https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" +
  133. getVideoId(),
  134. onload: function (xhr) {
  135. if (xhr != undefined) {
  136. const { dislikes, likes } = xhr.response;
  137. cLog(`Received count: ${dislikes}`);
  138. setDislikes(numberFormat(dislikes));
  139. createRateBar(likes, dislikes);
  140. }
  141. },
  142. });
  143. }
  144. function likeClicked() {
  145. cLog('Like clicked', getState());
  146. setState();
  147. }
  148. function dislikeClicked() {
  149. cLog('Dislike clicked', getState());
  150. setState();
  151. }
  152. function setInitalState() {
  153. setState();
  154. }
  155. function getVideoId() {
  156. const urlParams = new URLSearchParams(window.location.search);
  157. const videoId = urlParams.get("v");
  158. return videoId;
  159. }
  160. function isVideoLoaded() {
  161. const videoId = getVideoId();
  162. return (
  163. document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
  164. );
  165. }
  166. function roundDown(num) {
  167. if (num < 1000) return num;
  168. const int = Math.floor(Math.log10(num) - 2);
  169. const decimal = int + (int % 3 ? 1 : 0);
  170. const value = Math.floor(num / 10 ** decimal);
  171. return value * (10 ** decimal);
  172. }
  173. function numberFormat(numberState) {
  174. const userLocales = navigator.language;
  175. const formatter = Intl.NumberFormat(userLocales, {
  176. notation: 'compact',
  177. minimumFractionDigits: 1,
  178. maximumFractionDigits: 1
  179. });
  180. return formatter.format(roundDown(numberState)).replace(/\.0|,0/, '');
  181. }
  182. function setEventListeners(evt) {
  183. function checkForJS_Finish() {
  184. if (getButtons()?.offsetParent && isVideoLoaded()) {
  185. clearInterval(jsInitChecktimer);
  186. const buttons = getButtons();
  187. if (!window.returnDislikeButtonlistenersSet) {
  188. cLog('Registering button listeners...');
  189. buttons.children[0].addEventListener("click", likeClicked);
  190. buttons.children[1].addEventListener("click", dislikeClicked);
  191. window.returnDislikeButtonlistenersSet = true;
  192. }
  193. setInitalState();
  194. }
  195. }
  196. if (window.location.href.indexOf("watch?") >= 0) {
  197. cLog('Setting up...');
  198. var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
  199. }
  200. }
  201. (function () {
  202. "use strict";
  203. window.addEventListener("yt-navigate-finish", setEventListeners, true);
  204. setEventListeners();
  205. })();