Return Youtube Dislike.user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. } else if (isVideoDisliked()) {
  64. return "disliked";
  65. }
  66. return "neutral";
  67. }
  68. function setLikes(likesCount) {
  69. getButtons().children[0].querySelector("#text").innerText = likesCount;
  70. }
  71. function setDislikes(dislikesCount) {
  72. getButtons().children[1].querySelector("#text").innerText = dislikesCount;
  73. }
  74. function setState() {
  75. cLog('Fetching votes...');
  76. doXHR({
  77. method: "GET",
  78. responseType: "json",
  79. url:
  80. "https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" +
  81. getVideoId(),
  82. onload: function (xhr) {
  83. if (xhr != undefined) {
  84. const { dislikes } = xhr.response;
  85. cLog(`Received count: ${dislikes}`);
  86. setDislikes(numberFormat(dislikes));
  87. }
  88. },
  89. });
  90. }
  91. function likeClicked() {
  92. cLog('Like clicked', getState());
  93. setState();
  94. }
  95. function dislikeClicked() {
  96. cLog('Dislike clicked', getState());
  97. setState();
  98. }
  99. function setInitalState() {
  100. setState();
  101. }
  102. function getVideoId() {
  103. const urlParams = new URLSearchParams(window.location.search);
  104. const videoId = urlParams.get("v");
  105. return videoId;
  106. }
  107. function isVideoLoaded() {
  108. const videoId = getVideoId();
  109. return (
  110. document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
  111. );
  112. }
  113. function numberFormat(numberState) {
  114. const userLocales = navigator.language;
  115. const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
  116. return formatter.format(numberState);
  117. }
  118. function setEventListeners(evt) {
  119. function checkForJS_Finish() {
  120. if (getButtons()?.offsetParent && isVideoLoaded()) {
  121. clearInterval(jsInitChecktimer);
  122. const buttons = getButtons();
  123. if (!window.returnDislikeButtonlistenersSet) {
  124. cLog('Registering button listeners...');
  125. buttons.children[0].addEventListener("click", likeClicked);
  126. buttons.children[1].addEventListener("click", dislikeClicked);
  127. window.returnDislikeButtonlistenersSet = true;
  128. }
  129. setInitalState();
  130. }
  131. }
  132. if (window.location.href.indexOf("watch?") >= 0) {
  133. cLog('Setting up...');
  134. var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
  135. }
  136. }
  137. (function () {
  138. "use strict";
  139. window.addEventListener("yt-navigate-finish", setEventListeners, true);
  140. setEventListeners();
  141. })();