mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-02 02:41:22 +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
@@ -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,
|
||||
};
|
||||
Reference in new issue
Block a user