mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-11-29 09:27:54 +01:00
Merge branch 'main' into ColorizedButtonsAndBar-New
This commit is contained in:
29 files changed
+741
-93
No files matched your search
@@ -4,7 +4,8 @@ import {
|
||||
cLog,
|
||||
getColorFromTheme,
|
||||
} from "./utils";
|
||||
|
||||
import { isMobile, likesDisabledState } from "./state";
|
||||
import { cLog } from "./utils";
|
||||
function createRateBar(likes, dislikes) {
|
||||
if (!likesDisabledState) {
|
||||
let rateBar = document.getElementById("ryd-bar-container");
|
||||
@@ -17,6 +18,7 @@ function createRateBar(likes, dislikes) {
|
||||
const widthPercent =
|
||||
likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
|
||||
|
||||
|
||||
if (!rateBar) {
|
||||
let colorLikeStyle = '';
|
||||
let colorDislikeStyle = '';
|
||||
@@ -24,6 +26,8 @@ function createRateBar(likes, dislikes) {
|
||||
colorLikeStyle = '; background-color: ' + getColorFromTheme(true);
|
||||
colorDislikeStyle = '; background-color: ' + getColorFromTheme(false);
|
||||
}
|
||||
|
||||
if (!rateBar && !isMobile()) {
|
||||
(
|
||||
document.getElementById("menu-container") ||
|
||||
document.querySelector("ytm-slim-video-action-bar-renderer")
|
||||
|
||||
@@ -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(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()) {
|
||||
return document.querySelector(".slim-video-action-bar-actions");
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -109,7 +111,6 @@ function storageChangeHandler(changes, area) {
|
||||
handleDisableVoteSubmissionChangeEvent(
|
||||
changes.disableVoteSubmission.newValue
|
||||
);
|
||||
}
|
||||
if (changes.coloredThumbs !== undefined) {
|
||||
handleColoredThumbsChangeEvent(
|
||||
changes.coloredThumbs.newValue
|
||||
@@ -124,6 +125,13 @@ function storageChangeHandler(changes, area) {
|
||||
handleColorThemeChangeEvent(
|
||||
changes.colorTheme.newValue
|
||||
);
|
||||
if (changes.numberDisplayRoundDown !== undefined) {
|
||||
handleNumberDisplayRoundDownChangeEvent(
|
||||
changes.numberDisplayRoundDown.newValue
|
||||
);
|
||||
}
|
||||
if (changes.numberDisplayFormat !== undefined) {
|
||||
handleNumberDisplayFormatChangeEvent(changes.numberDisplayFormat.newValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +139,7 @@ function handleDisableVoteSubmissionChangeEvent(value) {
|
||||
extConfig.disableVoteSubmission = value;
|
||||
}
|
||||
|
||||
|
||||
function handleColoredThumbsChangeEvent(value) {
|
||||
extConfig.coloredThumbs = value;
|
||||
}
|
||||
@@ -142,6 +151,14 @@ function handleColoredBarChangeEvent(value) {
|
||||
function handleColorThemeChangeEvent(value) {
|
||||
extConfig.colorTheme = value;
|
||||
}
|
||||
|
||||
function handleNumberDisplayFormatChangeEvent(value) {
|
||||
extConfig.numberDisplayFormat = value;
|
||||
}
|
||||
|
||||
function handleNumberDisplayRoundDownChangeEvent(value) {
|
||||
extConfig.numberDisplayRoundDown = value;
|
||||
}
|
||||
|
||||
export {
|
||||
sendVote,
|
||||
|
||||
@@ -22,6 +22,8 @@ let extConfig = {
|
||||
coloredThumbs: false,
|
||||
coloredBar: false,
|
||||
colorTheme: "classic",
|
||||
numberDisplayFormat: 'compactShort',
|
||||
numberDisplayRoundDown: true,
|
||||
};
|
||||
|
||||
let storedData = {
|
||||
@@ -36,6 +38,10 @@ function isMobile() {
|
||||
return location.hostname == "m.youtube.com";
|
||||
}
|
||||
|
||||
function isShorts() {
|
||||
return location.pathname.startsWith("/shorts")
|
||||
}
|
||||
|
||||
function isVideoLiked() {
|
||||
if (isMobile()) {
|
||||
return (
|
||||
@@ -81,7 +87,7 @@ function setDislikes(dislikesCount) {
|
||||
}
|
||||
getButtons().children[1].querySelector("#text").innerText = dislikesCount;
|
||||
} else {
|
||||
cLog("likes count diabled by creator");
|
||||
cLog("likes count disabled by creator");
|
||||
if (isMobile()) {
|
||||
getButtons().children[1].querySelector(
|
||||
".button-renderer-text"
|
||||
@@ -94,6 +100,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")
|
||||
@@ -158,6 +169,8 @@ function initExtConfig() {
|
||||
initializeColoredThumbs();
|
||||
initializeColoredBar();
|
||||
initializeColorTheme();
|
||||
initializeNumberDisplayFormat();
|
||||
initializeNumberDisplayRoundDown();
|
||||
}
|
||||
|
||||
function initializeDisableVoteSubmission() {
|
||||
@@ -170,6 +183,7 @@ function initializeDisableVoteSubmission() {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function initializeColoredThumbs() {
|
||||
getBrowser().storage.sync.get(['coloredThumbs'], (res) => {
|
||||
if (res.coloredThumbs === undefined) {
|
||||
@@ -189,9 +203,17 @@ function initializeColoredBar() {
|
||||
else {
|
||||
extConfig.coloredBar = res.coloredBar;
|
||||
}
|
||||
function initializeNumberDisplayRoundDown() {
|
||||
getBrowser().storage.sync.get(["numberDisplayRoundDown"], (res) => {
|
||||
if (res.numberDisplayRoundDown === undefined) {
|
||||
getBrowser().storage.sync.set({ numberDisplayRoundDown: true });
|
||||
} else {
|
||||
extConfig.numberDisplayRoundDown = res.numberDisplayRoundDown;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function initializeColorTheme() {
|
||||
getBrowser().storage.sync.get(['colorTheme'], (res) => {
|
||||
if (res.colorTheme === undefined) {
|
||||
@@ -200,11 +222,21 @@ function initializeColorTheme() {
|
||||
else {
|
||||
extConfig.colorTheme = res.colorTheme;
|
||||
}
|
||||
}
|
||||
function initializeNumberDisplayFormat() {
|
||||
getBrowser().storage.sync.get(['numberDisplayFormat'], (res) => {
|
||||
if (res.numberDisplayFormat === undefined) {
|
||||
getBrowser().storage.sync.set({ numberDisplayFormat: 'compactShort' });
|
||||
}
|
||||
else {
|
||||
extConfig.numberDisplayFormat = res.numberDisplayFormat;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export {
|
||||
isMobile,
|
||||
isShorts,
|
||||
isVideoDisliked,
|
||||
isVideoLiked,
|
||||
getState,
|
||||
|
||||
@@ -19,14 +19,43 @@ function numberFormat(numberState) {
|
||||
?.getAttribute("href")
|
||||
)?.searchParams?.get("locale");
|
||||
} catch {}
|
||||
|
||||
let numberDisplay;
|
||||
if (extConfig.numberDisplayRoundDown === false) {
|
||||
numberDisplay = numberState;
|
||||
} else {
|
||||
numberDisplay = roundDown(numberState);
|
||||
}
|
||||
return getNumberFormatter(extConfig.numberDisplayFormat).format(numberDisplay);
|
||||
}
|
||||
|
||||
function getNumberFormatter(optionSelect) {
|
||||
let formatterNotation;
|
||||
let formatterCompactDisplay;
|
||||
|
||||
switch(optionSelect) {
|
||||
case 'compactLong':
|
||||
formatterNotation = 'compact';
|
||||
formatterCompactDisplay = 'long';
|
||||
break;
|
||||
case 'standard':
|
||||
formatterNotation = 'standard';
|
||||
formatterCompactDisplay = 'short';
|
||||
break;
|
||||
case 'compactShort':
|
||||
default:
|
||||
formatterNotation = 'compact';
|
||||
formatterCompactDisplay = 'short';
|
||||
}
|
||||
|
||||
const formatter = Intl.NumberFormat(
|
||||
document.documentElement.lang || userLocales || navigator.language,
|
||||
{
|
||||
notation: "compact",
|
||||
notation: formatterNotation,
|
||||
compactDisplay: formatterCompactDisplay,
|
||||
}
|
||||
);
|
||||
|
||||
return formatter.format(roundDown(numberState));
|
||||
return formatter;
|
||||
}
|
||||
|
||||
function getBrowser() {
|
||||
@@ -49,10 +78,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 (
|
||||
@@ -71,6 +115,7 @@ function cLog(message, writer) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getColorFromTheme(voteIsLike) {
|
||||
let colorString;
|
||||
switch(extConfig.colorTheme) {
|
||||
@@ -106,4 +151,5 @@ export {
|
||||
isVideoLoaded,
|
||||
cLog,
|
||||
getColorFromTheme,
|
||||
}
|
||||
}
|
||||
export { numberFormat, getBrowser, getVideoId, isInViewport, isVideoLoaded, cLog }
|
||||
Reference in new issue
Block a user