mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-11-23 14:53:01 +01:00
also change the icon of the extension when voting is disabled popup.js, content-script, bg-script listenes to storage change event so that they can all stay in sync without passing messages icon_hold is addded advanced setting is enabled in popup.html add fallback for changing extension icon refactor stateData
116 lines
3.5 KiB
JavaScript
116 lines
3.5 KiB
JavaScript
import { getBrowser, getVideoId, numberFormat, cLog } from "./utils"
|
|
import { checkForSignInButton, getButtons } from "./buttons"
|
|
import { NEUTRAL_STATE, LIKED_STATE, DISLIKED_STATE, setDislikes, extConfig, storedData } from "./state"
|
|
import { createRateBar } from "./bar"
|
|
|
|
function sendVote(vote) {
|
|
if (extConfig.disableVoteSubmission !== true) {
|
|
getBrowser().runtime.sendMessage({
|
|
message: "send_vote",
|
|
vote: vote,
|
|
videoId: getVideoId(window.location.href),
|
|
});
|
|
}
|
|
}
|
|
|
|
function sendVideoIds() {
|
|
let links = Array.from(
|
|
document.getElementsByClassName(
|
|
"yt-simple-endpoint ytd-compact-video-renderer"
|
|
)
|
|
).concat(
|
|
Array.from(
|
|
document.getElementsByClassName("yt-simple-endpoint ytd-thumbnail")
|
|
)
|
|
);
|
|
// Also try mobile
|
|
if (links.length < 1)
|
|
links = Array.from(
|
|
document.querySelectorAll(
|
|
".large-media-item-metadata > a, a.large-media-item-thumbnail-container"
|
|
)
|
|
);
|
|
const ids = links
|
|
.filter((x) => x.href && x.href.indexOf("/watch?v=") > 0)
|
|
.map((x) => getVideoId(x.href));
|
|
getBrowser().runtime.sendMessage({
|
|
message: "send_links",
|
|
videoIds: ids,
|
|
});
|
|
}
|
|
|
|
function likeClicked() {
|
|
if (checkForSignInButton() === false) {
|
|
if (storedData.previousState === DISLIKED_STATE) {
|
|
sendVote(1);
|
|
storedData.dislikes--;
|
|
storedData.likes++;
|
|
createRateBar(storedData.likes, storedData.dislikes);
|
|
setDislikes(numberFormat(storedData.dislikes));
|
|
storedData.previousState = LIKED_STATE;
|
|
} else if (storedData.previousState === NEUTRAL_STATE) {
|
|
sendVote(1);
|
|
storedData.likes++;
|
|
createRateBar(storedData.likes, storedData.dislikes);
|
|
storedData.previousState = LIKED_STATE;
|
|
} else if ((storedData.previousState = LIKED_STATE)) {
|
|
sendVote(0);
|
|
storedData.likes--;
|
|
createRateBar(storedData.likes, storedData.dislikes);
|
|
storedData.previousState = NEUTRAL_STATE;
|
|
}
|
|
}
|
|
}
|
|
|
|
function dislikeClicked() {
|
|
if (checkForSignInButton() == false) {
|
|
if (storedData.previousState === NEUTRAL_STATE) {
|
|
sendVote(-1);
|
|
storedData.dislikes++;
|
|
setDislikes(numberFormat(storedData.dislikes));
|
|
createRateBar(storedData.likes, storedData.dislikes);
|
|
storedData.previousState = DISLIKED_STATE;
|
|
} else if (storedData.previousState === DISLIKED_STATE) {
|
|
sendVote(0);
|
|
storedData.dislikes--;
|
|
setDislikes(numberFormat(storedData.dislikes));
|
|
createRateBar(storedData.likes, storedData.dislikes);
|
|
storedData.previousState = NEUTRAL_STATE;
|
|
} else if (storedData.previousState === LIKED_STATE) {
|
|
sendVote(-1);
|
|
storedData.likes--;
|
|
storedData.dislikes++;
|
|
setDislikes(numberFormat(storedData.dislikes));
|
|
createRateBar(storedData.likes, storedData.dislikes);
|
|
storedData.previousState = DISLIKED_STATE;
|
|
}
|
|
}
|
|
}
|
|
|
|
function addLikeDislikeEventListener() {
|
|
const buttons = getButtons();
|
|
if (!window.returnDislikeButtonlistenersSet) {
|
|
buttons.children[0].addEventListener("click", likeClicked);
|
|
buttons.children[1].addEventListener("click", dislikeClicked);
|
|
window.returnDislikeButtonlistenersSet = true;
|
|
}
|
|
}
|
|
|
|
function storageChangeHandler(changes, area) {
|
|
if (changes.disableVoteSubmission !== undefined) {
|
|
handleDisableVoteSubmissionChangeEvent(changes.disableVoteSubmission.newValue);
|
|
}
|
|
}
|
|
|
|
function handleDisableVoteSubmissionChangeEvent(value) {
|
|
extConfig.disableVoteSubmission = value;
|
|
}
|
|
|
|
export {
|
|
sendVote,
|
|
sendVideoIds,
|
|
likeClicked,
|
|
dislikeClicked,
|
|
addLikeDislikeEventListener,
|
|
storageChangeHandler,
|
|
}; |