From 1cc579d00d2f3f0d2235e20f15c0ec855adc2b8e Mon Sep 17 00:00:00 2001 From: RyanBurgert219 Date: Fri, 18 Mar 2022 10:27:58 -0400 Subject: [PATCH 1/4] Extensions: Supports YouTube Shorts --- Extensions/combined/ryd.content-script.js | 3 ++- Extensions/combined/src/bar.js | 2 +- Extensions/combined/src/buttons.js | 15 ++++++++++++++- Extensions/combined/src/state.js | 10 ++++++++++ Extensions/combined/src/utils.js | 17 ++++++++++++++++- 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Extensions/combined/ryd.content-script.js b/Extensions/combined/ryd.content-script.js index 880d25c..8b34ed6 100644 --- a/Extensions/combined/ryd.content-script.js +++ b/Extensions/combined/ryd.content-script.js @@ -9,6 +9,7 @@ import { //--- Import State Functions ---// import { isMobile, + isShorts, isVideoDisliked, isVideoLiked, getState, @@ -35,7 +36,7 @@ let jsInitChecktimer = null; function setEventListeners(evt) { function checkForJS_Finish() { - if (getButtons()?.offsetParent && isVideoLoaded()) { + if (isShorts() || getButtons()?.offsetParent && isVideoLoaded()) { clearInterval(jsInitChecktimer); jsInitChecktimer = null; addLikeDislikeEventListener(); diff --git a/Extensions/combined/src/bar.js b/Extensions/combined/src/bar.js index 790cfef..cbdaa26 100644 --- a/Extensions/combined/src/bar.js +++ b/Extensions/combined/src/bar.js @@ -1,5 +1,5 @@ import { getButtons } from "./buttons"; -import { likesDisabledState } from "./state"; +import { isMobile, likesDisabledState } from "./state"; import { cLog } from "./utils"; function createRateBar(likes, dislikes) { diff --git a/Extensions/combined/src/buttons.js b/Extensions/combined/src/buttons.js index 18c707a..d8267bc 100644 --- a/Extensions/combined/src/buttons.js +++ b/Extensions/combined/src/buttons.js @@ -1,6 +1,19 @@ -import { isMobile } from "./state"; +import { isMobile, isShorts } from "./state"; +import { isInViewport } from "./utils"; function getButtons() { + //--- If Watching Youtube Shorts: ---// + if(isShorts()) { + let elements=document.querySelectorAll("#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()) { return document.querySelector(".slim-video-action-bar-actions"); } diff --git a/Extensions/combined/src/state.js b/Extensions/combined/src/state.js index e2aaabb..47cf477 100644 --- a/Extensions/combined/src/state.js +++ b/Extensions/combined/src/state.js @@ -27,6 +27,10 @@ function isMobile() { return location.hostname == "m.youtube.com"; } +function isShorts() { + return location.pathname.startsWith("/shorts") +} + function isVideoLiked() { if (isMobile()) { return ( @@ -85,6 +89,11 @@ function setDislikes(dislikesCount) { } 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() .querySelector("button") .getAttribute("aria-label") @@ -156,6 +165,7 @@ function initializeDisableVoteSubmission() { export { isMobile, + isShorts, isVideoDisliked, isVideoLiked, getState, diff --git a/Extensions/combined/src/utils.js b/Extensions/combined/src/utils.js index e1c6dbe..452861d 100644 --- a/Extensions/combined/src/utils.js +++ b/Extensions/combined/src/utils.js @@ -45,10 +45,25 @@ function getVideoId(url) { if (pathname.startsWith("/clip")) { return document.querySelector("meta[itemprop='videoId']").content; } else { + if (pathname.startsWith("/shorts")) { + return pathname.substr(8); + } 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() { const videoId = getVideoId(window.location.href); return ( @@ -67,4 +82,4 @@ function cLog(message, writer) { } } -export { numberFormat, getBrowser, getVideoId, isVideoLoaded, cLog } \ No newline at end of file +export { numberFormat, getBrowser, getVideoId, isInViewport, isVideoLoaded, cLog } From 0630627d3bc1bdde3899932390e359514f51d7c7 Mon Sep 17 00:00:00 2001 From: RyanBurgert219 Date: Sun, 20 Mar 2022 05:22:27 -0400 Subject: [PATCH 2/4] UserScript: Supports YouTube Shorts --- .../UserScript/Return Youtube Dislike.user.js | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Extensions/UserScript/Return Youtube Dislike.user.js b/Extensions/UserScript/Return Youtube Dislike.user.js index 0f831b4..8e46192 100644 --- a/Extensions/UserScript/Return Youtube Dislike.user.js +++ b/Extensions/UserScript/Return Youtube Dislike.user.js @@ -30,13 +30,34 @@ let likesvalue = 0; let dislikesvalue = 0; let isMobile = location.hostname == "m.youtube.com"; +let isShorts = () => location.pathname.startsWith("/shorts") let mobileDislikes = 0; function cLog(text, subtext = "") { subtext = subtext.trim() === "" ? "" : `(${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() { + if(isShorts()) { + let elements=document.querySelectorAll("#like-button > ytd-like-button-renderer"); + for(let element of elements) { + if(isInViewport(element)) { + return element; + } + } + } if (isMobile) { return document.querySelector(".slim-video-action-bar-actions"); } @@ -214,6 +235,7 @@ function createRateBar(likes, dislikes) { } } + function setState() { cLog("Fetching votes..."); let statsSet = false; @@ -232,7 +254,6 @@ function setState() { } }); }); - setState = function(){}; } function likeClicked() { @@ -288,6 +309,9 @@ function getVideoId() { if (pathname.startsWith("/clip")) { return document.querySelector("meta[itemprop='videoId']").content; } else { + if (pathname.startsWith("/shorts")) { + return pathname.substr(8); + } return urlObject.searchParams.get("v"); } } @@ -335,7 +359,7 @@ function setEventListeners(evt) { function checkForJS_Finish(check) { console.log(); - if (getButtons()?.offsetParent && isVideoLoaded()) { + if (isShorts() || getButtons()?.offsetParent && isVideoLoaded()) { clearInterval(jsInitChecktimer); const buttons = getButtons(); From 73e58beac43b98d81e6e983ec7db3fa87e939b63 Mon Sep 17 00:00:00 2001 From: RyanBurgert219 Date: Sun, 20 Mar 2022 07:13:35 -0400 Subject: [PATCH 3/4] Mobile Extensions: Supports YouTube Shorts --- Extensions/combined/ryd.content-script.js | 4 ++-- Extensions/combined/src/buttons.js | 2 +- Extensions/combined/src/events.js | 2 ++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Extensions/combined/ryd.content-script.js b/Extensions/combined/ryd.content-script.js index 8b34ed6..72d2dea 100644 --- a/Extensions/combined/ryd.content-script.js +++ b/Extensions/combined/ryd.content-script.js @@ -37,11 +37,11 @@ let jsInitChecktimer = null; function setEventListeners(evt) { function checkForJS_Finish() { if (isShorts() || getButtons()?.offsetParent && isVideoLoaded()) { - clearInterval(jsInitChecktimer); - jsInitChecktimer = null; addLikeDislikeEventListener(); setInitialState(); getBrowser().storage.onChanged.addListener(storageChangeHandler); + clearInterval(jsInitChecktimer); + jsInitChecktimer = null; } } diff --git a/Extensions/combined/src/buttons.js b/Extensions/combined/src/buttons.js index d8267bc..4362b24 100644 --- a/Extensions/combined/src/buttons.js +++ b/Extensions/combined/src/buttons.js @@ -4,7 +4,7 @@ import { isInViewport } from "./utils"; function getButtons() { //--- If Watching Youtube Shorts: ---// if(isShorts()) { - let elements=document.querySelectorAll("#like-button > ytd-like-button-renderer") + 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) diff --git a/Extensions/combined/src/events.js b/Extensions/combined/src/events.js index 1874e14..247db75 100644 --- a/Extensions/combined/src/events.js +++ b/Extensions/combined/src/events.js @@ -100,6 +100,8 @@ function addLikeDislikeEventListener() { if (!window.returnDislikeButtonlistenersSet) { buttons.children[0].addEventListener("click", likeClicked); buttons.children[1].addEventListener("click", dislikeClicked); + buttons.children[0].addEventListener("touchstart", likeClicked); + buttons.children[1].addEventListener("touchstart", dislikeClicked); window.returnDislikeButtonlistenersSet = true; } } From c8267e7d8d8d6e902d11a0d1130d290fec40a1d0 Mon Sep 17 00:00:00 2001 From: RyanBurgert219 Date: Sun, 20 Mar 2022 07:13:48 -0400 Subject: [PATCH 4/4] Mobile UserScript: Supports YouTube Shorts --- Extensions/UserScript/Return Youtube Dislike.user.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Extensions/UserScript/Return Youtube Dislike.user.js b/Extensions/UserScript/Return Youtube Dislike.user.js index 8e46192..c5d03a9 100644 --- a/Extensions/UserScript/Return Youtube Dislike.user.js +++ b/Extensions/UserScript/Return Youtube Dislike.user.js @@ -51,7 +51,7 @@ function isInViewport(element) { function getButtons() { if(isShorts()) { - let elements=document.querySelectorAll("#like-button > ytd-like-button-renderer"); + 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; @@ -360,16 +360,20 @@ function setEventListeners(evt) { function checkForJS_Finish(check) { console.log(); if (isShorts() || getButtons()?.offsetParent && isVideoLoaded()) { - clearInterval(jsInitChecktimer); const buttons = getButtons(); if (!window.returnDislikeButtonlistenersSet) { cLog("Registering button listeners..."); - buttons.children[0].addEventListener("click", likeClicked); - buttons.children[1].addEventListener("click", dislikeClicked); + try { + 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; } setInitialState(); + clearInterval(jsInitChecktimer); } }