Return YouTube Dislike

Return of the YouTube Dislike, Based off https://www.returnyoutubedislike.com/

当前为 2021-12-01 提交的版本,查看 最新版本

  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. // @compatible chrome
  9. // @compatible firefox
  10. // @compatible opera
  11. // @compatible safari
  12. // @compatible edge
  13. // @grant GM.xmlHttpRequest
  14. // ==/UserScript==
  15. function cLog(text, subtext = '') {
  16. subtext = subtext.trim() === '' ? '' : `(${subtext})`;
  17. console.log(`[Return YouTube Dislikes] ${text} ${subtext}`);
  18. }
  19.  
  20. function doXHR(opts) {
  21. if (typeof GM_xmlhttpRequest === 'function') {
  22. return GM_xmlhttpRequest(opts);
  23. }
  24. if (typeof GM !== 'undefined') /*This will prevent from throwing "Uncaught ReferenceError: GM is not defined"*/ {
  25. if (typeof GM.xmlHttpRequest === 'function') {
  26. return GM.xmlHttpRequest(opts);
  27. }
  28. }
  29.  
  30. console.warn('Unable to detect UserScript plugin, falling back to native XHR.');
  31.  
  32. const xhr = new XMLHttpRequest();
  33.  
  34. xhr.open(opts.method, opts.url, true);
  35. xhr.onload = () => opts.onload({
  36. response: JSON.parse(xhr.responseText),
  37. });
  38. xhr.onerror = err => console.error('XHR Failed', err);
  39. xhr.send();
  40. }
  41.  
  42. function getButtons() {
  43. if (document.getElementById("menu-container").offsetParent === null) {
  44. return document.querySelector(
  45. "ytd-menu-renderer.ytd-watch-metadata > div"
  46. );
  47. } else {
  48. return document
  49. .getElementById("menu-container")
  50. ?.querySelector("#top-level-buttons-computed");
  51. }
  52. }
  53.  
  54. function getLikeButton() {
  55. return getButtons().children[0];
  56. }
  57.  
  58. function getDislikeButton() {
  59. return getButtons().children[1];
  60. }
  61.  
  62. function isVideoLiked() {
  63. return getLikeButton().classList.contains("style-default-active");
  64. }
  65.  
  66. function isVideoDisliked() {
  67. return getDislikeButton().classList.contains("style-default-active");
  68. }
  69.  
  70. function isVideoNotLiked() {
  71. return getLikeButton().classList.contains("style-text");
  72. }
  73.  
  74. function isVideoNotDisliked() {
  75. return getDislikeButton().classList.contains("style-text");
  76. }
  77.  
  78. function getState() {
  79. if (isVideoLiked()) {
  80. return "liked";
  81. }
  82. if (isVideoDisliked()) {
  83. return "disliked";
  84. }
  85. return "neutral";
  86. }
  87.  
  88. function setLikes(likesCount) {
  89. getButtons().children[0].querySelector("#text").innerText = likesCount;
  90. }
  91.  
  92. function setDislikes(dislikesCount) {
  93. getButtons().children[1].querySelector("#text").innerText = dislikesCount;
  94. }
  95.  
  96. function createRateBar(likes, dislikes) {
  97. var rateBar = document.getElementById(
  98. "return-youtube-dislike-bar-container"
  99. );
  100.  
  101. const widthPx =
  102. getButtons().children[0].clientWidth +
  103. getButtons().children[1].clientWidth +
  104. 8;
  105.  
  106. const widthPercent =
  107. likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
  108.  
  109. if (!rateBar) {
  110. document.getElementById("menu-container").insertAdjacentHTML(
  111. "beforeend",
  112. `
  113. <div class="ryd-tooltip" style="width: ${widthPx}px">
  114. <div class="ryd-tooltip-bar-container">
  115. <div
  116. id="return-youtube-dislike-bar-container"
  117. style="width: 100%; height: 2px;"
  118. >
  119. <div
  120. id="return-youtube-dislike-bar"
  121. style="width: ${widthPercent}%; height: 100%"
  122. ></div>
  123. </div>
  124. </div>
  125. <tp-yt-paper-tooltip position="top" id="ryd-dislike-tooltip" class="style-scope ytd-sentiment-bar-renderer" role="tooltip" tabindex="-1">
  126. <!--css-build:shady-->${likes.toLocaleString()}&nbsp;/&nbsp;${dislikes.toLocaleString()}
  127. </tp-yt-paper-tooltip>
  128. </div>
  129. `
  130. );
  131. } else {
  132. document.getElementById(
  133. "return-youtube-dislike-bar-container"
  134. ).style.width = widthPx + "px";
  135. document.getElementById("return-youtube-dislike-bar").style.width =
  136. widthPercent + "%";
  137.  
  138. document.querySelector(
  139. "#ryd-dislike-tooltip > #tooltip"
  140. ).innerHTML = `${likes.toLocaleString()}&nbsp;/&nbsp;${dislikes.toLocaleString()}`;
  141. }
  142. }
  143.  
  144. function setState() {
  145. cLog('Fetching votes...');
  146.  
  147. doXHR({
  148. method: "GET",
  149. responseType: "json",
  150. url:
  151. "https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" +
  152. getVideoId(),
  153. onload: function (xhr) {
  154. if (xhr != undefined) {
  155. const { dislikes, likes } = xhr.response;
  156. cLog(`Received count: ${dislikes}`);
  157. setDislikes(numberFormat(dislikes));
  158. createRateBar(likes, dislikes);
  159. }
  160. },
  161. });
  162. }
  163.  
  164. function likeClicked() {
  165. cLog('Like clicked', getState());
  166. setState();
  167. }
  168.  
  169. function dislikeClicked() {
  170. cLog('Dislike clicked', getState());
  171. setState();
  172. }
  173.  
  174. function setInitalState() {
  175. setState();
  176. }
  177.  
  178. function getVideoId() {
  179. const urlParams = new URLSearchParams(window.location.search);
  180. const videoId = urlParams.get("v");
  181.  
  182. return videoId;
  183. }
  184.  
  185. function isVideoLoaded() {
  186. const videoId = getVideoId();
  187.  
  188. return (
  189. document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
  190. );
  191. }
  192.  
  193. function roundDown(num) {
  194. if (num < 1000) return num;
  195. const int = Math.floor(Math.log10(num) - 2);
  196. const decimal = int + (int % 3 ? 1 : 0);
  197. const value = Math.floor(num / 10 ** decimal);
  198. return value * (10 ** decimal);
  199. }
  200.  
  201. function numberFormat(numberState) {
  202. const userLocales = navigator.language;
  203.  
  204. const formatter = Intl.NumberFormat(userLocales, {
  205. notation: 'compact',
  206. minimumFractionDigits: 1,
  207. maximumFractionDigits: 1
  208. });
  209.  
  210. return formatter.format(roundDown(numberState)).replace(/\.0|,0/, '');
  211. }
  212.  
  213. function setEventListeners(evt) {
  214. function checkForJS_Finish() {
  215. if (getButtons()?.offsetParent && isVideoLoaded()) {
  216. clearInterval(jsInitChecktimer);
  217. const buttons = getButtons();
  218.  
  219. if (!window.returnDislikeButtonlistenersSet) {
  220. cLog('Registering button listeners...');
  221. buttons.children[0].addEventListener("click", likeClicked);
  222. buttons.children[1].addEventListener("click", dislikeClicked);
  223. window.returnDislikeButtonlistenersSet = true;
  224. }
  225. setInitalState();
  226. }
  227. }
  228.  
  229. if (window.location.href.indexOf("watch?") >= 0) {
  230. cLog('Setting up...');
  231. var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
  232. }
  233. }
  234.  
  235. (function () {
  236. "use strict";
  237. window.addEventListener("yt-navigate-finish", setEventListeners, true);
  238. setEventListeners();
  239. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址