mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-03 03:11:23 +01:00
Userscript - New (spring 2024) UI support (#1046)
* Userscript - new (spring 2024) UI support Add new player selector for isVideoLoaded() (fixes #1043) * Githook tasks for 17f5bdf0 (in a separate commit for readability)
This commit is contained in:
1 parent
27d97a8b5a
commit
63a8b2868b
1 file changed
+38
-90
@@ -73,9 +73,7 @@ function isInViewport(element) {
|
|||||||
function getButtons() {
|
function getButtons() {
|
||||||
if (isShorts()) {
|
if (isShorts()) {
|
||||||
let elements = document.querySelectorAll(
|
let elements = document.querySelectorAll(
|
||||||
isMobile
|
isMobile ? "ytm-like-button-renderer" : "#like-button > ytd-like-button-renderer",
|
||||||
? "ytm-like-button-renderer"
|
|
||||||
: "#like-button > ytd-like-button-renderer",
|
|
||||||
);
|
);
|
||||||
for (let element of elements) {
|
for (let element of elements) {
|
||||||
if (isInViewport(element)) {
|
if (isInViewport(element)) {
|
||||||
@@ -85,42 +83,30 @@ function getButtons() {
|
|||||||
}
|
}
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
return (
|
return (
|
||||||
document.querySelector(
|
document.querySelector(".slim-video-action-bar-actions .segmented-buttons") ??
|
||||||
".slim-video-action-bar-actions .segmented-buttons",
|
document.querySelector(".slim-video-action-bar-actions")
|
||||||
) ?? document.querySelector(".slim-video-action-bar-actions")
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (document.getElementById("menu-container")?.offsetParent === null) {
|
if (document.getElementById("menu-container")?.offsetParent === null) {
|
||||||
return (
|
return (
|
||||||
document.querySelector("ytd-menu-renderer.ytd-watch-metadata > div") ??
|
document.querySelector("ytd-menu-renderer.ytd-watch-metadata > div") ??
|
||||||
document.querySelector(
|
document.querySelector("ytd-menu-renderer.ytd-video-primary-info-renderer > div")
|
||||||
"ytd-menu-renderer.ytd-video-primary-info-renderer > div",
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return document
|
return document.getElementById("menu-container")?.querySelector("#top-level-buttons-computed");
|
||||||
.getElementById("menu-container")
|
|
||||||
?.querySelector("#top-level-buttons-computed");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDislikeButton() {
|
function getDislikeButton() {
|
||||||
if (
|
if (getButtons().children[0].tagName === "YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER") {
|
||||||
getButtons().children[0].tagName ===
|
|
||||||
"YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER"
|
|
||||||
) {
|
|
||||||
if (getButtons().children[0].children[1] === undefined) {
|
if (getButtons().children[0].children[1] === undefined) {
|
||||||
return document.querySelector("#segmented-dislike-button");
|
return document.querySelector("#segmented-dislike-button");
|
||||||
} else {
|
} else {
|
||||||
return getButtons().children[0].children[1];
|
return getButtons().children[0].children[1];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (
|
if (getButtons().querySelector("segmented-like-dislike-button-view-model")) {
|
||||||
getButtons().querySelector("segmented-like-dislike-button-view-model")
|
const dislikeViewModel = getButtons().querySelector("dislike-button-view-model");
|
||||||
) {
|
|
||||||
const dislikeViewModel = getButtons().querySelector(
|
|
||||||
"dislike-button-view-model",
|
|
||||||
);
|
|
||||||
if (!dislikeViewModel) cLog("Dislike button wasn't added to DOM yet...");
|
if (!dislikeViewModel) cLog("Dislike button wasn't added to DOM yet...");
|
||||||
return dislikeViewModel;
|
return dislikeViewModel;
|
||||||
} else {
|
} else {
|
||||||
@@ -130,13 +116,11 @@ function getDislikeButton() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getLikeButton() {
|
function getLikeButton() {
|
||||||
return getButtons().children[0].tagName ===
|
return getButtons().children[0].tagName === "YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER"
|
||||||
"YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER"
|
|
||||||
? document.querySelector("#segmented-like-button") !== null
|
? document.querySelector("#segmented-like-button") !== null
|
||||||
? document.querySelector("#segmented-like-button")
|
? document.querySelector("#segmented-like-button")
|
||||||
: getButtons().children[0].children[0]
|
: getButtons().children[0].children[0]
|
||||||
: getButtons().querySelector("like-button-view-model") ??
|
: getButtons().querySelector("like-button-view-model") ?? getButtons().children[0];
|
||||||
getButtons().children[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLikeTextContainer() {
|
function getLikeTextContainer() {
|
||||||
@@ -158,8 +142,7 @@ function getDislikeTextContainer() {
|
|||||||
textSpan.id = "text";
|
textSpan.id = "text";
|
||||||
textSpan.style.marginLeft = "6px";
|
textSpan.style.marginLeft = "6px";
|
||||||
dislikeButton?.querySelector("button").appendChild(textSpan);
|
dislikeButton?.querySelector("button").appendChild(textSpan);
|
||||||
if (dislikeButton)
|
if (dislikeButton) dislikeButton.querySelector("button").style.width = "auto";
|
||||||
dislikeButton.querySelector("button").style.width = "auto";
|
|
||||||
result = textSpan;
|
result = textSpan;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -204,11 +187,7 @@ if (isShorts() && !shortsObserver) {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cLog(
|
cLog("Unexpected mutation observer event: " + mutation.target + mutation.type);
|
||||||
"Unexpected mutation observer event: " +
|
|
||||||
mutation.target +
|
|
||||||
mutation.type,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -216,20 +195,14 @@ if (isShorts() && !shortsObserver) {
|
|||||||
|
|
||||||
function isVideoLiked() {
|
function isVideoLiked() {
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
return (
|
return getLikeButton().querySelector("button").getAttribute("aria-label") == "true";
|
||||||
getLikeButton().querySelector("button").getAttribute("aria-label") ==
|
|
||||||
"true"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return getLikeButton().classList.contains("style-default-active");
|
return getLikeButton().classList.contains("style-default-active");
|
||||||
}
|
}
|
||||||
|
|
||||||
function isVideoDisliked() {
|
function isVideoDisliked() {
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
return (
|
return getDislikeButton()?.querySelector("button").getAttribute("aria-label") == "true";
|
||||||
getDislikeButton()?.querySelector("button").getAttribute("aria-label") ==
|
|
||||||
"true"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return getDislikeButton()?.classList.contains("style-default-active");
|
return getDislikeButton()?.classList.contains("style-default-active");
|
||||||
}
|
}
|
||||||
@@ -271,8 +244,7 @@ function getState() {
|
|||||||
|
|
||||||
function setLikes(likesCount) {
|
function setLikes(likesCount) {
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
getButtons().children[0].querySelector(".button-renderer-text").innerText =
|
getButtons().children[0].querySelector(".button-renderer-text").innerText = likesCount;
|
||||||
likesCount;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
getLikeTextContainer().innerText = likesCount;
|
getLikeTextContainer().innerText = likesCount;
|
||||||
@@ -295,8 +267,7 @@ function getLikeCountFromButton() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
let likeButton =
|
let likeButton =
|
||||||
getLikeButton().querySelector("yt-formatted-string#text") ??
|
getLikeButton().querySelector("yt-formatted-string#text") ?? getLikeButton().querySelector("button");
|
||||||
getLikeButton().querySelector("button");
|
|
||||||
|
|
||||||
let likesStr = likeButton.getAttribute("aria-label").replace(/\D/g, "");
|
let likesStr = likeButton.getAttribute("aria-label").replace(/\D/g, "");
|
||||||
return likesStr.length > 0 ? parseInt(likesStr) : false;
|
return likesStr.length > 0 ? parseInt(likesStr) : false;
|
||||||
@@ -355,11 +326,9 @@ function createRateBar(likes, dislikes) {
|
|||||||
}
|
}
|
||||||
let rateBar = document.getElementById("return-youtube-dislike-bar-container");
|
let rateBar = document.getElementById("return-youtube-dislike-bar-container");
|
||||||
|
|
||||||
const widthPx =
|
const widthPx = getLikeButton().clientWidth + (getDislikeButton()?.clientWidth ?? 52);
|
||||||
getLikeButton().clientWidth + (getDislikeButton()?.clientWidth ?? 52);
|
|
||||||
|
|
||||||
const widthPercent =
|
const widthPercent = likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
|
||||||
likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
|
|
||||||
|
|
||||||
var likePercentage = parseFloat(widthPercent.toFixed(1));
|
var likePercentage = parseFloat(widthPercent.toFixed(1));
|
||||||
const dislikePercentage = (100 - likePercentage).toLocaleString();
|
const dislikePercentage = (100 - likePercentage).toLocaleString();
|
||||||
@@ -416,21 +385,15 @@ function createRateBar(likes, dislikes) {
|
|||||||
`,
|
`,
|
||||||
);
|
);
|
||||||
let descriptionAndActionsElement = document.getElementById("top-row");
|
let descriptionAndActionsElement = document.getElementById("top-row");
|
||||||
descriptionAndActionsElement.style.borderBottom =
|
descriptionAndActionsElement.style.borderBottom = "1px solid var(--yt-spec-10-percent-layer)";
|
||||||
"1px solid var(--yt-spec-10-percent-layer)";
|
|
||||||
descriptionAndActionsElement.style.paddingBottom = "10px";
|
descriptionAndActionsElement.style.paddingBottom = "10px";
|
||||||
} else {
|
} else {
|
||||||
document.querySelector(".ryd-tooltip").style.width = widthPx + "px";
|
document.querySelector(".ryd-tooltip").style.width = widthPx + "px";
|
||||||
document.getElementById("return-youtube-dislike-bar").style.width =
|
document.getElementById("return-youtube-dislike-bar").style.width = widthPercent + "%";
|
||||||
widthPercent + "%";
|
|
||||||
|
|
||||||
if (extConfig.coloredBar) {
|
if (extConfig.coloredBar) {
|
||||||
document.getElementById(
|
document.getElementById("return-youtube-dislike-bar-container").style.backgroundColor = getColorFromTheme(false);
|
||||||
"return-youtube-dislike-bar-container",
|
document.getElementById("return-youtube-dislike-bar").style.backgroundColor = getColorFromTheme(true);
|
||||||
).style.backgroundColor = getColorFromTheme(false);
|
|
||||||
document.getElementById(
|
|
||||||
"return-youtube-dislike-bar",
|
|
||||||
).style.backgroundColor = getColorFromTheme(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -439,9 +402,7 @@ function setState() {
|
|||||||
cLog("Fetching votes...");
|
cLog("Fetching votes...");
|
||||||
let statsSet = false;
|
let statsSet = false;
|
||||||
|
|
||||||
fetch(
|
fetch(`https://returnyoutubedislikeapi.com/votes?videoId=${getVideoId()}`).then((response) => {
|
||||||
`https://returnyoutubedislikeapi.com/votes?videoId=${getVideoId()}`,
|
|
||||||
).then((response) => {
|
|
||||||
response.json().then((json) => {
|
response.json().then((json) => {
|
||||||
if (json && !("traceId" in response) && !statsSet) {
|
if (json && !("traceId" in response) && !statsSet) {
|
||||||
const { dislikes, likes } = json;
|
const { dislikes, likes } = json;
|
||||||
@@ -460,27 +421,19 @@ function setState() {
|
|||||||
const dislikeButton = getDislikeButton();
|
const dislikeButton = getDislikeButton();
|
||||||
if (isShorts()) {
|
if (isShorts()) {
|
||||||
// for shorts, leave deactived buttons in default color
|
// for shorts, leave deactived buttons in default color
|
||||||
const shortLikeButton = getLikeButton().querySelector(
|
const shortLikeButton = getLikeButton().querySelector("tp-yt-paper-button#button");
|
||||||
"tp-yt-paper-button#button",
|
const shortDislikeButton = dislikeButton?.querySelector("tp-yt-paper-button#button");
|
||||||
);
|
|
||||||
const shortDislikeButton = dislikeButton?.querySelector(
|
|
||||||
"tp-yt-paper-button#button",
|
|
||||||
);
|
|
||||||
if (shortLikeButton.getAttribute("aria-pressed") === "true") {
|
if (shortLikeButton.getAttribute("aria-pressed") === "true") {
|
||||||
shortLikeButton.style.color = getColorFromTheme(true);
|
shortLikeButton.style.color = getColorFromTheme(true);
|
||||||
}
|
}
|
||||||
if (
|
if (shortDislikeButton && shortDislikeButton.getAttribute("aria-pressed") === "true") {
|
||||||
shortDislikeButton &&
|
|
||||||
shortDislikeButton.getAttribute("aria-pressed") === "true"
|
|
||||||
) {
|
|
||||||
shortDislikeButton.style.color = getColorFromTheme(false);
|
shortDislikeButton.style.color = getColorFromTheme(false);
|
||||||
}
|
}
|
||||||
shortsObserver.observe(shortLikeButton);
|
shortsObserver.observe(shortLikeButton);
|
||||||
shortsObserver.observe(shortDislikeButton);
|
shortsObserver.observe(shortDislikeButton);
|
||||||
} else {
|
} else {
|
||||||
getLikeButton().style.color = getColorFromTheme(true);
|
getLikeButton().style.color = getColorFromTheme(true);
|
||||||
if (dislikeButton)
|
if (dislikeButton) dislikeButton.style.color = getColorFromTheme(false);
|
||||||
dislikeButton.style.color = getColorFromTheme(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -567,7 +520,12 @@ function isVideoLoaded() {
|
|||||||
const videoId = getVideoId();
|
const videoId = getVideoId();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
|
// desktop: spring 2024 UI
|
||||||
|
document.querySelector(`ytd-watch-grid[video-id='${videoId}']`) !== null ||
|
||||||
|
// desktop: older UI
|
||||||
|
document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null ||
|
||||||
|
// mobile: no video-id attribute
|
||||||
|
document.querySelector('#player[loading="false"]:not([hidden])') !== null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -586,9 +544,7 @@ function numberFormat(numberState) {
|
|||||||
} else {
|
} else {
|
||||||
numberDisplay = roundDown(numberState);
|
numberDisplay = roundDown(numberState);
|
||||||
}
|
}
|
||||||
return getNumberFormatter(extConfig.numberDisplayFormat).format(
|
return getNumberFormatter(extConfig.numberDisplayFormat).format(numberDisplay);
|
||||||
numberDisplay,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNumberFormatter(optionSelect) {
|
function getNumberFormatter(optionSelect) {
|
||||||
@@ -605,9 +561,7 @@ function getNumberFormatter(optionSelect) {
|
|||||||
?.getAttribute("href"),
|
?.getAttribute("href"),
|
||||||
)?.searchParams?.get("locale");
|
)?.searchParams?.get("locale");
|
||||||
} catch {
|
} catch {
|
||||||
cLog(
|
cLog("Cannot find browser locale. Use en as default for number formatting.");
|
||||||
"Cannot find browser locale. Use en as default for number formatting.",
|
|
||||||
);
|
|
||||||
userLocales = "en";
|
userLocales = "en";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -697,12 +651,8 @@ function setEventListeners(evt) {
|
|||||||
smartimationObserver.container = null;
|
smartimationObserver.container = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const smartimationContainer =
|
const smartimationContainer = buttons.querySelector("yt-smartimation");
|
||||||
buttons.querySelector("yt-smartimation");
|
if (smartimationContainer && smartimationObserver.container != smartimationContainer) {
|
||||||
if (
|
|
||||||
smartimationContainer &&
|
|
||||||
smartimationObserver.container != smartimationContainer
|
|
||||||
) {
|
|
||||||
cLog("Initializing smartimation mutation observer");
|
cLog("Initializing smartimation mutation observer");
|
||||||
smartimationObserver.disconnect();
|
smartimationObserver.disconnect();
|
||||||
smartimationObserver.observe(smartimationContainer);
|
smartimationObserver.observe(smartimationContainer);
|
||||||
@@ -740,9 +690,7 @@ if (isMobile) {
|
|||||||
if (dislikeButton?.querySelector(".button-renderer-text") === null) {
|
if (dislikeButton?.querySelector(".button-renderer-text") === null) {
|
||||||
getDislikeTextContainer().innerText = mobileDislikes;
|
getDislikeTextContainer().innerText = mobileDislikes;
|
||||||
} else {
|
} else {
|
||||||
if (dislikeButton)
|
if (dislikeButton) dislikeButton.querySelector(".button-renderer-text").innerText = mobileDislikes;
|
||||||
dislikeButton.querySelector(".button-renderer-text").innerText =
|
|
||||||
mobileDislikes;
|
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user