From 689b0b7f8c92fb51d085b7ab8bf90ab0f48c48c8 Mon Sep 17 00:00:00 2001 From: Leon Bubova Date: Sat, 18 Dec 2021 00:41:12 +0100 Subject: [PATCH] add setState, processResponse to state.js, getVideoId to utils.js --- Extensions/combined/ryd.content-script.js | 49 ++--------------------- Extensions/combined/src/state.js | 37 +++++++++++++++++ Extensions/combined/src/utils.js | 12 +++++- 3 files changed, 51 insertions(+), 47 deletions(-) diff --git a/Extensions/combined/ryd.content-script.js b/Extensions/combined/ryd.content-script.js index de85993..e1f5dce 100644 --- a/Extensions/combined/ryd.content-script.js +++ b/Extensions/combined/ryd.content-script.js @@ -9,6 +9,7 @@ import { isVideoDisliked, isVideoLiked, getState, + setState, setLikes, setDislikes, getLikeCountFromButton, @@ -16,7 +17,7 @@ import { DISLIKED_STATE, NEUTRAL_STATE, } from "./src/state"; -import { numberFormat, getBrowser, cLog } from "./src/utils"; +import { numberFormat, getBrowser, getVideoId, cLog } from "./src/utils"; import { createRateBar } from "./src/bar"; let storedData = { @@ -25,40 +26,6 @@ let storedData = { previousState: NEUTRAL_STATE, }; -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( - { - 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); - } else { - } - } - ); -} - function sendVote(vote) { getBrowser().runtime.sendMessage({ message: "send_vote", @@ -116,22 +83,12 @@ function dislikeClicked() { } function setInitialState() { - setState(); + setState(storedData); setTimeout(() => { sendVideoIds(); }, 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 isVideoLoaded() { const videoId = getVideoId(window.location.href); return ( diff --git a/Extensions/combined/src/state.js b/Extensions/combined/src/state.js index 82dca35..fa4df1b 100644 --- a/Extensions/combined/src/state.js +++ b/Extensions/combined/src/state.js @@ -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, diff --git a/Extensions/combined/src/utils.js b/Extensions/combined/src/utils.js index 0d26ab9..a7a12a5 100644 --- a/Extensions/combined/src/utils.js +++ b/Extensions/combined/src/utils.js @@ -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 } \ No newline at end of file +export { numberFormat, getBrowser, getVideoId, cLog } \ No newline at end of file