Created
May 10, 2022 14:00
-
-
Save warchildmd/ee849d64c41b7acf272aee31e44e49f7 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 YouTube Dislike | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://www.youtube.com/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
async function likesElementVisible() { | |
while (document.querySelector('#menu-container>#menu #top-level-buttons-computed') == null | |
|| document.querySelector('#menu-container>#menu #top-level-buttons-computed').children == null | |
|| document.querySelector('#menu-container>#menu #top-level-buttons-computed').children.length < 3) { | |
console.log('waiting for likes to be displayed...') | |
await sleep(1000) | |
} | |
} | |
async function displayDislikes() { | |
var videoUrl = window.location.href | |
var videoUrlParts = videoUrl.split('v=') | |
if (videoUrlParts.length < 2) { | |
console.log('not a video') | |
return | |
} | |
var videoId = videoUrlParts[videoUrlParts.length - 1].split('&')[0] | |
console.log(videoId) | |
await likesElementVisible() | |
var likeElement = document.querySelector('#menu-container>#menu #top-level-buttons-computed').children[0].children[0].children[1] | |
var dislikeElement = document.querySelector('#menu-container>#menu #top-level-buttons-computed').children[1].children[0].children[1] | |
dislikeElement.innerHTML = 'LOADING...' | |
await sleep(1000) | |
var strLikes = likeElement.getAttribute('aria-label') | |
var likes = parseInt(strLikes.replace(',', '').split(' ')[0]) | |
console.log(likes) | |
function formatNumber(nr) { | |
var strNr = nr.toString() | |
if (nr < 1000) { | |
return strNr; | |
} | |
if (nr < 1000000) { | |
return strNr.substring(0, strNr.length - 3) + 'K' | |
} | |
if (nr < 1000000000) { | |
return strNr.substring(0, strNr.length - 6) + 'M' | |
} | |
return strNr.substring(0, strNr.length - 9) + 'B' | |
} | |
function reqListener () { | |
var response = JSON.parse(this.responseText) | |
console.log(response) | |
dislikeElement.innerHTML = formatNumber(response.dislikes) | |
} | |
console.log('Requesting dislike information...') | |
var oReq = new XMLHttpRequest(); | |
oReq.addEventListener("load", reqListener); | |
oReq.open("GET", "https://returnyoutubedislikeapi.com/Votes?videoId=" + videoId + "&likeCount=" + likes); | |
oReq.send(); | |
} | |
document.addEventListener('yt-navigate-finish', function() { | |
displayDislikes() | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment