Created
November 28, 2021 04:19
-
-
Save gslin/ca7eceea7620815061446463a16fa4ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Show Youtube Dislike Count | |
// @namespace maxhyt.youtubepp | |
// @version 1.1.2.1 | |
// @description (Re)Add dislike count to YouTube videos | |
// @author Maxhyt | |
// @license AGPL-3.0 | |
// @match https://www.youtube.com/* | |
// @icon https://icons.duckduckgo.com/ip2/youtube.com.ico | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
(async function() { | |
'use strict'; | |
while (document.body.querySelector('ytd-sentiment-bar-renderer') === null) { | |
await Sleep(100); | |
} | |
let likeBarContainer = document.body.querySelector('ytd-sentiment-bar-renderer'); | |
let tooltip = likeBarContainer.querySelector('#tooltip'); | |
likeBarContainer.querySelector('#container').addEventListener('mouseover', () => { | |
tooltip.classList.remove('hidden'); | |
}); | |
likeBarContainer.querySelector('#container').addEventListener('mouseleave', () => { | |
tooltip.classList.add('hidden'); | |
}); | |
window.addEventListener('yt-page-data-updated', UpdateCounter); | |
const buttons = document.body.querySelectorAll('a.yt-simple-endpoint > yt-formatted-string.ytd-toggle-button-renderer'); | |
let likeText = buttons[0]; | |
let dislikeText = buttons[1]; | |
function UpdateCounter() { | |
const data = document.querySelector("ytd-app")?.data; | |
const contents = data?.response.contents.twoColumnWatchNextResults?.results.results.contents; | |
if (data && contents) { | |
let videoInfo; | |
for (const content of contents) { | |
if (content.videoPrimaryInfoRenderer) { | |
videoInfo = content.videoPrimaryInfoRenderer; | |
break; | |
} | |
} | |
const likeCount = parseInt(videoInfo?.videoActions.menuRenderer.topLevelButtons[0].toggleButtonRenderer.accessibility.label.replace(/\D/g, '')); | |
if (likeCount >= 0) { | |
const r = data.playerResponse.videoDetails.averageRating; | |
const dislikeCount = Math.round(likeCount * (5 - r) / (r - 1)); | |
ShowDislikes(likeCount, dislikeCount); | |
} | |
} | |
} | |
function ShowDislikes(likeCount, dislikeCount) { | |
dislikeText.innerHTML = formatNumber(dislikeCount); | |
if (likeBarContainer) { | |
const likeBarWidth = likeText.parentNode.parentNode.getBoundingClientRect().width + dislikeText.parentNode.parentNode.getBoundingClientRect().width + 8; | |
likeBarContainer.style.width = `${likeBarWidth}px`; | |
likeBarContainer.removeAttribute('hidden'); | |
const likePerc = Math.floor(likeCount / (likeCount + dislikeCount) * 100); | |
likeBarContainer.querySelector('#like-bar').style.width = `${likePerc}%`; | |
tooltip.innerHTML = `${likeCount.toLocaleString()} / ${dislikeCount.toLocaleString()}`; | |
} | |
} | |
function formatNumber(num) { | |
if (num >= 1e9) { | |
const tmp = num >= 1e10 ? Math.floor(num / 1e9) : (Math.floor(num / 1e8) / 10); | |
return `${tmp.toLocaleString()}B`; | |
} | |
if (num >= 1e6) { | |
const tmp = num >= 1e7 ? Math.floor(num / 1e6) : (Math.floor(num / 1e5) / 10); | |
return `${tmp.toLocaleString()}M`; | |
} | |
if (num >= 1e3) { | |
const tmp = num >= 1e4 ? Math.floor(num / 1e3) : (Math.floor(num / 1e2) / 10); | |
return `${tmp.toLocaleString()}K`; | |
} | |
return num; | |
} | |
function Sleep(timeout) { | |
return new Promise(resolve => { | |
setTimeout(() => resolve(), timeout); | |
}); | |
} | |
UpdateCounter(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script not recognized as compatible with Tampermonkey 4.13.6136 (1 May 2021). Mozilla Firefox 98.0 (64-bit).