// ==UserScript== // @name YouTube Restore Dislike Counters // @version 1.0.1 // @description A userscript to restore the dislike counts on YouTube. Not 100% accurate all the time, but stil pretty accurate. // @author syndiate // @match *://www.youtube.com/* // @run_at document_start // ==/UserScript== // Turns a number like 1234567 into 1,234,567 function numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } // Wait for the webpage to load before doing anything. async function waitForElements(s) { while (!document.querySelector(s)) { await new Promise(r => requestAnimationFrame(r)); } } async function init() { try { //Dig into the Webpage to find the Video-Rating element. const data = document.querySelector("ytd-app").data; const dataContents = data.response.contents.twoColumnWatchNextResults.results.results.contents; let like_button = null; let likes = 0; for(let i = 0; i < dataContents.length; i++) { if (typeof dataContents[i].videoPrimaryInfoRenderer != 'undefined') { like_button = dataContents[i].videoPrimaryInfoRenderer.videoActions.menuRenderer.topLevelButtons[0].toggleButtonRenderer; break; } } // If you liked the video, take the toggledText-Element. If not, take the defaultText-Element. const like_count_text = like_button.isToggled ? like_button.toggledText : like_button.defaultText; likes = parseInt(like_count_text.simpleText.replace(/(\.|,)/g, "")); // YouTube / Google will most likely remove averageRating too! // The ratio goes from 0 to 5 stars. let ratio = data.playerResponse.videoDetails.averageRating; // Calculate the dislikes using the star-ratio and the likes. let dislikes = ratio != 0 ? Math.round(likes*((5-ratio)/(ratio-1))) : 0; let dislikesfin = numberWithCommas(dislikes); let likesfin = numberWithCommas(likes); // Apply the calculated numbers to the text. document.querySelector("yt-formatted-string#text.ytd-toggle-button-renderer").innerHTML = ratio != 0 ? likesfin : "0"; document.querySelectorAll("yt-formatted-string#text.ytd-toggle-button-renderer")[1].innerHTML = dislikesfin; // Display the ratingbar document.querySelector("ytd-sentiment-bar-renderer").removeAttribute("hidden"); document.getElementById("like-bar").setAttribute("style", "width: " + (100/(1+(5-ratio)/(ratio-1))) + "%;"); } catch(e) {} } waitForElements("yt-formatted-string#text.ytd-toggle-button-renderer").then(() => init()); window.addEventListener('yt-page-data-updated', init, false);