create state.js, button.js and move functions into their own files, import at origin

This commit is contained in:
Leon Bubova committed 2021-12-17 23:50:25 +01:00
1 parent 3bc47f9e56
commit 4d9f706904
3 files changed
+150 -124

No files matched your search

+43 -124
View File
@@ -1,4 +1,17 @@
import { import {
getButtons,
getLikeButton,
getDislikeButton,
checkForSignInButton,
} from "./src/buttons";
import {
isMobile,
isVideoDisliked,
isVideoLiked,
getState,
setLikes,
setDislikes,
getLikeCountFromButton,
LIKED_STATE, LIKED_STATE,
DISLIKED_STATE, DISLIKED_STATE,
NEUTRAL_STATE, NEUTRAL_STATE,
@@ -6,124 +19,30 @@ import {
import { numberFormat } from "./src/utils"; import { numberFormat } from "./src/utils";
let storedData = { let storedData = {
likes: 0, likes: 0,
dislikes: 0, dislikes: 0,
previousState: NEUTRAL_STATE, previousState: NEUTRAL_STATE,
}; };
function cLog(message, writer) { function cLog(message, writer) {
message = `[return youtube dislike]: ${message}`; message = `[return youtube dislike]: ${message}`;
if (writer) { if (writer) {
writer(message); writer(message);
} else { } else {
console.log(message); console.log(message);
} }
} }
function isMobile() { function processResponse(response) {
return location.hostname == "m.youtube.com";
}
function getButtons() {
if (isMobile()) {
return document.querySelector(".slim-video-action-bar-actions");
}
//--- If Menu Element Is Displayed: ---//
if (document.getElementById("menu-container")?.offsetParent === null) {
return document.querySelector(
"ytd-menu-renderer.ytd-watch-metadata > div"
);
//--- If Menu Element Isnt Displayed: ---//
} else {
return document
.getElementById("menu-container")
?.querySelector("#top-level-buttons-computed");
}
}
function getLikeButton() {
return getButtons().children[0];
}
function getDislikeButton() {
return getButtons().children[1];
}
function isVideoLiked() {
if (isMobile()) {
return (
getLikeButton().querySelector("button").getAttribute("aria-label") ==
"true"
);
}
return getLikeButton().classList.contains("style-default-active");
}
function isVideoDisliked() {
if (isMobile()) {
return (
getDislikeButton().querySelector("button").getAttribute("aria-label") ==
"true"
);
}
return getDislikeButton().classList.contains("style-default-active");
}
function checkForSignInButton() {
if (
document.querySelector(
"a[href^='https://accounts.google.com/ServiceLogin']"
)
) {
return true;
} else {
return false;
}
}
function getState() {
if (isVideoLiked()) {
return { current: LIKED_STATE, previous: storedData.previousState };
}
if (isVideoDisliked()) {
return { current: DISLIKED_STATE, previous: storedData.previousState };
}
return { current: NEUTRAL_STATE, previous: storedData.previousState };
}
//--- Sets The Likes And Dislikes Values ---//
function setLikes(likesCount) {
(getButtons().children[0].querySelector("#text")).innerText = likesCount;
}
function setDislikes(dislikesCount) {
if (isMobile()) {
getButtons().children[1].querySelector(
".button-renderer-text"
).innerText = dislikesCount;
return;
}
(getButtons().children[1].querySelector("#text").innerText) = dislikesCount;
}
function getLikeCountFromButton() {
let likesStr = getLikeButton()
.querySelector("button")
.getAttribute("aria-label")
.replace(/\D/g, "");
return likesStr.length > 0 ? parseInt(likesStr) : false;
}
function processResponse(response) {
const formattedDislike = numberFormat(response.dislikes); const formattedDislike = numberFormat(response.dislikes);
setDislikes(formattedDislike); setDislikes(formattedDislike);
storedData.dislikes = parseInt(response.dislikes); storedData.dislikes = parseInt(response.dislikes);
storedData.likes = getLikeCountFromButton() || parseInt(response.likes); storedData.likes = getLikeCountFromButton() || parseInt(response.likes);
createRateBar(storedData.likes, storedData.dislikes); createRateBar(storedData.likes, storedData.dislikes);
} }
function setState() { function setState() {
storedData.previousState = isVideoDisliked() storedData.previousState = isVideoDisliked()
? DISLIKED_STATE ? DISLIKED_STATE
: isVideoLiked() : isVideoLiked()
@@ -147,7 +66,7 @@ import { numberFormat } from "./src/utils";
} }
} }
); );
} }
function sendVote(vote) { function sendVote(vote) {
getBrowser().runtime.sendMessage({ getBrowser().runtime.sendMessage({
@@ -155,9 +74,9 @@ function sendVote(vote) {
vote: vote, vote: vote,
videoId: getVideoId(window.location.href), videoId: getVideoId(window.location.href),
}); });
} }
function likeClicked() { function likeClicked() {
if (checkForSignInButton() === false) { if (checkForSignInButton() === false) {
if (storedData.previousState === DISLIKED_STATE) { if (storedData.previousState === DISLIKED_STATE) {
sendVote(1); sendVote(1);
@@ -178,9 +97,9 @@ function sendVote(vote) {
storedData.previousState = NEUTRAL_STATE; storedData.previousState = NEUTRAL_STATE;
} }
} }
} }
function dislikeClicked() { function dislikeClicked() {
if (checkForSignInButton() == false) { if (checkForSignInButton() == false) {
if (storedData.previousState === NEUTRAL_STATE) { if (storedData.previousState === NEUTRAL_STATE) {
sendVote(-1); sendVote(-1);
@@ -203,16 +122,16 @@ function sendVote(vote) {
storedData.previousState = DISLIKED_STATE; storedData.previousState = DISLIKED_STATE;
} }
} }
} }
function setInitialState() { function setInitialState() {
setState(); setState();
setTimeout(() => { setTimeout(() => {
sendVideoIds(); sendVideoIds();
}, 1500); }, 1500);
} }
function getVideoId(url) { function getVideoId(url) {
const urlObject = new URL(url); const urlObject = new URL(url);
const pathname = urlObject.pathname; const pathname = urlObject.pathname;
if (pathname.startsWith("/clip")) { if (pathname.startsWith("/clip")) {
@@ -220,20 +139,20 @@ function sendVote(vote) {
} else { } else {
return urlObject.searchParams.get("v"); return urlObject.searchParams.get("v");
} }
} }
function isVideoLoaded() { function isVideoLoaded() {
const videoId = getVideoId(window.location.href); const videoId = getVideoId(window.location.href);
return ( return (
document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null || document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null ||
// mobile: no video-id attribute // mobile: no video-id attribute
document.querySelector('#player[loading="false"]:not([hidden])') !== null document.querySelector('#player[loading="false"]:not([hidden])') !== null
); );
} }
let jsInitChecktimer = null; let jsInitChecktimer = null;
function setEventListeners(evt) { function setEventListeners(evt) {
function checkForJS_Finish() { function checkForJS_Finish() {
if (getButtons()?.offsetParent && isVideoLoaded()) { if (getButtons()?.offsetParent && isVideoLoaded()) {
clearInterval(jsInitChecktimer); clearInterval(jsInitChecktimer);
@@ -251,9 +170,9 @@ function sendVote(vote) {
if (window.location.href.indexOf("watch?") >= 0) { if (window.location.href.indexOf("watch?") >= 0) {
jsInitChecktimer = setInterval(checkForJS_Finish, 111); jsInitChecktimer = setInterval(checkForJS_Finish, 111);
} }
} }
function createRateBar(likes, dislikes) { function createRateBar(likes, dislikes) {
let rateBar = document.getElementById("ryd-bar-container"); let rateBar = document.getElementById("ryd-bar-container");
const widthPx = const widthPx =
@@ -297,9 +216,9 @@ function sendVote(vote) {
"#ryd-dislike-tooltip > #tooltip" "#ryd-dislike-tooltip > #tooltip"
).innerHTML = `${likes.toLocaleString()} / ${dislikes.toLocaleString()}`; ).innerHTML = `${likes.toLocaleString()} / ${dislikes.toLocaleString()}`;
} }
} }
function sendVideoIds() { function sendVideoIds() {
let links = Array.from( let links = Array.from(
document.getElementsByClassName( document.getElementsByClassName(
"yt-simple-endpoint ytd-compact-video-renderer" "yt-simple-endpoint ytd-compact-video-renderer"
@@ -323,17 +242,17 @@ function sendVote(vote) {
message: "send_links", message: "send_links",
videoIds: ids, videoIds: ids,
}); });
} }
setEventListeners(); setEventListeners();
document.addEventListener("yt-navigate-finish", function (event) { document.addEventListener("yt-navigate-finish", function (event) {
if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer); if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
window.returnDislikeButtonlistenersSet = false; window.returnDislikeButtonlistenersSet = false;
setEventListeners(); setEventListeners();
}); });
setTimeout(() => sendVideoIds(), 2500); setTimeout(() => sendVideoIds(), 2500);
function getBrowser() { function getBrowser() {
if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") { if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") {
+43
View File
@@ -0,0 +1,43 @@
import { isMobile } from "./state";
function getButtons() {
if (isMobile()) {
return document.querySelector(".slim-video-action-bar-actions");
}
//--- If Menu Element Is Displayed: ---//
if (document.getElementById("menu-container")?.offsetParent === null) {
return document.querySelector("ytd-menu-renderer.ytd-watch-metadata > div");
//--- If Menu Element Isnt Displayed: ---//
} else {
return document
.getElementById("menu-container")
?.querySelector("#top-level-buttons-computed");
}
}
function getLikeButton() {
return getButtons().children[0];
}
function getDislikeButton() {
return getButtons().children[1];
}
function checkForSignInButton() {
if (
document.querySelector(
"a[href^='https://accounts.google.com/ServiceLogin']"
)
) {
return true;
} else {
return false;
}
}
export {
getButtons,
getLikeButton,
getDislikeButton,
checkForSignInButton,
};
+64
View File
@@ -1,9 +1,73 @@
import { getLikeButton, getDislikeButton, getButtons } from "./buttons";
const LIKED_STATE = "LIKED_STATE"; const LIKED_STATE = "LIKED_STATE";
const DISLIKED_STATE = "DISLIKED_STATE"; const DISLIKED_STATE = "DISLIKED_STATE";
const NEUTRAL_STATE = "NEUTRAL_STATE"; const NEUTRAL_STATE = "NEUTRAL_STATE";
function isMobile() {
return location.hostname == "m.youtube.com";
}
function isVideoLiked() {
if (isMobile()) {
return (
getLikeButton().querySelector("button").getAttribute("aria-label") ==
"true"
);
}
return getLikeButton().classList.contains("style-default-active");
}
function isVideoDisliked() {
if (isMobile()) {
return (
getDislikeButton().querySelector("button").getAttribute("aria-label") ==
"true"
);
}
return getDislikeButton().classList.contains("style-default-active");
}
function getState(storedData) {
if (isVideoLiked()) {
return { current: LIKED_STATE, previous: storedData.previousState };
}
if (isVideoDisliked()) {
return { current: DISLIKED_STATE, previous: storedData.previousState };
}
return { current: NEUTRAL_STATE, previous: storedData.previousState };
}
//--- Sets The Likes And Dislikes Values ---//
function setLikes(likesCount) {
getButtons().children[0].querySelector("#text").innerText = likesCount;
}
function setDislikes(dislikesCount) {
if (isMobile()) {
getButtons().children[1].querySelector(".button-renderer-text").innerText =
dislikesCount;
return;
}
getButtons().children[1].querySelector("#text").innerText = dislikesCount;
}
function getLikeCountFromButton() {
let likesStr = getLikeButton()
.querySelector("button")
.getAttribute("aria-label")
.replace(/\D/g, "");
return likesStr.length > 0 ? parseInt(likesStr) : false;
}
export { export {
isMobile,
isVideoDisliked,
isVideoLiked,
getState,
setLikes,
setDislikes,
getLikeCountFromButton,
LIKED_STATE, LIKED_STATE,
DISLIKED_STATE, DISLIKED_STATE,
NEUTRAL_STATE, NEUTRAL_STATE,