|
@@ -8,6 +8,7 @@ import {
|
|
getColorFromTheme,
|
|
getColorFromTheme,
|
|
} from "./utils";
|
|
} from "./utils";
|
|
import { sendVideoIds } from "./events";
|
|
import { sendVideoIds } from "./events";
|
|
|
|
+import { localize } from "./utils";
|
|
|
|
|
|
//TODO: Do not duplicate here and in ryd.background.js
|
|
//TODO: Do not duplicate here and in ryd.background.js
|
|
const apiUrl = "https://returnyoutubedislikeapi.com";
|
|
const apiUrl = "https://returnyoutubedislikeapi.com";
|
|
@@ -15,8 +16,6 @@ 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";
|
|
|
|
|
|
-const DISLIKES_DISABLED_TEXT = "DISLIKES DISABLED";
|
|
|
|
-
|
|
|
|
let extConfig = {
|
|
let extConfig = {
|
|
disableVoteSubmission: false,
|
|
disableVoteSubmission: false,
|
|
coloredThumbs: false,
|
|
coloredThumbs: false,
|
|
@@ -33,8 +32,6 @@ let storedData = {
|
|
previousState: NEUTRAL_STATE,
|
|
previousState: NEUTRAL_STATE,
|
|
};
|
|
};
|
|
|
|
|
|
-let likesDisabledState = true;
|
|
|
|
-
|
|
|
|
function isMobile() {
|
|
function isMobile() {
|
|
return location.hostname == "m.youtube.com";
|
|
return location.hostname == "m.youtube.com";
|
|
}
|
|
}
|
|
@@ -43,6 +40,46 @@ function isShorts() {
|
|
return location.pathname.startsWith("/shorts");
|
|
return location.pathname.startsWith("/shorts");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+let mutationObserver = new Object();
|
|
|
|
+
|
|
|
|
+if (isShorts() && mutationObserver.exists !== true) {
|
|
|
|
+ cLog('initializing mutation observer')
|
|
|
|
+ mutationObserver.options = {
|
|
|
|
+ childList: false,
|
|
|
|
+ attributes: true,
|
|
|
|
+ subtree: false
|
|
|
|
+ };
|
|
|
|
+ mutationObserver.exists = true;
|
|
|
|
+ mutationObserver.observer = new MutationObserver( function(mutationList, observer) {
|
|
|
|
+ mutationList.forEach( (mutation) => {
|
|
|
|
+ if (mutation.type === 'attributes' &&
|
|
|
|
+ mutation.target.nodeName === 'TP-YT-PAPER-BUTTON' &&
|
|
|
|
+ mutation.target.id === 'button') {
|
|
|
|
+ // cLog('Short thumb button status changed');
|
|
|
|
+ if (mutation.target.getAttribute('aria-pressed') === 'true') {
|
|
|
|
+ mutation.target.style.color =
|
|
|
|
+ (mutation.target.parentElement.parentElement.id === 'like-button') ?
|
|
|
|
+ getColorFromTheme(true) : getColorFromTheme(false);
|
|
|
|
+ } else {
|
|
|
|
+ mutation.target.style.color = 'unset';
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ cLog('unexpected mutation observer event: ' + mutation.target + mutation.type);
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+function isLikesDisabled() {
|
|
|
|
+ // return true if the like button's text doesn't contain any number
|
|
|
|
+ if (isMobile()) {
|
|
|
|
+ return /^\D*$/.test(
|
|
|
|
+ getButtons().children[0].querySelector(".button-renderer-text").innerText
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ return /^\D*$/.test(getButtons().children[0].querySelector("#text").innerText);
|
|
|
|
+}
|
|
|
|
+
|
|
function isVideoLiked() {
|
|
function isVideoLiked() {
|
|
if (isMobile()) {
|
|
if (isMobile()) {
|
|
return (
|
|
return (
|
|
@@ -79,7 +116,7 @@ function setLikes(likesCount) {
|
|
}
|
|
}
|
|
|
|
|
|
function setDislikes(dislikesCount) {
|
|
function setDislikes(dislikesCount) {
|
|
- if (!likesDisabledState) {
|
|
|
|
|
|
+ if (!isLikesDisabled()) {
|
|
if (isMobile()) {
|
|
if (isMobile()) {
|
|
getButtons().children[1].querySelector(
|
|
getButtons().children[1].querySelector(
|
|
".button-renderer-text"
|
|
".button-renderer-text"
|
|
@@ -92,11 +129,12 @@ function setDislikes(dislikesCount) {
|
|
if (isMobile()) {
|
|
if (isMobile()) {
|
|
getButtons().children[1].querySelector(
|
|
getButtons().children[1].querySelector(
|
|
".button-renderer-text"
|
|
".button-renderer-text"
|
|
- ).innerText = DISLIKES_DISABLED_TEXT;
|
|
|
|
|
|
+ ).innerText = localize("TextLikesDisabled");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- getButtons().children[1].querySelector("#text").innerText =
|
|
|
|
- DISLIKES_DISABLED_TEXT;
|
|
|
|
|
|
+ getButtons().children[1].querySelector("#text").innerText = localize(
|
|
|
|
+ "TextLikesDisabled"
|
|
|
|
+ );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -126,11 +164,31 @@ function processResponse(response, storedData) {
|
|
storedData.likes = getLikeCountFromButton() || parseInt(response.likes);
|
|
storedData.likes = getLikeCountFromButton() || parseInt(response.likes);
|
|
createRateBar(storedData.likes, storedData.dislikes);
|
|
createRateBar(storedData.likes, storedData.dislikes);
|
|
if (extConfig.coloredThumbs === true) {
|
|
if (extConfig.coloredThumbs === true) {
|
|
- getLikeButton().style.color = getColorFromTheme(true);
|
|
|
|
- getDislikeButton().style.color = getColorFromTheme(false);
|
|
|
|
|
|
+ if (isShorts()) { // for shorts, leave deactived buttons in default color
|
|
|
|
+ let shortLikeButton = getLikeButton().querySelector('tp-yt-paper-button#button');
|
|
|
|
+ let shortDislikeButton = getDislikeButton().querySelector('tp-yt-paper-button#button');
|
|
|
|
+ if (shortLikeButton.getAttribute('aria-pressed') === 'true') {
|
|
|
|
+ shortLikeButton.style.color = getColorFromTheme(true);
|
|
|
|
+ }
|
|
|
|
+ if (shortDislikeButton.getAttribute('aria-pressed') === 'true') {
|
|
|
|
+ shortDislikeButton.style.color = getColorFromTheme(false);
|
|
|
|
+ }
|
|
|
|
+ mutationObserver.observer.observe(shortLikeButton, mutationObserver.options);
|
|
|
|
+ mutationObserver.observer.observe(shortDislikeButton, mutationObserver.options);
|
|
|
|
+ } else {
|
|
|
|
+ getLikeButton().style.color = getColorFromTheme(true);
|
|
|
|
+ getDislikeButton().style.color = getColorFromTheme(false);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Tells the user if the API is down
|
|
|
|
+function displayError(error) {
|
|
|
|
+ getButtons().children[1].querySelector("#text").innerText = localize(
|
|
|
|
+ "textTempUnavailable"
|
|
|
|
+ );
|
|
|
|
+}
|
|
|
|
+
|
|
async function setState(storedData) {
|
|
async function setState(storedData) {
|
|
storedData.previousState = isVideoDisliked()
|
|
storedData.previousState = isVideoDisliked()
|
|
? DISLIKED_STATE
|
|
? DISLIKED_STATE
|
|
@@ -151,14 +209,14 @@ async function setState(storedData) {
|
|
},
|
|
},
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
+ .then((response) => {
|
|
|
|
+ if (!response.ok) displayError(response.error);
|
|
|
|
+ return response;
|
|
|
|
+ })
|
|
.then((response) => response.json())
|
|
.then((response) => response.json())
|
|
- .catch();
|
|
|
|
|
|
+ .catch(displayError);
|
|
cLog("response from api:");
|
|
cLog("response from api:");
|
|
cLog(JSON.stringify(response));
|
|
cLog(JSON.stringify(response));
|
|
- likesDisabledState =
|
|
|
|
- numberFormat(response.dislikes) == 0 &&
|
|
|
|
- numberFormat(response.likes) == 0 &&
|
|
|
|
- numberFormat(response.viewCount) == 0;
|
|
|
|
if (response !== undefined && !("traceId" in response) && !statsSet) {
|
|
if (response !== undefined && !("traceId" in response) && !statsSet) {
|
|
processResponse(response, storedData);
|
|
processResponse(response, storedData);
|
|
}
|
|
}
|
|
@@ -268,5 +326,5 @@ export {
|
|
extConfig,
|
|
extConfig,
|
|
initExtConfig,
|
|
initExtConfig,
|
|
storedData,
|
|
storedData,
|
|
- likesDisabledState,
|
|
|
|
|
|
+ isLikesDisabled
|
|
};
|
|
};
|