mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-03 03:11:23 +01:00
Merge pull request #511 from RyannDaGreat/main
YouTube Shorts now shows dislike counts
This commit is contained in:
7 files changed
+80
-11
No files matched your search
@@ -30,13 +30,34 @@ let likesvalue = 0;
|
|||||||
let dislikesvalue = 0;
|
let dislikesvalue = 0;
|
||||||
|
|
||||||
let isMobile = location.hostname == "m.youtube.com";
|
let isMobile = location.hostname == "m.youtube.com";
|
||||||
|
let isShorts = () => location.pathname.startsWith("/shorts")
|
||||||
let mobileDislikes = 0;
|
let mobileDislikes = 0;
|
||||||
function cLog(text, subtext = "") {
|
function cLog(text, subtext = "") {
|
||||||
subtext = subtext.trim() === "" ? "" : `(${subtext})`;
|
subtext = subtext.trim() === "" ? "" : `(${subtext})`;
|
||||||
console.log(`[Return YouTube Dislikes] ${text} ${subtext}`);
|
console.log(`[Return YouTube Dislikes] ${text} ${subtext}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isInViewport(element) {
|
||||||
|
const rect = element.getBoundingClientRect();
|
||||||
|
const height = innerHeight || document.documentElement.clientHeight;
|
||||||
|
const width = innerWidth || document.documentElement.clientWidth;
|
||||||
|
return (
|
||||||
|
rect.top >= 0 &&
|
||||||
|
rect.left >= 0 &&
|
||||||
|
rect.bottom <= height &&
|
||||||
|
rect.right <= width
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function getButtons() {
|
function getButtons() {
|
||||||
|
if(isShorts()) {
|
||||||
|
let elements=document.querySelectorAll(isMobile ? "ytm-like-button-renderer" : "#like-button > ytd-like-button-renderer");
|
||||||
|
for(let element of elements) {
|
||||||
|
if(isInViewport(element)) {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
return document.querySelector(".slim-video-action-bar-actions");
|
return document.querySelector(".slim-video-action-bar-actions");
|
||||||
}
|
}
|
||||||
@@ -214,6 +235,7 @@ function createRateBar(likes, dislikes) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function setState() {
|
function setState() {
|
||||||
cLog("Fetching votes...");
|
cLog("Fetching votes...");
|
||||||
let statsSet = false;
|
let statsSet = false;
|
||||||
@@ -232,7 +254,6 @@ function setState() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
setState = function(){};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function likeClicked() {
|
function likeClicked() {
|
||||||
@@ -288,6 +309,9 @@ function getVideoId() {
|
|||||||
if (pathname.startsWith("/clip")) {
|
if (pathname.startsWith("/clip")) {
|
||||||
return document.querySelector("meta[itemprop='videoId']").content;
|
return document.querySelector("meta[itemprop='videoId']").content;
|
||||||
} else {
|
} else {
|
||||||
|
if (pathname.startsWith("/shorts")) {
|
||||||
|
return pathname.substr(8);
|
||||||
|
}
|
||||||
return urlObject.searchParams.get("v");
|
return urlObject.searchParams.get("v");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -335,17 +359,21 @@ function setEventListeners(evt) {
|
|||||||
|
|
||||||
function checkForJS_Finish(check) {
|
function checkForJS_Finish(check) {
|
||||||
console.log();
|
console.log();
|
||||||
if (getButtons()?.offsetParent && isVideoLoaded()) {
|
if (isShorts() || getButtons()?.offsetParent && isVideoLoaded()) {
|
||||||
clearInterval(jsInitChecktimer);
|
|
||||||
const buttons = getButtons();
|
const buttons = getButtons();
|
||||||
|
|
||||||
if (!window.returnDislikeButtonlistenersSet) {
|
if (!window.returnDislikeButtonlistenersSet) {
|
||||||
cLog("Registering button listeners...");
|
cLog("Registering button listeners...");
|
||||||
buttons.children[0].addEventListener("click", likeClicked);
|
try {
|
||||||
buttons.children[1].addEventListener("click", dislikeClicked);
|
buttons.children[0].addEventListener("click", likeClicked);
|
||||||
|
buttons.children[1].addEventListener("click", dislikeClicked);
|
||||||
|
buttons.children[0].addEventListener("touchstart", likeClicked);
|
||||||
|
buttons.children[1].addEventListener("touchstart", dislikeClicked);
|
||||||
|
} catch { return } //Don't spam errors into the console
|
||||||
window.returnDislikeButtonlistenersSet = true;
|
window.returnDislikeButtonlistenersSet = true;
|
||||||
}
|
}
|
||||||
setInitialState();
|
setInitialState();
|
||||||
|
clearInterval(jsInitChecktimer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
//--- Import State Functions ---//
|
//--- Import State Functions ---//
|
||||||
import {
|
import {
|
||||||
isMobile,
|
isMobile,
|
||||||
|
isShorts,
|
||||||
isVideoDisliked,
|
isVideoDisliked,
|
||||||
isVideoLiked,
|
isVideoLiked,
|
||||||
getState,
|
getState,
|
||||||
@@ -35,12 +36,12 @@ let jsInitChecktimer = null;
|
|||||||
|
|
||||||
function setEventListeners(evt) {
|
function setEventListeners(evt) {
|
||||||
function checkForJS_Finish() {
|
function checkForJS_Finish() {
|
||||||
if (getButtons()?.offsetParent && isVideoLoaded()) {
|
if (isShorts() || getButtons()?.offsetParent && isVideoLoaded()) {
|
||||||
clearInterval(jsInitChecktimer);
|
|
||||||
jsInitChecktimer = null;
|
|
||||||
addLikeDislikeEventListener();
|
addLikeDislikeEventListener();
|
||||||
setInitialState();
|
setInitialState();
|
||||||
getBrowser().storage.onChanged.addListener(storageChangeHandler);
|
getBrowser().storage.onChanged.addListener(storageChangeHandler);
|
||||||
|
clearInterval(jsInitChecktimer);
|
||||||
|
jsInitChecktimer = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { getButtons } from "./buttons";
|
import { getButtons } from "./buttons";
|
||||||
import { likesDisabledState } from "./state";
|
import { isMobile, likesDisabledState } from "./state";
|
||||||
import { cLog } from "./utils";
|
import { cLog } from "./utils";
|
||||||
|
|
||||||
function createRateBar(likes, dislikes) {
|
function createRateBar(likes, dislikes) {
|
||||||
|
|||||||
@@ -1,6 +1,19 @@
|
|||||||
import { isMobile } from "./state";
|
import { isMobile, isShorts } from "./state";
|
||||||
|
import { isInViewport } from "./utils";
|
||||||
|
|
||||||
function getButtons() {
|
function getButtons() {
|
||||||
|
//--- If Watching Youtube Shorts: ---//
|
||||||
|
if(isShorts()) {
|
||||||
|
let elements=document.querySelectorAll(isMobile() ? "ytm-like-button-renderer" : "#like-button > ytd-like-button-renderer");
|
||||||
|
for(let element of elements) {
|
||||||
|
//Youtube Shorts can have multiple like/dislike buttons when scrolling through videos
|
||||||
|
//However, only one of them should be visible (no matter how you zoom)
|
||||||
|
if(isInViewport(element)) {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//--- If Watching On Mobile: ---//
|
||||||
if (isMobile()) {
|
if (isMobile()) {
|
||||||
return document.querySelector(".slim-video-action-bar-actions");
|
return document.querySelector(".slim-video-action-bar-actions");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,6 +100,8 @@ function addLikeDislikeEventListener() {
|
|||||||
if (!window.returnDislikeButtonlistenersSet) {
|
if (!window.returnDislikeButtonlistenersSet) {
|
||||||
buttons.children[0].addEventListener("click", likeClicked);
|
buttons.children[0].addEventListener("click", likeClicked);
|
||||||
buttons.children[1].addEventListener("click", dislikeClicked);
|
buttons.children[1].addEventListener("click", dislikeClicked);
|
||||||
|
buttons.children[0].addEventListener("touchstart", likeClicked);
|
||||||
|
buttons.children[1].addEventListener("touchstart", dislikeClicked);
|
||||||
window.returnDislikeButtonlistenersSet = true;
|
window.returnDislikeButtonlistenersSet = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ function isMobile() {
|
|||||||
return location.hostname == "m.youtube.com";
|
return location.hostname == "m.youtube.com";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isShorts() {
|
||||||
|
return location.pathname.startsWith("/shorts")
|
||||||
|
}
|
||||||
|
|
||||||
function isVideoLiked() {
|
function isVideoLiked() {
|
||||||
if (isMobile()) {
|
if (isMobile()) {
|
||||||
return (
|
return (
|
||||||
@@ -85,6 +89,11 @@ function setDislikes(dislikesCount) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getLikeCountFromButton() {
|
function getLikeCountFromButton() {
|
||||||
|
if(isShorts()) {
|
||||||
|
//Youtube Shorts don't work with this query. It's not nessecary; we can skip it and still see the results.
|
||||||
|
//It should be possible to fix this function, but it's not critical to showing the dislike count.
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
let likesStr = getLikeButton()
|
let likesStr = getLikeButton()
|
||||||
.querySelector("button")
|
.querySelector("button")
|
||||||
.getAttribute("aria-label")
|
.getAttribute("aria-label")
|
||||||
@@ -156,6 +165,7 @@ function initializeDisableVoteSubmission() {
|
|||||||
|
|
||||||
export {
|
export {
|
||||||
isMobile,
|
isMobile,
|
||||||
|
isShorts,
|
||||||
isVideoDisliked,
|
isVideoDisliked,
|
||||||
isVideoLiked,
|
isVideoLiked,
|
||||||
getState,
|
getState,
|
||||||
|
|||||||
@@ -45,10 +45,25 @@ function getVideoId(url) {
|
|||||||
if (pathname.startsWith("/clip")) {
|
if (pathname.startsWith("/clip")) {
|
||||||
return document.querySelector("meta[itemprop='videoId']").content;
|
return document.querySelector("meta[itemprop='videoId']").content;
|
||||||
} else {
|
} else {
|
||||||
|
if (pathname.startsWith("/shorts")) {
|
||||||
|
return pathname.substr(8);
|
||||||
|
}
|
||||||
return urlObject.searchParams.get("v");
|
return urlObject.searchParams.get("v");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isInViewport(element) {
|
||||||
|
const rect = element.getBoundingClientRect();
|
||||||
|
const height = innerHeight || document.documentElement.clientHeight;
|
||||||
|
const width = innerWidth || document.documentElement.clientWidth;
|
||||||
|
return (
|
||||||
|
rect.top >= 0 &&
|
||||||
|
rect.left >= 0 &&
|
||||||
|
rect.bottom <= height &&
|
||||||
|
rect.right <= width
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function isVideoLoaded() {
|
function isVideoLoaded() {
|
||||||
const videoId = getVideoId(window.location.href);
|
const videoId = getVideoId(window.location.href);
|
||||||
return (
|
return (
|
||||||
@@ -67,4 +82,4 @@ function cLog(message, writer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { numberFormat, getBrowser, getVideoId, isVideoLoaded, cLog }
|
export { numberFormat, getBrowser, getVideoId, isInViewport, isVideoLoaded, cLog }
|
||||||
Reference in new issue
Block a user