mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-02 19:01:21 +01:00
Merge pull request #527 from CreaZyp154/display-error-when-api-down
Display 'temporarily unavailable' when the API can't be reached
This commit is contained in:
5 files changed
+52
-17
No files matched your search
@@ -26,9 +26,15 @@
|
|||||||
"textSettings": {
|
"textSettings": {
|
||||||
"message": "Disable like/dislike submission"
|
"message": "Disable like/dislike submission"
|
||||||
},
|
},
|
||||||
|
"textLikesDisabled": {
|
||||||
|
"message": "disabled by owner"
|
||||||
|
},
|
||||||
"textSettingsHover": {
|
"textSettingsHover": {
|
||||||
"message": "Stops counting your likes and dislikes."
|
"message": "Stops counting your likes and dislikes."
|
||||||
},
|
},
|
||||||
|
"textTempUnavailable": {
|
||||||
|
"message": "temporarily unavailable"
|
||||||
|
},
|
||||||
"textUpdate": {
|
"textUpdate": {
|
||||||
"message": "update to"
|
"message": "update to"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -23,12 +23,18 @@
|
|||||||
"legendSettings": {
|
"legendSettings": {
|
||||||
"message": "Paramètres"
|
"message": "Paramètres"
|
||||||
},
|
},
|
||||||
|
"textLikesDisabled": {
|
||||||
|
"message": "Désactivé par le créateur"
|
||||||
|
},
|
||||||
"textSettings": {
|
"textSettings": {
|
||||||
"message": "Désactiver l'envoi des likes/dislikes"
|
"message": "Désactiver l'envoi des likes/dislikes"
|
||||||
},
|
},
|
||||||
"textSettingsHover": {
|
"textSettingsHover": {
|
||||||
"message": "Arrête de compter les likes et les dislikes mis sur les vidéos."
|
"message": "Arrête de compter les likes et les dislikes mis sur les vidéos."
|
||||||
},
|
},
|
||||||
|
"textTempUnavailable": {
|
||||||
|
"message": "temporairement indisponible"
|
||||||
|
},
|
||||||
"textUpdate": {
|
"textUpdate": {
|
||||||
"message": "mettre à jour vers"
|
"message": "mettre à jour vers"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { getButtons } from "./buttons";
|
import { getButtons } from "./buttons";
|
||||||
import { likesDisabledState, extConfig, isMobile } from "./state";
|
import { extConfig, isMobile, isLikesDisabled } from "./state";
|
||||||
import { cLog, getColorFromTheme } from "./utils";
|
import { cLog, getColorFromTheme } from "./utils";
|
||||||
|
|
||||||
function createRateBar(likes, dislikes) {
|
function createRateBar(likes, dislikes) {
|
||||||
if (!likesDisabledState) {
|
if (!isLikesDisabled()) {
|
||||||
let rateBar = document.getElementById("ryd-bar-container");
|
let rateBar = document.getElementById("ryd-bar-container");
|
||||||
|
|
||||||
const widthPx =
|
const widthPx =
|
||||||
@@ -61,7 +62,9 @@ function createRateBar(likes, dislikes) {
|
|||||||
} else {
|
} else {
|
||||||
cLog("removing bar");
|
cLog("removing bar");
|
||||||
let ratebar = document.getElementById("ryd-bar-container");
|
let ratebar = document.getElementById("ryd-bar-container");
|
||||||
ratebar.parentNode.removeChild(ratebar);
|
if(ratebar) {
|
||||||
|
ratebar.parentNode.removeChild(ratebar);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
@@ -32,8 +31,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";
|
||||||
}
|
}
|
||||||
@@ -42,6 +39,16 @@ function isShorts() {
|
|||||||
return location.pathname.startsWith("/shorts");
|
return location.pathname.startsWith("/shorts");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 (
|
||||||
@@ -78,7 +85,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"
|
||||||
@@ -91,11 +98,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 =
|
getButtons().children[1].querySelector("#text").innerText = localize(
|
||||||
DISLIKES_DISABLED_TEXT;
|
"TextLikesDisabled"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +132,13 @@ function processResponse(response, storedData) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
@@ -144,14 +159,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);
|
||||||
}
|
}
|
||||||
@@ -250,5 +265,5 @@ export {
|
|||||||
extConfig,
|
extConfig,
|
||||||
initExtConfig,
|
initExtConfig,
|
||||||
storedData,
|
storedData,
|
||||||
likesDisabledState,
|
isLikesDisabled
|
||||||
};
|
};
|
||||||
@@ -29,6 +29,10 @@ function numberFormat(numberState) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function localize(localeString) {
|
||||||
|
return chrome.i18n.getMessage(localeString);
|
||||||
|
}
|
||||||
|
|
||||||
function getNumberFormatter(optionSelect) {
|
function getNumberFormatter(optionSelect) {
|
||||||
let formatterNotation;
|
let formatterNotation;
|
||||||
let formatterCompactDisplay;
|
let formatterCompactDisplay;
|
||||||
@@ -151,4 +155,5 @@ export {
|
|||||||
isVideoLoaded,
|
isVideoLoaded,
|
||||||
cLog,
|
cLog,
|
||||||
getColorFromTheme,
|
getColorFromTheme,
|
||||||
|
localize,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user