Return Youtube Dislike.user.js 4.3 KB

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