Return Youtube Dislike.user.js 3.6 KB

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