Return Youtube Dislike.user.js 4.3 KB

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