Return Youtube Dislike.user.js 3.5 KB

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