|
@@ -1,122 +1,187 @@
|
|
-(function(securityToken, extensionId) {
|
|
|
|
- function getButtons() {
|
|
|
|
- return document
|
|
|
|
- .getElementById("menu-container")
|
|
|
|
- ?.querySelector("#top-level-buttons-computed");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function getLikeButton() {
|
|
|
|
- return getButtons().children[0];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function getDislikeButton() {
|
|
|
|
- return getButtons().children[1];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function isVideoLiked() {
|
|
|
|
- return getLikeButton().classList.contains("style-default-active");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function isVideoDisliked() {
|
|
|
|
- return getDislikeButton().classList.contains("style-default-active");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function isVideoNotLiked() {
|
|
|
|
- return getLikeButton().classList.contains("style-text");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function isVideoNotDisliked() {
|
|
|
|
- return getDislikeButton().classList.contains("style-text");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function getState() {
|
|
|
|
- if (isVideoLiked()) {
|
|
|
|
- return "liked";
|
|
|
|
- }
|
|
|
|
- if (isVideoDisliked()) {
|
|
|
|
- return "disliked";
|
|
|
|
- }
|
|
|
|
- return "neutral";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function setLikes(likesCount) {
|
|
|
|
- getButtons().children[0].querySelector("#text").innerText = likesCount;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function setDislikes(dislikesCount) {
|
|
|
|
- getButtons().children[1].querySelector("#text").innerText = dislikesCount;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function setState() {
|
|
|
|
- chrome.runtime.sendMessage(extensionId, {
|
|
|
|
- securityToken: securityToken,
|
|
|
|
- message: "set_state",
|
|
|
|
- videoId: getVideoId(),
|
|
|
|
- state: getState(),
|
|
|
|
- },
|
|
|
|
- function(response) {
|
|
|
|
- if (response != undefined) {
|
|
|
|
- const formattedDislike = numberFormat(response.dislikes);
|
|
|
|
- // setLikes(response.likes);
|
|
|
|
- console.log(response);
|
|
|
|
- setDislikes(formattedDislike);
|
|
|
|
- } else {}
|
|
|
|
- }
|
|
|
|
- );
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function likeClicked() {
|
|
|
|
- console.log("like" + getState());
|
|
|
|
- setState();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function dislikeClicked() {
|
|
|
|
- console.log("dislike" + getState());
|
|
|
|
- setState();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function setInitalState() {
|
|
|
|
- setState();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function getVideoId() {
|
|
|
|
- const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
- const videoId = urlParams.get("v");
|
|
|
|
- return videoId;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function isVideoLoaded() {
|
|
|
|
- const videoId = getVideoId();
|
|
|
|
- return (
|
|
|
|
- document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
|
|
|
|
- );
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function numberFormat(numberState) {
|
|
|
|
- const userLocales = navigator.language;
|
|
|
|
- const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
|
|
|
|
- return formatter.format(numberState);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function setEventListeners(evt) {
|
|
|
|
- function checkForJS_Finish() {
|
|
|
|
- if (getButtons()?.offsetParent && isVideoLoaded()) {
|
|
|
|
- clearInterval(jsInitChecktimer);
|
|
|
|
- const buttons = getButtons();
|
|
|
|
- if (!window.returnDislikeButtonlistenersSet) {
|
|
|
|
- buttons.children[0].addEventListener("click", likeClicked);
|
|
|
|
- buttons.children[1].addEventListener("click", dislikeClicked);
|
|
|
|
- window.returnDislikeButtonlistenersSet = true;
|
|
|
|
- }
|
|
|
|
- setInitalState();
|
|
|
|
- }
|
|
|
|
|
|
+(function (extensionId) {
|
|
|
|
+ function getButtons() {
|
|
|
|
+ return document
|
|
|
|
+ .getElementById("menu-container")
|
|
|
|
+ ?.querySelector("#top-level-buttons-computed");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function getLikeButton() {
|
|
|
|
+ return getButtons().children[0];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function getDislikeButton() {
|
|
|
|
+ return getButtons().children[1];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function isVideoLiked() {
|
|
|
|
+ return getLikeButton().classList.contains("style-default-active");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function isVideoDisliked() {
|
|
|
|
+ return getDislikeButton().classList.contains("style-default-active");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function isVideoNotLiked() {
|
|
|
|
+ return getLikeButton().classList.contains("style-text");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function isVideoNotDisliked() {
|
|
|
|
+ return getDislikeButton().classList.contains("style-text");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function getState() {
|
|
|
|
+ if (isVideoLiked()) {
|
|
|
|
+ return "liked";
|
|
|
|
+ }
|
|
|
|
+ if (isVideoDisliked()) {
|
|
|
|
+ return "disliked";
|
|
|
|
+ }
|
|
|
|
+ return "neutral";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function setLikes(likesCount) {
|
|
|
|
+ getButtons().children[0].querySelector("#text").innerText = likesCount;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function setDislikes(dislikesCount) {
|
|
|
|
+ getButtons().children[1].querySelector("#text").innerText = dislikesCount;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function setState() {
|
|
|
|
+ chrome.runtime.sendMessage(
|
|
|
|
+ extensionId,
|
|
|
|
+ {
|
|
|
|
+ message: "set_state",
|
|
|
|
+ videoId: getVideoId(window.location.href),
|
|
|
|
+ state: getState(),
|
|
|
|
+ },
|
|
|
|
+ function (response) {
|
|
|
|
+ if (response != undefined) {
|
|
|
|
+ const formattedDislike = numberFormat(response.dislikes);
|
|
|
|
+ // setLikes(response.likes);
|
|
|
|
+ setDislikes(formattedDislike);
|
|
|
|
+ createRateBar(response.likes, response.dislikes);
|
|
|
|
+ } else {
|
|
}
|
|
}
|
|
-
|
|
|
|
- if (window.location.href.indexOf("watch?") >= 0) {
|
|
|
|
- var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
|
|
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function likeClicked() {
|
|
|
|
+ // console.log("like" + getState());
|
|
|
|
+ // setState();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function dislikeClicked() {
|
|
|
|
+ // console.log("dislike" + getState());
|
|
|
|
+ // setState();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function setInitalState() {
|
|
|
|
+ setState();
|
|
|
|
+ setTimeout(() => sendVideoIds(), 1500);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function getVideoId(url) {
|
|
|
|
+ const urlObject = new URL(url);
|
|
|
|
+ const videoId = urlObject.searchParams.get("v");
|
|
|
|
+ return videoId;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function isVideoLoaded() {
|
|
|
|
+ const videoId = getVideoId(window.location.href);
|
|
|
|
+ return (
|
|
|
|
+ document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function numberFormat(numberState) {
|
|
|
|
+ const userLocales = navigator.language;
|
|
|
|
+ const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
|
|
|
|
+ return formatter.format(numberState);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var jsInitChecktimer = null;
|
|
|
|
+
|
|
|
|
+ function setEventListeners(evt) {
|
|
|
|
+ function checkForJS_Finish() {
|
|
|
|
+ if (getButtons()?.offsetParent && isVideoLoaded()) {
|
|
|
|
+ clearInterval(jsInitChecktimer);
|
|
|
|
+ jsInitChecktimer = null;
|
|
|
|
+ const buttons = getButtons();
|
|
|
|
+ if (!window.returnDislikeButtonlistenersSet) {
|
|
|
|
+ buttons.children[0].addEventListener("click", likeClicked);
|
|
|
|
+ buttons.children[1].addEventListener("click", dislikeClicked);
|
|
|
|
+ window.returnDislikeButtonlistenersSet = true;
|
|
}
|
|
}
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ setInitalState();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (window.location.href.indexOf("watch?") >= 0) {
|
|
|
|
+ jsInitChecktimer = setInterval(checkForJS_Finish, 111);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function createRateBar(likes, dislikes) {
|
|
|
|
+ var rateBar = document.getElementById(
|
|
|
|
+ "return-youtube-dislike-bar-container"
|
|
|
|
+ );
|
|
|
|
+ const widthPx =
|
|
|
|
+ getButtons().children[0].clientWidth +
|
|
|
|
+ getButtons().children[1].clientWidth;
|
|
|
|
+
|
|
|
|
+ const widthPercent =
|
|
|
|
+ likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
|
|
|
|
+
|
|
|
|
+ if (!rateBar) {
|
|
|
|
+ document.getElementById("menu-container").insertAdjacentHTML(
|
|
|
|
+ "beforeend",
|
|
|
|
+ `<div id="return-youtube-dislike-bar-container"
|
|
|
|
+ style="width: ${widthPx}px;
|
|
|
|
+ height: 3px; margin-left: 6px;">
|
|
|
|
+ <div id="return-youtube-dislike-bar" style="width: ${widthPercent}%; height: 100%" ></div>
|
|
|
|
+ </div>`
|
|
|
|
+ );
|
|
|
|
+ } else {
|
|
|
|
+ document.getElementById(
|
|
|
|
+ "return-youtube-dislike-bar-container"
|
|
|
|
+ ).style.width = widthPx + "px";
|
|
|
|
+ document.getElementById("return-youtube-dislike-bar").style.width =
|
|
|
|
+ widthPercent + "%";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function sendVideoIds() {
|
|
|
|
+ const ids = Array.from(
|
|
|
|
+ document.getElementsByClassName(
|
|
|
|
+ "yt-simple-endpoint ytd-compact-video-renderer"
|
|
|
|
+ )
|
|
|
|
+ )
|
|
|
|
+ .concat(
|
|
|
|
+ Array.from(
|
|
|
|
+ document.getElementsByClassName("yt-simple-endpoint ytd-thumbnail")
|
|
|
|
+ )
|
|
|
|
+ )
|
|
|
|
+ .filter((x) => x.href && x.href.indexOf("/watch?v=") > 0)
|
|
|
|
+ .map((x) => getVideoId(x.href));
|
|
|
|
+ chrome.runtime.sendMessage(extensionId, {
|
|
|
|
+ message: "send_links",
|
|
|
|
+ videoIds: ids,
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ setEventListeners();
|
|
|
|
+
|
|
|
|
+ document.addEventListener("yt-navigate-finish", function (event) {
|
|
|
|
+ if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
|
|
|
|
+ window.returnDislikeButtonlistenersSet = false;
|
|
setEventListeners();
|
|
setEventListeners();
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ window.onscrollend = () => {
|
|
|
|
+ sendVideoIds();
|
|
|
|
+ };
|
|
|
|
|
|
-})(document.currentScript.getAttribute("security-token"), document.currentScript.getAttribute("extension-id"));
|
|
|
|
|
|
+ setTimeout(() => sendVideoIds(), 1500);
|
|
|
|
+})(document.currentScript.getAttribute("extension-id"));
|