add setState, processResponse to state.js, getVideoId to utils.js

This commit is contained in:
Leon Bubova committed 2021-12-18 00:41:12 +01:00
1 parent d51a5a58b0
commit 689b0b7f8c
3 files changed
+51 -47

No files matched your search

+37
View File
@@ -1,4 +1,6 @@
import { getLikeButton, getDislikeButton, getButtons } from "./buttons";
import { createRateBar } from "./bar";
import { getBrowser, getVideoId, cLog, numberFormat } from "./utils";
const LIKED_STATE = "LIKED_STATE";
const DISLIKED_STATE = "DISLIKED_STATE";
@@ -60,11 +62,46 @@ function getLikeCountFromButton() {
return likesStr.length > 0 ? parseInt(likesStr) : false;
}
function processResponse(response, storedData) {
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) {
storedData.previousState = isVideoDisliked()
? DISLIKED_STATE
: isVideoLiked()
? LIKED_STATE
: NEUTRAL_STATE;
let statsSet = false;
getBrowser().runtime.sendMessage(
{
message: "set_state",
videoId: getVideoId(window.location.href),
state: getState(storedData).current,
likeCount: getLikeCountFromButton() || null,
},
function (response) {
cLog("response from api:");
cLog(JSON.stringify(response));
if (response !== undefined && !("traceId" in response) && !statsSet) {
processResponse(response, storedData);
} else {
}
}
);
}
export {
isMobile,
isVideoDisliked,
isVideoLiked,
getState,
setState,
setLikes,
setDislikes,
getLikeCountFromButton,
+11 -1
View File
@@ -39,6 +39,16 @@ function getBrowser() {
}
}
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 cLog(message, writer) {
message = `[return youtube dislike]: ${message}`;
if (writer) {
@@ -48,4 +58,4 @@ function cLog(message, writer) {
}
}
export { numberFormat, getBrowser, cLog }
export { numberFormat, getBrowser, getVideoId, cLog }