mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-11-30 09:57:52 +01:00
add setState, processResponse to state.js, getVideoId to utils.js
This commit is contained in:
1 parent
d51a5a58b0
commit
689b0b7f8c
3 files changed
+51
-47
No files matched your search
@@ -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 (
|
||||
|
||||
Reference in new issue
Block a user