Return Youtube Dislike.user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/watch*
  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.xmlHttpRequest === 'function') {
  26. return GM.xmlHttpRequest(opts);
  27. }
  28. console.warn('Unable to detect UserScript plugin, falling back to native XHR.');
  29. const xhr = new XMLHttpRequest();
  30. xhr.open(opts.method, opts.url, true);
  31. xhr.onload = () => opts.onload({
  32. response: JSON.parse(xhr.responseText),
  33. });
  34. xhr.onerror = err => console.error('XHR Failed', err);
  35. xhr.send();
  36. }
  37. function getButtons() {
  38. return document
  39. .getElementById("menu-container")
  40. ?.querySelector("#top-level-buttons-computed");
  41. }
  42. function getLikeButton() {
  43. return getButtons().children[0];
  44. }
  45. function getDislikeButton() {
  46. return getButtons().children[1];
  47. }
  48. function isVideoLiked() {
  49. return getLikeButton().classList.contains("style-default-active");
  50. }
  51. function isVideoDisliked() {
  52. return getDislikeButton().classList.contains("style-default-active");
  53. }
  54. function isVideoNotLiked() {
  55. return getLikeButton().classList.contains("style-text");
  56. }
  57. function isVideoNotDisliked() {
  58. return getDislikeButton().classList.contains("style-text");
  59. }
  60. function getState() {
  61. if (isVideoLiked()) {
  62. return "liked";
  63. }
  64. if (isVideoDisliked()) {
  65. return "disliked";
  66. }
  67. return "neutral";
  68. }
  69. function setLikes(likesCount) {
  70. getButtons().children[0].querySelector("#text").innerText = likesCount;
  71. }
  72. function setDislikes(dislikesCount) {
  73. getButtons().children[1].querySelector("#text").innerText = dislikesCount;
  74. }
  75. function setState() {
  76. cLog('Fetching votes...');
  77. doXHR({
  78. method: "GET",
  79. responseType: "json",
  80. url:
  81. "https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" +
  82. getVideoId(),
  83. onload: function (xhr) {
  84. if (xhr != undefined) {
  85. const { dislikes } = xhr.response;
  86. cLog(`Received count: ${dislikes}`);
  87. setDislikes(numberFormat(dislikes));
  88. }
  89. },
  90. });
  91. }
  92. function likeClicked() {
  93. cLog('Like clicked', getState());
  94. setState();
  95. }
  96. function dislikeClicked() {
  97. cLog('Dislike clicked', getState());
  98. setState();
  99. }
  100. function setInitalState() {
  101. setState();
  102. }
  103. function getVideoId() {
  104. const urlParams = new URLSearchParams(window.location.search);
  105. const videoId = urlParams.get("v");
  106. return videoId;
  107. }
  108. function isVideoLoaded() {
  109. const videoId = getVideoId();
  110. return (
  111. document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
  112. );
  113. }
  114. function numberFormat(numberState) {
  115. const userLocales = navigator.language;
  116. const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
  117. return formatter.format(numberState);
  118. }
  119. function setEventListeners(evt) {
  120. function checkForJS_Finish() {
  121. if (getButtons()?.offsetParent && isVideoLoaded()) {
  122. clearInterval(jsInitChecktimer);
  123. const buttons = getButtons();
  124. if (!window.returnDislikeButtonlistenersSet) {
  125. cLog('Registering button listeners...');
  126. buttons.children[0].addEventListener("click", likeClicked);
  127. buttons.children[1].addEventListener("click", dislikeClicked);
  128. window.returnDislikeButtonlistenersSet = true;
  129. }
  130. setInitalState();
  131. }
  132. }
  133. if (window.location.href.indexOf("watch?") >= 0) {
  134. cLog('Setting up...');
  135. var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
  136. }
  137. }
  138. (function () {
  139. "use strict";
  140. window.addEventListener("yt-navigate-finish", setEventListeners, true);
  141. setEventListeners();
  142. })();