mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-04 11:51:20 +01:00
create state.js, button.js and move functions into their own files, import at origin
This commit is contained in:
1 parent
3bc47f9e56
commit
4d9f706904
3 files changed
+150
-124
No files matched your search
@@ -1,4 +1,17 @@
|
||||
import {
|
||||
getButtons,
|
||||
getLikeButton,
|
||||
getDislikeButton,
|
||||
checkForSignInButton,
|
||||
} from "./src/buttons";
|
||||
import {
|
||||
isMobile,
|
||||
isVideoDisliked,
|
||||
isVideoLiked,
|
||||
getState,
|
||||
setLikes,
|
||||
setDislikes,
|
||||
getLikeCountFromButton,
|
||||
LIKED_STATE,
|
||||
DISLIKED_STATE,
|
||||
NEUTRAL_STATE,
|
||||
@@ -6,124 +19,30 @@ import {
|
||||
|
||||
import { numberFormat } from "./src/utils";
|
||||
|
||||
let storedData = {
|
||||
let storedData = {
|
||||
likes: 0,
|
||||
dislikes: 0,
|
||||
previousState: NEUTRAL_STATE,
|
||||
};
|
||||
};
|
||||
|
||||
function cLog(message, writer) {
|
||||
function cLog(message, writer) {
|
||||
message = `[return youtube dislike]: ${message}`;
|
||||
if (writer) {
|
||||
writer(message);
|
||||
} else {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isMobile() {
|
||||
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) {
|
||||
function processResponse(response) {
|
||||
const formattedDislike = numberFormat(response.dislikes);
|
||||
setDislikes(formattedDislike);
|
||||
storedData.dislikes = parseInt(response.dislikes);
|
||||
storedData.likes = getLikeCountFromButton() || parseInt(response.likes);
|
||||
createRateBar(storedData.likes, storedData.dislikes);
|
||||
}
|
||||
}
|
||||
|
||||
function setState() {
|
||||
function setState() {
|
||||
storedData.previousState = isVideoDisliked()
|
||||
? DISLIKED_STATE
|
||||
: isVideoLiked()
|
||||
@@ -147,7 +66,7 @@ import { numberFormat } from "./src/utils";
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function sendVote(vote) {
|
||||
getBrowser().runtime.sendMessage({
|
||||
@@ -155,9 +74,9 @@ function sendVote(vote) {
|
||||
vote: vote,
|
||||
videoId: getVideoId(window.location.href),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function likeClicked() {
|
||||
function likeClicked() {
|
||||
if (checkForSignInButton() === false) {
|
||||
if (storedData.previousState === DISLIKED_STATE) {
|
||||
sendVote(1);
|
||||
@@ -178,9 +97,9 @@ function sendVote(vote) {
|
||||
storedData.previousState = NEUTRAL_STATE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function dislikeClicked() {
|
||||
function dislikeClicked() {
|
||||
if (checkForSignInButton() == false) {
|
||||
if (storedData.previousState === NEUTRAL_STATE) {
|
||||
sendVote(-1);
|
||||
@@ -203,16 +122,16 @@ function sendVote(vote) {
|
||||
storedData.previousState = DISLIKED_STATE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setInitialState() {
|
||||
function setInitialState() {
|
||||
setState();
|
||||
setTimeout(() => {
|
||||
sendVideoIds();
|
||||
}, 1500);
|
||||
}
|
||||
}
|
||||
|
||||
function getVideoId(url) {
|
||||
function getVideoId(url) {
|
||||
const urlObject = new URL(url);
|
||||
const pathname = urlObject.pathname;
|
||||
if (pathname.startsWith("/clip")) {
|
||||
@@ -220,20 +139,20 @@ function sendVote(vote) {
|
||||
} else {
|
||||
return urlObject.searchParams.get("v");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isVideoLoaded() {
|
||||
function isVideoLoaded() {
|
||||
const videoId = getVideoId(window.location.href);
|
||||
return (
|
||||
document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null ||
|
||||
// mobile: no video-id attribute
|
||||
document.querySelector('#player[loading="false"]:not([hidden])') !== null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let jsInitChecktimer = null;
|
||||
let jsInitChecktimer = null;
|
||||
|
||||
function setEventListeners(evt) {
|
||||
function setEventListeners(evt) {
|
||||
function checkForJS_Finish() {
|
||||
if (getButtons()?.offsetParent && isVideoLoaded()) {
|
||||
clearInterval(jsInitChecktimer);
|
||||
@@ -251,9 +170,9 @@ function sendVote(vote) {
|
||||
if (window.location.href.indexOf("watch?") >= 0) {
|
||||
jsInitChecktimer = setInterval(checkForJS_Finish, 111);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createRateBar(likes, dislikes) {
|
||||
function createRateBar(likes, dislikes) {
|
||||
let rateBar = document.getElementById("ryd-bar-container");
|
||||
|
||||
const widthPx =
|
||||
@@ -297,9 +216,9 @@ function sendVote(vote) {
|
||||
"#ryd-dislike-tooltip > #tooltip"
|
||||
).innerHTML = `${likes.toLocaleString()} / ${dislikes.toLocaleString()}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sendVideoIds() {
|
||||
function sendVideoIds() {
|
||||
let links = Array.from(
|
||||
document.getElementsByClassName(
|
||||
"yt-simple-endpoint ytd-compact-video-renderer"
|
||||
@@ -323,17 +242,17 @@ function sendVote(vote) {
|
||||
message: "send_links",
|
||||
videoIds: ids,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
setEventListeners();
|
||||
setEventListeners();
|
||||
|
||||
document.addEventListener("yt-navigate-finish", function (event) {
|
||||
document.addEventListener("yt-navigate-finish", function (event) {
|
||||
if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
|
||||
window.returnDislikeButtonlistenersSet = false;
|
||||
setEventListeners();
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(() => sendVideoIds(), 2500);
|
||||
setTimeout(() => sendVideoIds(), 2500);
|
||||
|
||||
function getBrowser() {
|
||||
if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") {
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -1,9 +1,73 @@
|
||||
import { getLikeButton, getDislikeButton, getButtons } from "./buttons";
|
||||
|
||||
const LIKED_STATE = "LIKED_STATE";
|
||||
const DISLIKED_STATE = "DISLIKED_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 {
|
||||
isMobile,
|
||||
isVideoDisliked,
|
||||
isVideoLiked,
|
||||
getState,
|
||||
setLikes,
|
||||
setDislikes,
|
||||
getLikeCountFromButton,
|
||||
LIKED_STATE,
|
||||
DISLIKED_STATE,
|
||||
NEUTRAL_STATE,
|
||||
|
||||
Reference in new issue
Block a user