Return Youtube Dislike.user.js 4.1 KB

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