mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-11-23 06:43:02 +01:00
* Fix CSS * Implement more accurate method for getting like + dislike btn widths
128 lines
4.7 KiB
JavaScript
128 lines
4.7 KiB
JavaScript
import { getButtons, getDislikeButton, getLikeButton } from "./buttons";
|
|
import {
|
|
extConfig,
|
|
isMobile,
|
|
isLikesDisabled,
|
|
isNewDesign,
|
|
isRoundedDesign,
|
|
isShorts,
|
|
} from "./state";
|
|
import { cLog, getColorFromTheme, isInViewport } from "./utils";
|
|
|
|
function createRateBar(likes, dislikes) {
|
|
let rateBar = document.getElementById("ryd-bar-container");
|
|
if (!isLikesDisabled()) {
|
|
// sometimes rate bar is hidden
|
|
if (rateBar && !isInViewport(rateBar)) {
|
|
rateBar.remove();
|
|
rateBar = null;
|
|
}
|
|
|
|
const widthPx =
|
|
parseFloat(window.getComputedStyle(getLikeButton()).width) +
|
|
parseFloat(window.getComputedStyle(getDislikeButton()).width) +
|
|
(isRoundedDesign() ? 0 : 8);
|
|
|
|
const widthPercent =
|
|
likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
|
|
|
|
var likePercentage = parseFloat(widthPercent.toFixed(1));
|
|
const dislikePercentage = (100 - likePercentage).toLocaleString();
|
|
likePercentage = likePercentage.toLocaleString();
|
|
|
|
if (extConfig.showTooltipPercentage) {
|
|
var tooltipInnerHTML;
|
|
switch (extConfig.tooltipPercentageMode) {
|
|
case "dash_dislike":
|
|
tooltipInnerHTML = `${likes.toLocaleString()} / ${dislikes.toLocaleString()} - ${dislikePercentage}%`;
|
|
break;
|
|
case "both":
|
|
tooltipInnerHTML = `${likePercentage}% / ${dislikePercentage}%`;
|
|
break;
|
|
case "only_like":
|
|
tooltipInnerHTML = `${likePercentage}%`;
|
|
break;
|
|
case "only_dislike":
|
|
tooltipInnerHTML = `${dislikePercentage}%`;
|
|
break;
|
|
default: // dash_like
|
|
tooltipInnerHTML = `${likes.toLocaleString()} / ${dislikes.toLocaleString()} - ${likePercentage}%`;
|
|
}
|
|
} else {
|
|
tooltipInnerHTML = `${likes.toLocaleString()} / ${dislikes.toLocaleString()}`;
|
|
}
|
|
|
|
if (!isShorts()) {
|
|
if (!rateBar && !isMobile()) {
|
|
let colorLikeStyle = "";
|
|
let colorDislikeStyle = "";
|
|
if (extConfig.coloredBar) {
|
|
colorLikeStyle = "; background-color: " + getColorFromTheme(true);
|
|
colorDislikeStyle = "; background-color: " + getColorFromTheme(false);
|
|
}
|
|
let actions =
|
|
isNewDesign() && getButtons().id === "top-level-buttons-computed"
|
|
? getButtons()
|
|
: document.getElementById("menu-container");
|
|
(
|
|
actions ||
|
|
document.querySelector("ytm-slim-video-action-bar-renderer")
|
|
).insertAdjacentHTML(
|
|
"beforeend",
|
|
`
|
|
<div class="ryd-tooltip ryd-tooltip-${isNewDesign() ? "new" : "old"}-design" style="width: ${widthPx}px">
|
|
<div class="ryd-tooltip-bar-container">
|
|
<div
|
|
id="ryd-bar-container"
|
|
style="width: 100%; height: 2px;${colorDislikeStyle}"
|
|
>
|
|
<div
|
|
id="ryd-bar"
|
|
style="width: ${widthPercent}%; height: 100%${colorLikeStyle}"
|
|
></div>
|
|
</div>
|
|
</div>
|
|
<tp-yt-paper-tooltip position="top" id="ryd-dislike-tooltip" class="style-scope ytd-sentiment-bar-renderer" role="tooltip" tabindex="-1">
|
|
<!--css-build:shady-->${tooltipInnerHTML}
|
|
</tp-yt-paper-tooltip>
|
|
</div>
|
|
`,
|
|
);
|
|
|
|
if (isNewDesign()) {
|
|
// Add border between info and comments
|
|
let descriptionAndActionsElement = document.getElementById("top-row");
|
|
descriptionAndActionsElement.style.borderBottom =
|
|
"1px solid var(--yt-spec-10-percent-layer)";
|
|
descriptionAndActionsElement.style.paddingBottom = "10px";
|
|
|
|
// Fix like/dislike ratio bar offset in new UI
|
|
document.getElementById("actions-inner").style.width = "revert";
|
|
if (isRoundedDesign()) {
|
|
document.getElementById("actions").style.flexDirection =
|
|
"row-reverse";
|
|
}
|
|
}
|
|
} else {
|
|
document.querySelector(`.ryd-tooltip`).style.width = widthPx + "px";
|
|
document.getElementById("ryd-bar").style.width = widthPercent + "%";
|
|
document.querySelector("#ryd-dislike-tooltip > #tooltip").innerHTML =
|
|
tooltipInnerHTML;
|
|
if (extConfig.coloredBar) {
|
|
document.getElementById("ryd-bar-container").style.backgroundColor =
|
|
getColorFromTheme(false);
|
|
document.getElementById("ryd-bar").style.backgroundColor =
|
|
getColorFromTheme(true);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
cLog("removing bar");
|
|
if (rateBar) {
|
|
rateBar.parentNode.removeChild(rateBar);
|
|
}
|
|
}
|
|
}
|
|
|
|
export { createRateBar };
|