Return Youtube Dislike.user.js 4.3 KB

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