mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-11-23 23:03:01 +01:00
Extension fix - disappearing dislike counter (#999)
* Fix rate bar jumping around in width * Fix disappearing dislike counter * Fix disappearing dislike count on button focus/unfocus by @kokonguyen191 * Fix event listeners being thrown away by YouTube in some cases * Extension fix for disappearing dislikes while smartimations play * Check if smartimationContainer exists
This commit is contained in:
1 parent
2f2153ed37
commit
6791543617
5 files changed
+65
-42
No files matched your search
@@ -19,9 +19,9 @@ import {
|
||||
isVideoLoaded,
|
||||
cLog,
|
||||
} from "./src/utils";
|
||||
import { createRateBar } from "./src/bar";
|
||||
import {
|
||||
addLikeDislikeEventListener,
|
||||
createSmartimationObserver,
|
||||
storageChangeHandler,
|
||||
} from "./src/events";
|
||||
|
||||
@@ -37,6 +37,7 @@ async function setEventListeners (evt) {
|
||||
clearInterval(jsInitChecktimer);
|
||||
jsInitChecktimer = null;
|
||||
addLikeDislikeEventListener();
|
||||
createSmartimationObserver();
|
||||
await setInitialState();
|
||||
isSetInitialStateDone = true;
|
||||
getBrowser().storage.onChanged.addListener(storageChangeHandler);
|
||||
@@ -57,6 +58,5 @@ await setEventListeners();
|
||||
|
||||
document.addEventListener("yt-navigate-finish", async function (event) {
|
||||
if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
|
||||
window.returnDislikeButtonlistenersSet = false;
|
||||
await setEventListeners();
|
||||
});
|
||||
@@ -101,7 +101,7 @@ function createRateBar(likes, dislikes) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
document.getElementById("ryd-bar-container").style.width =
|
||||
document.querySelector(`.ryd-tooltip`).style.width =
|
||||
widthPx + "px";
|
||||
document.getElementById("ryd-bar").style.width = widthPercent + "%";
|
||||
document.querySelector("#ryd-dislike-tooltip > #tooltip").innerHTML =
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getBrowser, getVideoId, numberFormat, cLog } from "./utils";
|
||||
import { getBrowser, getVideoId, numberFormat, cLog, createObserver } from "./utils";
|
||||
import { checkForSignInButton, getButtons, getDislikeButton, getLikeButton } from './buttons';
|
||||
import {
|
||||
NEUTRAL_STATE,
|
||||
@@ -22,24 +22,28 @@ function sendVote(vote) {
|
||||
}
|
||||
}
|
||||
|
||||
function updateDOMDislikes() {
|
||||
setDislikes(numberFormat(storedData.dislikes));
|
||||
createRateBar(storedData.likes, storedData.dislikes);
|
||||
}
|
||||
|
||||
function likeClicked() {
|
||||
if (checkForSignInButton() === false) {
|
||||
if (storedData.previousState === DISLIKED_STATE) {
|
||||
sendVote(1);
|
||||
if (storedData.dislikes > 0) storedData.dislikes--;
|
||||
storedData.likes++;
|
||||
createRateBar(storedData.likes, storedData.dislikes);
|
||||
setDislikes(numberFormat(storedData.dislikes));
|
||||
updateDOMDislikes();
|
||||
storedData.previousState = LIKED_STATE;
|
||||
} else if (storedData.previousState === NEUTRAL_STATE) {
|
||||
sendVote(1);
|
||||
storedData.likes++;
|
||||
createRateBar(storedData.likes, storedData.dislikes);
|
||||
updateDOMDislikes();
|
||||
storedData.previousState = LIKED_STATE;
|
||||
} else if ((storedData.previousState = LIKED_STATE)) {
|
||||
sendVote(0);
|
||||
if (storedData.likes > 0) storedData.likes--;
|
||||
createRateBar(storedData.likes, storedData.dislikes);
|
||||
updateDOMDislikes();
|
||||
storedData.previousState = NEUTRAL_STATE;
|
||||
}
|
||||
if (extConfig.numberDisplayReformatLikes === true) {
|
||||
@@ -56,21 +60,18 @@ function dislikeClicked() {
|
||||
if (storedData.previousState === NEUTRAL_STATE) {
|
||||
sendVote(-1);
|
||||
storedData.dislikes++;
|
||||
setDislikes(numberFormat(storedData.dislikes));
|
||||
createRateBar(storedData.likes, storedData.dislikes);
|
||||
updateDOMDislikes();
|
||||
storedData.previousState = DISLIKED_STATE;
|
||||
} else if (storedData.previousState === DISLIKED_STATE) {
|
||||
sendVote(0);
|
||||
if (storedData.dislikes > 0) storedData.dislikes--;
|
||||
setDislikes(numberFormat(storedData.dislikes));
|
||||
createRateBar(storedData.likes, storedData.dislikes);
|
||||
updateDOMDislikes();
|
||||
storedData.previousState = NEUTRAL_STATE;
|
||||
} else if (storedData.previousState === LIKED_STATE) {
|
||||
sendVote(-1);
|
||||
if (storedData.likes > 0) storedData.likes--;
|
||||
storedData.dislikes++;
|
||||
setDislikes(numberFormat(storedData.dislikes));
|
||||
createRateBar(storedData.likes, storedData.dislikes);
|
||||
updateDOMDislikes();
|
||||
storedData.previousState = DISLIKED_STATE;
|
||||
if (extConfig.numberDisplayReformatLikes === true) {
|
||||
const nativeLikes = getLikeCountFromButton();
|
||||
@@ -83,14 +84,36 @@ function dislikeClicked() {
|
||||
}
|
||||
|
||||
function addLikeDislikeEventListener() {
|
||||
if (!window.returnDislikeButtonlistenersSet) {
|
||||
if (window.rydPreNavigateLikeButton !== getLikeButton()) {
|
||||
getLikeButton().addEventListener("click", likeClicked);
|
||||
getLikeButton().addEventListener("touchstart", likeClicked);
|
||||
if(getDislikeButton()) {
|
||||
if (getDislikeButton()) {
|
||||
getDislikeButton().addEventListener("click", dislikeClicked);
|
||||
getDislikeButton().addEventListener("touchstart", dislikeClicked);
|
||||
getDislikeButton().addEventListener("focusin", updateDOMDislikes);
|
||||
getDislikeButton().addEventListener("focusout", updateDOMDislikes);
|
||||
}
|
||||
window.returnDislikeButtonlistenersSet = true;
|
||||
window.rydPreNavigateLikeButton = getLikeButton();
|
||||
}
|
||||
}
|
||||
|
||||
let smartimationObserver = null;
|
||||
|
||||
function createSmartimationObserver() {
|
||||
if (!smartimationObserver) {
|
||||
smartimationObserver = createObserver({
|
||||
attributes: true,
|
||||
subtree: true
|
||||
}, updateDOMDislikes);
|
||||
smartimationObserver.container = null;
|
||||
}
|
||||
|
||||
const smartimationContainer = getButtons().querySelector('yt-smartimation');
|
||||
if (smartimationContainer && smartimationObserver.container != smartimationContainer) {
|
||||
cLog("Initializing smartimation mutation observer");
|
||||
smartimationObserver.disconnect();
|
||||
smartimationObserver.observe(smartimationContainer);
|
||||
smartimationObserver.container = smartimationContainer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,5 +172,6 @@ export {
|
||||
likeClicked,
|
||||
dislikeClicked,
|
||||
addLikeDislikeEventListener,
|
||||
createSmartimationObserver,
|
||||
storageChangeHandler,
|
||||
};
|
||||
@@ -11,9 +11,11 @@ import {
|
||||
getVideoId,
|
||||
cLog,
|
||||
numberFormat,
|
||||
getColorFromTheme, querySelector
|
||||
getColorFromTheme,
|
||||
querySelector,
|
||||
localize,
|
||||
createObserver
|
||||
} from './utils';
|
||||
import { localize } from "./utils";
|
||||
import { createStarRating } from "./starRating";
|
||||
|
||||
//TODO: Do not duplicate here and in ryd.background.js
|
||||
@@ -82,20 +84,13 @@ function isRoundedDesign() {
|
||||
return querySelector(extConfig.selectors.roundedDesign) !== null;
|
||||
}
|
||||
|
||||
let mutationObserver = new Object();
|
||||
let shortsObserver = null;
|
||||
|
||||
if (isShorts() && mutationObserver.exists !== true) {
|
||||
cLog("initializing mutation observer");
|
||||
mutationObserver.options = {
|
||||
childList: false,
|
||||
attributes: true,
|
||||
subtree: false,
|
||||
};
|
||||
mutationObserver.exists = true;
|
||||
mutationObserver.observer = new MutationObserver(function (
|
||||
mutationList,
|
||||
observer
|
||||
) {
|
||||
if (isShorts() && !shortsObserver) {
|
||||
cLog("Initializing shorts mutation observer");
|
||||
shortsObserver = createObserver({
|
||||
attributes: true
|
||||
}, (mutationList) => {
|
||||
mutationList.forEach((mutation) => {
|
||||
if (
|
||||
mutation.type === "attributes" &&
|
||||
@@ -114,7 +109,7 @@ if (isShorts() && mutationObserver.exists !== true) {
|
||||
return;
|
||||
}
|
||||
cLog(
|
||||
"unexpected mutation observer event: " + mutation.target + mutation.type
|
||||
"Unexpected mutation observer event: " + mutation.target + mutation.type
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -236,14 +231,8 @@ function processResponse(response, storedData) {
|
||||
if (shortDislikeButton.getAttribute("aria-pressed") === "true") {
|
||||
shortDislikeButton.style.color = getColorFromTheme(false);
|
||||
}
|
||||
mutationObserver.observer.observe(
|
||||
shortLikeButton,
|
||||
mutationObserver.options
|
||||
);
|
||||
mutationObserver.observer.observe(
|
||||
shortDislikeButton,
|
||||
mutationObserver.options
|
||||
);
|
||||
shortsObserver.observe(shortLikeButton);
|
||||
shortsObserver.observe(shortDislikeButton);
|
||||
} else {
|
||||
getLikeButton().style.color = getColorFromTheme(true);
|
||||
getDislikeButton().style.color = getColorFromTheme(false);
|
||||
|
||||
@@ -164,6 +164,15 @@ function querySelectorAll(selectors) {
|
||||
return result;
|
||||
}
|
||||
|
||||
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(); }
|
||||
return observerWrapper;
|
||||
}
|
||||
|
||||
export {
|
||||
numberFormat,
|
||||
getBrowser,
|
||||
@@ -174,5 +183,6 @@ export {
|
||||
getColorFromTheme,
|
||||
localize,
|
||||
querySelector,
|
||||
querySelectorAll
|
||||
querySelectorAll,
|
||||
createObserver
|
||||
};
|
||||
Reference in new issue
Block a user