Return Youtube Dislike.user.js 4.1 KB

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