anao123

YouTube Restore Dislike Counters

Nov 30th, 2021 (edited)
926
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         YouTube Restore Dislike Counters
  3. // @version      1.2.2
  4. // @description  A userscript to restore the dislike counts on YouTube. Not 100% accurate all the time, but stil pretty accurate.
  5. // @author       syndiate / me (bernd)
  6. // @match        *://www.youtube.com/*
  7. // @run-at       document-start
  8. // @downloadURL  https://pastebin.com/raw/Vrz7efY7
  9. // @updateURL    https://pastebin.com/raw/Vrz7efY7
  10. // ==/UserScript==
  11.  
  12. ;(function (window, document, undefined) {
  13.     var ButonRendererSelector = "yt-formatted-string#text.ytd-toggle-button-renderer";
  14.  
  15.     function numberWithCommas(num) {
  16.         return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  17.     }
  18.  
  19.     async function waitForElm(ele) {
  20.         while (!document.querySelector(ele))
  21.             await new Promise(x => requestAnimationFrame(x));
  22.         return;
  23.     }
  24.  
  25.     async function init() {
  26.         try {
  27.             // Get Data (server data on likes/dislikes)
  28.             var data = document.querySelector("ytd-app").data,
  29.                 topLevelButtonsData = data.response.contents.twoColumnWatchNextResults.results.results.contents.filter(obj => {
  30.                         return obj['videoPrimaryInfoRenderer'] !== void 0;
  31.                     }).at(-1).videoPrimaryInfoRenderer.videoActions.menuRenderer.topLevelButtons,
  32.                 isToggledLike = topLevelButtonsData[0].toggleButtonRenderer.isToggled,
  33.                 isToggledDislike = topLevelButtonsData[1].toggleButtonRenderer.isToggled,
  34.                 likes = Number(topLevelButtonsData[0].toggleButtonRenderer[isToggledLike ? "toggledText" : "defaultText"].accessibility.accessibilityData.label.match(/^[\d\.\,]+/g)[0].replace(/[\.,]/g,'')),
  35.                 rating = data.playerResponse.videoDetails.averageRating,
  36.                 // Calculate remaining data
  37.                 dislikes = rating != 0 ? Math.round(likes * ((5 - rating) / (rating - 1))) : 0,
  38.                 sentimentPercent = (likes / (likes + dislikes)) * 100;
  39.  
  40.             // Update likes and dislikes buttons count
  41.             var buttonRenderer = document.querySelectorAll(ButonRendererSelector);
  42.             buttonRenderer[0].innerHTML = numberWithCommas(likes);
  43.             buttonRenderer[1].innerHTML = numberWithCommas(dislikes);
  44.  
  45.             // Update sentiment bar
  46.             document.querySelector("ytd-sentiment-bar-renderer").removeAttribute("hidden");
  47.             document.getElementById("like-bar").setAttribute("style", "width: " + sentimentPercent + "%;");
  48.  
  49.             // When like/dislike buttons clicked
  50.             const observer = new MutationObserver(function(mutationList, observer) {
  51.                 mutationList.forEach(function (mutation) {
  52.                     if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
  53.                         var isToggledLikeLive = buttonRenderer[0].classList.contains('style-default-active'),
  54.                             isToggledDislikeLive = buttonRenderer[1].classList.contains('style-default-active');
  55.                         buttonRenderer[0].innerHTML = numberWithCommas(likes - isToggledLike + isToggledLikeLive);
  56.                         buttonRenderer[1].innerHTML = numberWithCommas(dislikes - isToggledDislike + isToggledDislikeLive);
  57.                     }
  58.                 })
  59.             });
  60.             observer.observe(buttonRenderer[0], {attributes: true});
  61.             observer.observe(buttonRenderer[1], {attributes: true});
  62.         } catch (e) {};
  63.     }
  64.  
  65.     window.addEventListener("DOMContentLoaded", function (event) {
  66.         waitForElm(ButonRendererSelector).then(() => init());
  67.         window.addEventListener('yt-page-data-updated', init, false);
  68.     });
  69. })(window, document);
  70.  
RAW Paste Data