mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-11-21 22:03:01 +01:00
Prettier applied to whole project, prettier added to pre-commit hook
This commit is contained in:
1 parent
47517ce0bd
commit
b133adeecb
59 files changed
+912
-819
No files matched your search
@@ -35,7 +35,7 @@ const extConfig = {
|
||||
numberDisplayRoundDown: true, // [true*, false] Round down numbers (Show rounded down numbers)
|
||||
tooltipPercentageMode: "none", // [none*, dash_like, dash_dislike, both, only_like, only_dislike] Mode of showing percentage in like/dislike bar tooltip.
|
||||
numberDisplayReformatLikes: false, // [true, false*] Re-format like numbers (Make likes and dislikes format consistent)
|
||||
rateBarEnabled: false // [true, false*] Enables ratio bar under like/dislike buttons
|
||||
rateBarEnabled: false, // [true, false*] Enables ratio bar under like/dislike buttons
|
||||
// END USER OPTIONS
|
||||
};
|
||||
|
||||
@@ -75,7 +75,7 @@ function getButtons() {
|
||||
let elements = document.querySelectorAll(
|
||||
isMobile
|
||||
? "ytm-like-button-renderer"
|
||||
: "#like-button > ytd-like-button-renderer"
|
||||
: "#like-button > ytd-like-button-renderer",
|
||||
);
|
||||
for (let element of elements) {
|
||||
if (isInViewport(element)) {
|
||||
@@ -85,14 +85,17 @@ function getButtons() {
|
||||
}
|
||||
if (isMobile) {
|
||||
return (
|
||||
document.querySelector(".slim-video-action-bar-actions .segmented-buttons") ??
|
||||
document.querySelector(".slim-video-action-bar-actions")
|
||||
document.querySelector(
|
||||
".slim-video-action-bar-actions .segmented-buttons",
|
||||
) ?? document.querySelector(".slim-video-action-bar-actions")
|
||||
);
|
||||
}
|
||||
if (document.getElementById("menu-container")?.offsetParent === null) {
|
||||
return (
|
||||
document.querySelector("ytd-menu-renderer.ytd-watch-metadata > div") ??
|
||||
document.querySelector("ytd-menu-renderer.ytd-video-primary-info-renderer > div")
|
||||
document.querySelector(
|
||||
"ytd-menu-renderer.ytd-video-primary-info-renderer > div",
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return document
|
||||
@@ -102,17 +105,22 @@ function getButtons() {
|
||||
}
|
||||
|
||||
function getDislikeButton() {
|
||||
if (getButtons().children[0].tagName ===
|
||||
"YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER")
|
||||
{
|
||||
if (
|
||||
getButtons().children[0].tagName ===
|
||||
"YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER"
|
||||
) {
|
||||
if (getButtons().children[0].children[1] === undefined) {
|
||||
return document.querySelector("#segmented-dislike-button");
|
||||
} else {
|
||||
return getButtons().children[0].children[1];
|
||||
}
|
||||
} else {
|
||||
if (getButtons().querySelector("segmented-like-dislike-button-view-model")) {
|
||||
const dislikeViewModel = getButtons().querySelector("dislike-button-view-model");
|
||||
if (
|
||||
getButtons().querySelector("segmented-like-dislike-button-view-model")
|
||||
) {
|
||||
const dislikeViewModel = getButtons().querySelector(
|
||||
"dislike-button-view-model",
|
||||
);
|
||||
if (!dislikeViewModel) cLog("Dislike button wasn't added to DOM yet...");
|
||||
return dislikeViewModel;
|
||||
} else {
|
||||
@@ -123,9 +131,12 @@ function getDislikeButton() {
|
||||
|
||||
function getLikeButton() {
|
||||
return getButtons().children[0].tagName ===
|
||||
"YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER"
|
||||
? document.querySelector("#segmented-like-button") !== null ? document.querySelector("#segmented-like-button") : getButtons().children[0].children[0]
|
||||
: getButtons().querySelector("like-button-view-model") ?? getButtons().children[0];
|
||||
"YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER"
|
||||
? document.querySelector("#segmented-like-button") !== null
|
||||
? document.querySelector("#segmented-like-button")
|
||||
: getButtons().children[0].children[0]
|
||||
: getButtons().querySelector("like-button-view-model") ??
|
||||
getButtons().children[0];
|
||||
}
|
||||
|
||||
function getLikeTextContainer() {
|
||||
@@ -136,19 +147,19 @@ function getLikeTextContainer() {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function getDislikeTextContainer() {
|
||||
const dislikeButton = getDislikeButton();
|
||||
let result =
|
||||
dislikeButton?.querySelector("#text") ??
|
||||
dislikeButton?.getElementsByTagName("yt-formatted-string")[0] ??
|
||||
dislikeButton?.querySelector("span[role='text']")
|
||||
dislikeButton?.querySelector("span[role='text']");
|
||||
if (result === null) {
|
||||
let textSpan = document.createElement("span");
|
||||
textSpan.id = "text";
|
||||
textSpan.style.marginLeft = "6px";
|
||||
dislikeButton?.querySelector("button").appendChild(textSpan);
|
||||
if (dislikeButton) dislikeButton.querySelector("button").style.width = "auto";
|
||||
if (dislikeButton)
|
||||
dislikeButton.querySelector("button").style.width = "auto";
|
||||
result = textSpan;
|
||||
}
|
||||
return result;
|
||||
@@ -158,8 +169,12 @@ function createObserver(options, callback) {
|
||||
const observerWrapper = new Object();
|
||||
observerWrapper.options = options;
|
||||
observerWrapper.observer = new MutationObserver(callback);
|
||||
observerWrapper.observe = function (element) { this.observer.observe(element, this.options); }
|
||||
observerWrapper.disconnect = function () { this.observer.disconnect(); }
|
||||
observerWrapper.observe = function (element) {
|
||||
this.observer.observe(element, this.options);
|
||||
};
|
||||
observerWrapper.disconnect = function () {
|
||||
this.observer.disconnect();
|
||||
};
|
||||
return observerWrapper;
|
||||
}
|
||||
|
||||
@@ -167,31 +182,36 @@ let shortsObserver = null;
|
||||
|
||||
if (isShorts() && !shortsObserver) {
|
||||
cLog("Initializing shorts mutation observer");
|
||||
shortsObserver = createObserver({
|
||||
attributes: true
|
||||
}, (mutationList) => {
|
||||
mutationList.forEach((mutation) => {
|
||||
if (
|
||||
mutation.type === "attributes" &&
|
||||
mutation.target.nodeName === "TP-YT-PAPER-BUTTON" &&
|
||||
mutation.target.id === "button"
|
||||
) {
|
||||
cLog("Short thumb button status changed");
|
||||
if (mutation.target.getAttribute("aria-pressed") === "true") {
|
||||
mutation.target.style.color =
|
||||
mutation.target.parentElement.parentElement.id === "like-button"
|
||||
? getColorFromTheme(true)
|
||||
: getColorFromTheme(false);
|
||||
} else {
|
||||
mutation.target.style.color = "unset";
|
||||
shortsObserver = createObserver(
|
||||
{
|
||||
attributes: true,
|
||||
},
|
||||
(mutationList) => {
|
||||
mutationList.forEach((mutation) => {
|
||||
if (
|
||||
mutation.type === "attributes" &&
|
||||
mutation.target.nodeName === "TP-YT-PAPER-BUTTON" &&
|
||||
mutation.target.id === "button"
|
||||
) {
|
||||
cLog("Short thumb button status changed");
|
||||
if (mutation.target.getAttribute("aria-pressed") === "true") {
|
||||
mutation.target.style.color =
|
||||
mutation.target.parentElement.parentElement.id === "like-button"
|
||||
? getColorFromTheme(true)
|
||||
: getColorFromTheme(false);
|
||||
} else {
|
||||
mutation.target.style.color = "unset";
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
cLog(
|
||||
"Unexpected mutation observer event: " + mutation.target + mutation.type
|
||||
);
|
||||
});
|
||||
});
|
||||
cLog(
|
||||
"Unexpected mutation observer event: " +
|
||||
mutation.target +
|
||||
mutation.type,
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function isVideoLiked() {
|
||||
@@ -263,7 +283,7 @@ function setDislikes(dislikesCount) {
|
||||
mobileDislikes = dislikesCount;
|
||||
return;
|
||||
}
|
||||
getDislikeTextContainer()?.removeAttribute('is-empty');
|
||||
getDislikeTextContainer()?.removeAttribute("is-empty");
|
||||
getDislikeTextContainer().innerText = dislikesCount;
|
||||
}
|
||||
|
||||
@@ -274,18 +294,15 @@ function getLikeCountFromButton() {
|
||||
//It should be possible to fix this function, but it's not critical to showing the dislike count.
|
||||
return false;
|
||||
}
|
||||
let likeButton = getLikeButton()
|
||||
.querySelector("yt-formatted-string#text") ??
|
||||
getLikeButton().querySelector("button");
|
||||
let likeButton =
|
||||
getLikeButton().querySelector("yt-formatted-string#text") ??
|
||||
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;
|
||||
}
|
||||
catch {
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
(typeof GM_addStyle != "undefined"
|
||||
@@ -339,8 +356,7 @@ function createRateBar(likes, dislikes) {
|
||||
let rateBar = document.getElementById("return-youtube-dislike-bar-container");
|
||||
|
||||
const widthPx =
|
||||
getLikeButton().clientWidth +
|
||||
(getDislikeButton()?.clientWidth ?? 52);
|
||||
getLikeButton().clientWidth + (getDislikeButton()?.clientWidth ?? 52);
|
||||
|
||||
const widthPercent =
|
||||
likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
|
||||
@@ -397,25 +413,23 @@ function createRateBar(likes, dislikes) {
|
||||
<!--css-build:shady-->${tooltipInnerHTML}
|
||||
</tp-yt-paper-tooltip>
|
||||
</div>
|
||||
`
|
||||
`,
|
||||
);
|
||||
let descriptionAndActionsElement = document.getElementById("top-row");
|
||||
descriptionAndActionsElement.style.borderBottom =
|
||||
"1px solid var(--yt-spec-10-percent-layer)";
|
||||
descriptionAndActionsElement.style.paddingBottom = "10px";
|
||||
} 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 =
|
||||
widthPercent + "%";
|
||||
|
||||
if (extConfig.coloredBar) {
|
||||
document.getElementById(
|
||||
"return-youtube-dislike-bar-container"
|
||||
"return-youtube-dislike-bar-container",
|
||||
).style.backgroundColor = getColorFromTheme(false);
|
||||
document.getElementById(
|
||||
"return-youtube-dislike-bar"
|
||||
"return-youtube-dislike-bar",
|
||||
).style.backgroundColor = getColorFromTheme(true);
|
||||
}
|
||||
}
|
||||
@@ -426,7 +440,7 @@ function setState() {
|
||||
let statsSet = false;
|
||||
|
||||
fetch(
|
||||
`https://returnyoutubedislikeapi.com/votes?videoId=${getVideoId()}`
|
||||
`https://returnyoutubedislikeapi.com/votes?videoId=${getVideoId()}`,
|
||||
).then((response) => {
|
||||
response.json().then((json) => {
|
||||
if (json && !("traceId" in response) && !statsSet) {
|
||||
@@ -447,24 +461,26 @@ function setState() {
|
||||
if (isShorts()) {
|
||||
// for shorts, leave deactived buttons in default color
|
||||
const shortLikeButton = getLikeButton().querySelector(
|
||||
"tp-yt-paper-button#button"
|
||||
"tp-yt-paper-button#button",
|
||||
);
|
||||
const shortDislikeButton = dislikeButton?.querySelector(
|
||||
"tp-yt-paper-button#button"
|
||||
"tp-yt-paper-button#button",
|
||||
);
|
||||
if (shortLikeButton.getAttribute("aria-pressed") === "true") {
|
||||
shortLikeButton.style.color = getColorFromTheme(true);
|
||||
}
|
||||
if (shortDislikeButton &&
|
||||
shortDislikeButton.getAttribute("aria-pressed") === "true")
|
||||
{
|
||||
if (
|
||||
shortDislikeButton &&
|
||||
shortDislikeButton.getAttribute("aria-pressed") === "true"
|
||||
) {
|
||||
shortDislikeButton.style.color = getColorFromTheme(false);
|
||||
}
|
||||
shortsObserver.observe(shortLikeButton);
|
||||
shortsObserver.observe(shortDislikeButton);
|
||||
} else {
|
||||
getLikeButton().style.color = getColorFromTheme(true);
|
||||
if (dislikeButton) dislikeButton.style.color = getColorFromTheme(false);
|
||||
if (dislikeButton)
|
||||
dislikeButton.style.color = getColorFromTheme(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -571,7 +587,7 @@ function numberFormat(numberState) {
|
||||
numberDisplay = roundDown(numberState);
|
||||
}
|
||||
return getNumberFormatter(extConfig.numberDisplayFormat).format(
|
||||
numberDisplay
|
||||
numberDisplay,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -586,11 +602,11 @@ function getNumberFormatter(optionSelect) {
|
||||
userLocales = new URL(
|
||||
Array.from(document.querySelectorAll("head > link[rel='search']"))
|
||||
?.find((n) => n?.getAttribute("href")?.includes("?locale="))
|
||||
?.getAttribute("href")
|
||||
?.getAttribute("href"),
|
||||
)?.searchParams?.get("locale");
|
||||
} catch {
|
||||
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";
|
||||
}
|
||||
@@ -671,17 +687,22 @@ function setEventListeners(evt) {
|
||||
preNavigateLikeButton = getLikeButton();
|
||||
|
||||
if (!smartimationObserver) {
|
||||
smartimationObserver = createObserver({
|
||||
attributes: true,
|
||||
subtree: true
|
||||
}, updateDOMDislikes);
|
||||
smartimationObserver = createObserver(
|
||||
{
|
||||
attributes: true,
|
||||
subtree: true,
|
||||
},
|
||||
updateDOMDislikes,
|
||||
);
|
||||
smartimationObserver.container = null;
|
||||
}
|
||||
|
||||
const smartimationContainer = buttons.querySelector('yt-smartimation');
|
||||
if (smartimationContainer &&
|
||||
smartimationObserver.container != smartimationContainer)
|
||||
{
|
||||
const smartimationContainer =
|
||||
buttons.querySelector("yt-smartimation");
|
||||
if (
|
||||
smartimationContainer &&
|
||||
smartimationObserver.container != smartimationContainer
|
||||
) {
|
||||
cLog("Initializing smartimation mutation observer");
|
||||
smartimationObserver.disconnect();
|
||||
smartimationObserver.observe(smartimationContainer);
|
||||
@@ -716,12 +737,12 @@ if (isMobile) {
|
||||
};
|
||||
setInterval(() => {
|
||||
const dislikeButton = getDislikeButton();
|
||||
if(dislikeButton?.querySelector(".button-renderer-text") === null){
|
||||
if (dislikeButton?.querySelector(".button-renderer-text") === null) {
|
||||
getDislikeTextContainer().innerText = mobileDislikes;
|
||||
}
|
||||
else{
|
||||
if (dislikeButton) dislikeButton.querySelector(".button-renderer-text").innerText =
|
||||
mobileDislikes;
|
||||
} else {
|
||||
if (dislikeButton)
|
||||
dislikeButton.querySelector(".button-renderer-text").innerText =
|
||||
mobileDislikes;
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
Reference in new issue
Block a user