Return Youtube Dislike.user.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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: "https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" + getVideoId(),
  61. onload: function(response) {
  62. if (response != undefined) {
  63. const formattedDislike = numberFormat(response.response.dislikes);
  64. console.log(response);
  65. setDislikes(formattedDislike);
  66. }
  67. }
  68. });
  69. }
  70. function likeClicked() {
  71. console.log("like" + getState());
  72. setState();
  73. }
  74. function dislikeClicked() {
  75. console.log("dislike" + getState());
  76. setState();
  77. }
  78. function setInitalState() {
  79. setState();
  80. }
  81. function getVideoId() {
  82. const urlParams = new URLSearchParams(window.location.search);
  83. const videoId = urlParams.get("v");
  84. return videoId;
  85. }
  86. function isVideoLoaded() {
  87. const videoId = getVideoId();
  88. return (
  89. document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
  90. );
  91. }
  92. function numberFormat(numberState) {
  93. const userLocales = navigator.language;
  94. const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
  95. return formatter.format(numberState);
  96. }
  97. function setEventListeners(evt) {
  98. function checkForJS_Finish() {
  99. if (getButtons()?.offsetParent && isVideoLoaded()) {
  100. clearInterval(jsInitChecktimer);
  101. const buttons = getButtons();
  102. if (!window.returnDislikeButtonlistenersSet) {
  103. buttons.children[0].addEventListener("click", likeClicked);
  104. buttons.children[1].addEventListener("click", dislikeClicked);
  105. window.returnDislikeButtonlistenersSet = true;
  106. }
  107. setInitalState();
  108. }
  109. }
  110. if (window.location.href.indexOf("watch?") >= 0) {
  111. var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
  112. }
  113. }
  114. (function() {
  115. 'use strict';
  116. window.addEventListener("yt-navigate-finish", setEventListeners, true);
  117. setEventListeners();
  118. })();