From dc79f7bfc5855f16d2dada859e0ad512d42cb5eb Mon Sep 17 00:00:00 2001 From: DARKDRAGON532 <69623818+DARKDRAGON532@users.noreply.github.com> Date: Sat, 27 Nov 2021 02:47:04 +0530 Subject: [PATCH] updated userscript to work with new ui --- .../UserScript/Return Youtube Dislike.user.js | 70 +++++++++++++++++-- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/Extensions/UserScript/Return Youtube Dislike.user.js b/Extensions/UserScript/Return Youtube Dislike.user.js index ec64dfe..9dea9a6 100644 --- a/Extensions/UserScript/Return Youtube Dislike.user.js +++ b/Extensions/UserScript/Return Youtube Dislike.user.js @@ -23,8 +23,10 @@ function doXHR(opts) { if (typeof GM_xmlhttpRequest === 'function') { return GM_xmlhttpRequest(opts); } - if (typeof GM.xmlHttpRequest === 'function') { - return GM.xmlHttpRequest(opts); + if (typeof GM !== 'undefined') /*This will prevent from throwing "Uncaught ReferenceError: GM is not defined"*/{ + if (typeof GM.xmlHttpRequest === 'function') { + return GM.xmlHttpRequest(opts); + } } console.warn('Unable to detect UserScript plugin, falling back to native XHR.'); @@ -40,9 +42,15 @@ function doXHR(opts) { } function getButtons() { - return document - .getElementById("menu-container") - ?.querySelector("#top-level-buttons-computed"); + if (document.getElementById("menu-container").offsetParent === null) { + return document.querySelector( + "ytd-menu-renderer.ytd-watch-metadata > div" + ); + } else { + return document + .getElementById("menu-container") + ?.querySelector("#top-level-buttons-computed"); + } } function getLikeButton() { @@ -88,6 +96,54 @@ function setDislikes(dislikesCount) { getButtons().children[1].querySelector("#text").innerText = dislikesCount; } +function createRateBar(likes, dislikes) { + var rateBar = document.getElementById( + "return-youtube-dislike-bar-container" + ); + + const widthPx = + getButtons().children[0].clientWidth + + getButtons().children[1].clientWidth + + 8; + + const widthPercent = + likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50; + + if (!rateBar) { + document.getElementById("menu-container").insertAdjacentHTML( + "beforeend", + ` +
+
+
+
+
+
+ + ${likes.toLocaleString()} / ${dislikes.toLocaleString()} + +
+` + ); + } else { + document.getElementById( + "return-youtube-dislike-bar-container" + ).style.width = widthPx + "px"; + document.getElementById("return-youtube-dislike-bar").style.width = + widthPercent + "%"; + + document.querySelector( + "#ryd-dislike-tooltip > #tooltip" + ).innerHTML = `${likes.toLocaleString()} / ${dislikes.toLocaleString()}`; + } +} + function setState() { cLog('Fetching votes...'); @@ -99,10 +155,10 @@ function setState() { getVideoId(), onload: function (xhr) { if (xhr != undefined) { - const { dislikes } = xhr.response; - + const { dislikes, likes } = xhr.response; cLog(`Received count: ${dislikes}`); setDislikes(numberFormat(dislikes)); + createRateBar(likes, dislikes); } }, });