mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-02 19:01:21 +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
+290
-264
No files matched your search
@@ -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,148 +19,54 @@ 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";
|
const formattedDislike = numberFormat(response.dislikes);
|
||||||
}
|
setDislikes(formattedDislike);
|
||||||
|
storedData.dislikes = parseInt(response.dislikes);
|
||||||
|
storedData.likes = getLikeCountFromButton() || parseInt(response.likes);
|
||||||
|
createRateBar(storedData.likes, storedData.dislikes);
|
||||||
|
}
|
||||||
|
|
||||||
function getButtons() {
|
function setState() {
|
||||||
if (isMobile()) {
|
storedData.previousState = isVideoDisliked()
|
||||||
return document.querySelector(".slim-video-action-bar-actions");
|
? DISLIKED_STATE
|
||||||
}
|
: isVideoLiked()
|
||||||
//--- If Menu Element Is Displayed: ---//
|
? LIKED_STATE
|
||||||
if (document.getElementById("menu-container")?.offsetParent === null) {
|
: NEUTRAL_STATE;
|
||||||
return document.querySelector(
|
let statsSet = false;
|
||||||
"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);
|
|
||||||
setDislikes(formattedDislike);
|
|
||||||
storedData.dislikes = parseInt(response.dislikes);
|
|
||||||
storedData.likes = getLikeCountFromButton() || parseInt(response.likes);
|
|
||||||
createRateBar(storedData.likes, storedData.dislikes);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setState() {
|
|
||||||
storedData.previousState = isVideoDisliked()
|
|
||||||
? DISLIKED_STATE
|
|
||||||
: isVideoLiked()
|
|
||||||
? LIKED_STATE
|
|
||||||
: NEUTRAL_STATE;
|
|
||||||
let statsSet = false;
|
|
||||||
|
|
||||||
getBrowser().runtime.sendMessage(
|
getBrowser().runtime.sendMessage(
|
||||||
{
|
{
|
||||||
message: "set_state",
|
message: "set_state",
|
||||||
videoId: getVideoId(window.location.href),
|
videoId: getVideoId(window.location.href),
|
||||||
state: getState(storedData).current,
|
state: getState(storedData).current,
|
||||||
likeCount: getLikeCountFromButton() || null,
|
likeCount: getLikeCountFromButton() || null,
|
||||||
},
|
},
|
||||||
function (response) {
|
function (response) {
|
||||||
cLog("response from api:");
|
cLog("response from api:");
|
||||||
cLog(JSON.stringify(response));
|
cLog(JSON.stringify(response));
|
||||||
if (response !== undefined && !("traceId" in response) && !statsSet) {
|
if (response !== undefined && !("traceId" in response) && !statsSet) {
|
||||||
processResponse(response);
|
processResponse(response);
|
||||||
} else {
|
} else {
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
}
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function sendVote(vote) {
|
function sendVote(vote) {
|
||||||
getBrowser().runtime.sendMessage({
|
getBrowser().runtime.sendMessage({
|
||||||
@@ -155,122 +74,122 @@ 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);
|
||||||
storedData.dislikes--;
|
storedData.dislikes--;
|
||||||
storedData.likes++;
|
storedData.likes++;
|
||||||
createRateBar(storedData.likes, storedData.dislikes);
|
createRateBar(storedData.likes, storedData.dislikes);
|
||||||
setDislikes(numberFormat(storedData.dislikes));
|
setDislikes(numberFormat(storedData.dislikes));
|
||||||
storedData.previousState = LIKED_STATE;
|
storedData.previousState = LIKED_STATE;
|
||||||
} else if (storedData.previousState === NEUTRAL_STATE) {
|
} else if (storedData.previousState === NEUTRAL_STATE) {
|
||||||
sendVote(1);
|
sendVote(1);
|
||||||
storedData.likes++;
|
storedData.likes++;
|
||||||
createRateBar(storedData.likes, storedData.dislikes);
|
createRateBar(storedData.likes, storedData.dislikes);
|
||||||
storedData.previousState = LIKED_STATE;
|
storedData.previousState = LIKED_STATE;
|
||||||
} else if ((storedData.previousState = LIKED_STATE)) {
|
} else if ((storedData.previousState = LIKED_STATE)) {
|
||||||
sendVote(0);
|
sendVote(0);
|
||||||
storedData.likes--;
|
storedData.likes--;
|
||||||
createRateBar(storedData.likes, storedData.dislikes);
|
createRateBar(storedData.likes, storedData.dislikes);
|
||||||
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);
|
||||||
storedData.dislikes++;
|
storedData.dislikes++;
|
||||||
setDislikes(numberFormat(storedData.dislikes));
|
setDislikes(numberFormat(storedData.dislikes));
|
||||||
createRateBar(storedData.likes, storedData.dislikes);
|
createRateBar(storedData.likes, storedData.dislikes);
|
||||||
storedData.previousState = DISLIKED_STATE;
|
storedData.previousState = DISLIKED_STATE;
|
||||||
} else if (storedData.previousState === DISLIKED_STATE) {
|
} else if (storedData.previousState === DISLIKED_STATE) {
|
||||||
sendVote(0);
|
sendVote(0);
|
||||||
storedData.dislikes--;
|
storedData.dislikes--;
|
||||||
setDislikes(numberFormat(storedData.dislikes));
|
setDislikes(numberFormat(storedData.dislikes));
|
||||||
createRateBar(storedData.likes, storedData.dislikes);
|
createRateBar(storedData.likes, storedData.dislikes);
|
||||||
storedData.previousState = NEUTRAL_STATE;
|
storedData.previousState = NEUTRAL_STATE;
|
||||||
} else if (storedData.previousState === LIKED_STATE) {
|
} else if (storedData.previousState === LIKED_STATE) {
|
||||||
sendVote(-1);
|
sendVote(-1);
|
||||||
storedData.likes--;
|
storedData.likes--;
|
||||||
storedData.dislikes++;
|
storedData.dislikes++;
|
||||||
setDislikes(numberFormat(storedData.dislikes));
|
setDislikes(numberFormat(storedData.dislikes));
|
||||||
createRateBar(storedData.likes, storedData.dislikes);
|
createRateBar(storedData.likes, storedData.dislikes);
|
||||||
storedData.previousState = DISLIKED_STATE;
|
storedData.previousState = DISLIKED_STATE;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function setInitialState() {
|
function setInitialState() {
|
||||||
setState();
|
setState();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
sendVideoIds();
|
sendVideoIds();
|
||||||
}, 1500);
|
}, 1500);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getVideoId(url) {
|
||||||
|
const urlObject = new URL(url);
|
||||||
|
const pathname = urlObject.pathname;
|
||||||
|
if (pathname.startsWith("/clip")) {
|
||||||
|
return document.querySelector("meta[itemprop='videoId']").content;
|
||||||
|
} else {
|
||||||
|
return urlObject.searchParams.get("v");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getVideoId(url) {
|
function isVideoLoaded() {
|
||||||
const urlObject = new URL(url);
|
const videoId = getVideoId(window.location.href);
|
||||||
const pathname = urlObject.pathname;
|
return (
|
||||||
if (pathname.startsWith("/clip")) {
|
|
||||||
return document.querySelector("meta[itemprop='videoId']").content;
|
|
||||||
} else {
|
|
||||||
return urlObject.searchParams.get("v");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isVideoLoaded() {
|
|
||||||
const videoId = getVideoId(window.location.href);
|
|
||||||
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);
|
||||||
jsInitChecktimer = null;
|
jsInitChecktimer = null;
|
||||||
const buttons = getButtons();
|
const buttons = getButtons();
|
||||||
if (!window.returnDislikeButtonlistenersSet) {
|
if (!window.returnDislikeButtonlistenersSet) {
|
||||||
buttons.children[0].addEventListener("click", likeClicked);
|
buttons.children[0].addEventListener("click", likeClicked);
|
||||||
buttons.children[1].addEventListener("click", dislikeClicked);
|
buttons.children[1].addEventListener("click", dislikeClicked);
|
||||||
window.returnDislikeButtonlistenersSet = true;
|
window.returnDislikeButtonlistenersSet = true;
|
||||||
}
|
|
||||||
setInitialState();
|
|
||||||
}
|
}
|
||||||
}
|
setInitialState();
|
||||||
|
|
||||||
if (window.location.href.indexOf("watch?") >= 0) {
|
|
||||||
jsInitChecktimer = setInterval(checkForJS_Finish, 111);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createRateBar(likes, dislikes) {
|
if (window.location.href.indexOf("watch?") >= 0) {
|
||||||
let rateBar = document.getElementById("ryd-bar-container");
|
jsInitChecktimer = setInterval(checkForJS_Finish, 111);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const widthPx =
|
function createRateBar(likes, dislikes) {
|
||||||
getButtons().children[0].clientWidth +
|
let rateBar = document.getElementById("ryd-bar-container");
|
||||||
getButtons().children[1].clientWidth +
|
|
||||||
8;
|
|
||||||
|
|
||||||
const widthPercent =
|
const widthPx =
|
||||||
likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
|
getButtons().children[0].clientWidth +
|
||||||
|
getButtons().children[1].clientWidth +
|
||||||
|
8;
|
||||||
|
|
||||||
if (!rateBar) {
|
const widthPercent =
|
||||||
(
|
likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
|
||||||
document.getElementById("menu-container") ||
|
|
||||||
document.querySelector("ytm-slim-video-action-bar-renderer")
|
if (!rateBar) {
|
||||||
).insertAdjacentHTML(
|
(
|
||||||
"beforeend",
|
document.getElementById("menu-container") ||
|
||||||
`
|
document.querySelector("ytm-slim-video-action-bar-renderer")
|
||||||
|
).insertAdjacentHTML(
|
||||||
|
"beforeend",
|
||||||
|
`
|
||||||
<div class="ryd-tooltip" style="width: ${widthPx}px">
|
<div class="ryd-tooltip" style="width: ${widthPx}px">
|
||||||
<div class="ryd-tooltip-bar-container">
|
<div class="ryd-tooltip-bar-container">
|
||||||
<div
|
<div
|
||||||
@@ -288,52 +207,52 @@ function sendVote(vote) {
|
|||||||
</tp-yt-paper-tooltip>
|
</tp-yt-paper-tooltip>
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
document.getElementById("ryd-bar-container").style.width = widthPx + "px";
|
document.getElementById("ryd-bar-container").style.width = widthPx + "px";
|
||||||
document.getElementById("ryd-bar").style.width = widthPercent + "%";
|
document.getElementById("ryd-bar").style.width = widthPercent + "%";
|
||||||
|
|
||||||
document.querySelector(
|
document.querySelector(
|
||||||
"#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"
|
||||||
)
|
)
|
||||||
).concat(
|
).concat(
|
||||||
Array.from(
|
Array.from(
|
||||||
document.getElementsByClassName("yt-simple-endpoint ytd-thumbnail")
|
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"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
// Also try mobile
|
const ids = links
|
||||||
if (links.length < 1)
|
.filter((x) => x.href && x.href.indexOf("/watch?v=") > 0)
|
||||||
links = Array.from(
|
.map((x) => getVideoId(x.href));
|
||||||
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({
|
getBrowser().runtime.sendMessage({
|
||||||
message: "send_links",
|
message: "send_links",
|
||||||
videoIds: ids,
|
videoIds: ids,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setEventListeners();
|
||||||
|
|
||||||
|
document.addEventListener("yt-navigate-finish", function (event) {
|
||||||
|
if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
|
||||||
|
window.returnDislikeButtonlistenersSet = false;
|
||||||
setEventListeners();
|
setEventListeners();
|
||||||
|
});
|
||||||
|
|
||||||
document.addEventListener("yt-navigate-finish", function (event) {
|
setTimeout(() => sendVideoIds(), 2500);
|
||||||
if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
|
|
||||||
window.returnDislikeButtonlistenersSet = false;
|
|
||||||
setEventListeners();
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(() => sendVideoIds(), 2500);
|
|
||||||
|
|
||||||
function getBrowser() {
|
function getBrowser() {
|
||||||
if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") {
|
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 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,
|
||||||
|
|||||||
Reference in new issue
Block a user