Return Youtube Dislike.user.js 3.8 KB

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