Merge branch 'main' into fix-onInstalled

This commit is contained in:
Dmitrii Selivanov authored and GitHub committed 2022-04-26 23:08:35 +02:00
commit 55cc2879ee
29 files changed
+615 -147

No files matched your search

@@ -2,7 +2,7 @@
// @name Return YouTube Dislike
// @namespace https://www.returnyoutubedislike.com/
// @homepage https://www.returnyoutubedislike.com/
// @version 0.9.0
// @version 3.0.0
// @encoding utf-8
// @description Return of the YouTube Dislike, Based off https://www.returnyoutubedislike.com/
// @icon https://github.com/Anarios/return-youtube-dislike/raw/main/Icons/Return%20Youtube%20Dislike%20-%20Transparent.png
@@ -22,6 +22,21 @@
// @grant GM_addStyle
// @run-at document-end
// ==/UserScript==
const extConfig = {
// BEGIN USER OPTIONS
// You may change the following variables to allowed values listed in the corresponding brackets (* means default). Keep the style and keywords intact.
showUpdatePopup: false, // [true, false*] Show a popup tab after extension update (See what's new)
disableVoteSubmission: false, // [true, false*] Disable like/dislike submission (Stops counting your likes and dislikes)
coloredThumbs: false, // [true, false*] Colorize thumbs (Use custom colors for thumb icons)
coloredBar: false, // [true, false*] Colorize ratio bar (Use custom colors for ratio bar)
colorTheme: "classic", // [classic*, accessible, neon] Color theme (red/green, blue/yellow, pink/cyan)
numberDisplayFormat: "compactShort", // [compactShort*, compactLong, standard] Number format (For non-English locale users, you may be able to improve appearance with a different option. Please file a feature request if your locale is not covered)
numberDisplayRoundDown: true, // [true*, false] Round down numbers (Show rounded down numbers)
numberDisplayReformatLikes: false, // [true, false*] Re-format like numbers (Make likes and dislikes format consistent)
// END USER OPTIONS
};
const LIKED_STATE = "LIKED_STATE";
const DISLIKED_STATE = "DISLIKED_STATE";
const NEUTRAL_STATE = "NEUTRAL_STATE";
@@ -82,6 +97,36 @@ function getDislikeButton() {
return getButtons().children[1];
}
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 isVideoLiked() {
if (isMobile) {
return (
@@ -154,6 +199,19 @@ function setDislikes(dislikesCount) {
getButtons().children[1].querySelector("#text").innerText = dislikesCount;
}
function getLikeCountFromButton() {
if (isShorts()) {
//Youtube Shorts don't work with this query. It's not nessecary; we can skip it and still see the results.
//It should be possible to fix this function, but it's not critical to showing the dislike count.
return 0;
}
let likesStr = getLikeButton()
.querySelector("button")
.getAttribute("aria-label")
.replace(/\D/g, "");
return likesStr.length > 0 ? parseInt(likesStr) : false;
}
(typeof GM_addStyle != "undefined"
? GM_addStyle
: (styles) => {
@@ -205,6 +263,13 @@ function createRateBar(likes, dislikes) {
likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
if (!rateBar && !isMobile) {
let colorLikeStyle = "";
let colorDislikeStyle = "";
if (extConfig.coloredBar) {
colorLikeStyle = "; background-color: " + getColorFromTheme(true);
colorDislikeStyle = "; background-color: " + getColorFromTheme(false);
}
document.getElementById("menu-container").insertAdjacentHTML(
"beforeend",
`
@@ -212,11 +277,11 @@ function createRateBar(likes, dislikes) {
<div class="ryd-tooltip-bar-container">
<div
id="return-youtube-dislike-bar-container"
style="width: 100%; height: 2px;"
style="width: 100%; height: 2px;${colorDislikeStyle}"
>
<div
id="return-youtube-dislike-bar"
style="width: ${widthPercent}%; height: 100%"
style="width: ${widthPercent}%; height: 100%${colorDislikeStyle}"
></div>
</div>
</div>
@@ -236,6 +301,13 @@ function createRateBar(likes, dislikes) {
document.querySelector(
"#ryd-dislike-tooltip > #tooltip"
).innerHTML = `${likes.toLocaleString()}&nbsp;/&nbsp;${dislikes.toLocaleString()}`;
if (extConfig.coloredBar) {
document.getElementById("return-youtube-dislike-bar-container").style.backgroundColor =
getColorFromTheme(false);
document.getElementById("return-youtube-dislike-bar").style.backgroundColor =
getColorFromTheme(true);
}
}
}
@@ -253,7 +325,30 @@ function setState() {
likesvalue = likes;
dislikesvalue = dislikes;
setDislikes(numberFormat(dislikes));
if (extConfig.numberDisplayReformatLikes === true) {
const nativeLikes = getLikeCountFromButton();
if (nativeLikes !== false) {
setLikes(numberFormat(nativeLikes));
}
}
createRateBar(likes, dislikes);
if (extConfig.coloredThumbs === true) {
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);
}
}
}
});
});
@@ -347,14 +442,73 @@ function numberFormat(numberState) {
?.getAttribute("href")
)?.searchParams?.get("locale");
} catch {}
let numberDisplay;
if (extConfig.numberDisplayRoundDown === false) {
numberDisplay = numberState;
} else {
numberDisplay = roundDown(numberState);
}
return getNumberFormatter(extConfig.numberDisplayFormat).format(
numberDisplay
);
}
function getNumberFormatter(optionSelect) {
let formatterNotation;
let formatterCompactDisplay;
switch (optionSelect) {
case "compactLong":
formatterNotation = "compact";
formatterCompactDisplay = "long";
break;
case "standard":
formatterNotation = "standard";
formatterCompactDisplay = "short";
break;
case "compactShort":
default:
formatterNotation = "compact";
formatterCompactDisplay = "short";
}
const formatter = Intl.NumberFormat(
document.documentElement.lang || userLocales || navigator.language,
{
notation: "compact",
notation: formatterNotation,
compactDisplay: formatterCompactDisplay,
}
);
return formatter;
}
return formatter.format(roundDown(numberState));
function getColorFromTheme(voteIsLike) {
let colorString;
switch (extConfig.colorTheme) {
case "accessible":
if (voteIsLike === true) {
colorString = "dodgerblue";
} else {
colorString = "gold";
}
break;
case "neon":
if (voteIsLike === true) {
colorString = "aqua";
} else {
colorString = "magenta";
}
break;
case "classic":
default:
if (voteIsLike === true) {
colorString = "lime";
} else {
colorString = "red";
}
}
return colorString;
}
function setEventListeners(evt) {
@@ -0,0 +1,50 @@
{
"extensionName": {
"message": "Return YouTube Dislike"
},
"extensionDesc": {
"message": "Macht Dislikes wieder sichtbar"
},
"textDeveloper": {
"message": "von Dmitry Selivanov & Community"
},
"linkWebsite": {
"message": "Website"
},
"linkFAQ": {
"message": "FAQ"
},
"linkDonate": {
"message": "Spenden"
},
"linkHelp": {
"message": "Hilfe"
},
"legendSettings": {
"message": "Einstellungen"
},
"textSettings": {
"message": "Deaktiviere Like/Dislike-Übermittlung"
},
"textSettingsHover": {
"message": "Stoppt deine Likes und Dislikes zu zählen."
},
"textUpdate": {
"message": "aktualisieren auf"
},
"version30installed": {
"message": "Version 4.0.0.0 installiert"
},
"whatsnew": {
"message": "Was ist neu"
},
"shortsSupport": {
"message": "YouTube Shorts Unterstützung"
},
"customColors": {
"message": "Benutzerdefinierte Farben für Dislike-Leiste und Buttons"
},
"customNumberFormats": {
"message": "Benutzerdefinierte Zahlenformate"
}
}
@@ -26,9 +26,15 @@
"textSettings": {
"message": "Disable like/dislike submission"
},
"textLikesDisabled": {
"message": "disabled by owner"
},
"textSettingsHover": {
"message": "Stops counting your likes and dislikes."
},
"textTempUnavailable": {
"message": "temporarily unavailable"
},
"textUpdate": {
"message": "update to"
},
@@ -23,12 +23,18 @@
"legendSettings": {
"message": "Paramètres"
},
"textLikesDisabled": {
"message": "Désactivé par le créateur"
},
"textSettings": {
"message": "Désactiver l'envoi des likes/dislikes"
},
"textSettingsHover": {
"message": "Arrête de compter les likes et les dislikes mis sur les vidéos."
},
"textTempUnavailable": {
"message": "temporairement indisponible"
},
"textUpdate": {
"message": "mettre à jour vers"
}
@@ -0,0 +1,50 @@
{
"extensionName": {
"message": "Επιστροφή του YouTube Dislike"
},
"extensionDesc": {
"message": "Επιστρέφει τα dislikes"
},
"textDeveloper": {
"message": "από τον Dmitry Selivanov & την κοινότητα"
},
"linkWebsite": {
"message": "Ιστοσελίδα"
},
"linkFAQ": {
"message": "Γρήγορες Ερωτήσιες & Απαντήσεις"
},
"linkDonate": {
"message": "Στηρίξτε"
},
"linkHelp": {
"message": "Βοήθεια"
},
"legendSettings": {
"message": "Ρυθμίσεις"
},
"textSettings": {
"message": "Διακοπή υποβολής like/dislike"
},
"textSettingsHover": {
"message": "Σταματάει να μετράει τα likes & dislikes που κάνεις."
},
"textUpdate": {
"message": "ενημέρωση σε"
},
"version30installed": {
"message": "Εκδοση 4.0.0.0 εγκαταστάθηκε"
},
"whatsnew": {
"message": "Τι είναι νέο;"
},
"shortsSupport": {
"message": "Υποστήριξη για YouTube Shorts"
},
"customColors": {
"message": "Προσαρμοζόμενα χρώματα για τα κουμπιά και την μπάρα dislike"
},
"customNumberFormats": {
"message": "Προσαρμοζόμενη μορφή αριθμών."
}
}
@@ -0,0 +1,50 @@
{
"extensionName": {
"message": "Return YouTube Dislike"
},
"extensionDesc": {
"message": "Ripristina la possibilità di vedere i dislikes"
},
"textDeveloper": {
"message": "di Dmitry Selivanov e la Community"
},
"linkWebsite": {
"message": "Sito web"
},
"linkFAQ": {
"message": "Domande frequenti"
},
"linkDonate": {
"message": "Fai una donazione"
},
"linkHelp": {
"message": "Aiuto"
},
"legendSettings": {
"message": "Impostazioni"
},
"textSettings": {
"message": "Disabilita l'invio di like/dislike"
},
"textSettingsHover": {
"message": "Interrompe il conteggio dei tuoi likes e dislikes."
},
"textUpdate": {
"message": "aggiorna a"
},
"version30installed": {
"message": "Versione 4.0.0.0 installata"
},
"whatsnew": {
"message": "Cosa c'è di nuovo"
},
"shortsSupport": {
"message": "Supporto per YouTube Shorts"
},
"customColors": {
"message": "Colori personalizzati per la barra dei dislike e i bottoni"
},
"customNumberFormats": {
"message": "Formati numerici personalizzati"
}
}
@@ -42,7 +42,7 @@
"message": "Поддержка YouTube Shorts"
},
"customColors": {
"message": "Выбор цветов дизлайк бара и кнопок"
"message": "Выбор цветов панели дизлайков и кнопок"
},
"customNumberFormats": {
"message": "Выбор формата чисел"
+1 -1
View File
@@ -2,7 +2,7 @@
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDesc__",
"default_locale": "en",
"version": "2.1.0.3",
"version": "3.0.0.1",
"manifest_version": 3,
"background": {
"service_worker": "ryd.background.js"
+1 -1
View File
@@ -2,7 +2,7 @@
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDesc__",
"default_locale": "en",
"version": "2.1.0.3",
"version": "3.0.0.1",
"manifest_version": 2,
"background": {
"scripts": ["ryd.background.js"]
+53 -47
View File
@@ -12,54 +12,54 @@
/>
</head>
<body>
<center>
<svg width="48" viewBox="0 0 24 24">
<path
d="M14.9 3H6c-.9 0-1.6.5-1.9 1.2l-3 7c-.1.3-.1.5-.1.7v2c0 1.1.9 2 2 2h6.3l-.9 4.5c-.1.5 0 1 .4 1.4l1.1 1.1 6.5-6.6c.4-.4.6-.9.6-1.4V5c-.1-1.1-1-2-2.1-2zm7.4 12.8h-2.9c-.4 0-.7-.3-.7-.7V3.9c0-.4.3-.7.7-.7h2.9c.4 0 .7.3.7.7V15c0 .4-.3.8-.7.8z"
fill="red"
<center>
<svg width="48" viewBox="0 0 24 24">
<path
d="M14.9 3H6c-.9 0-1.6.5-1.9 1.2l-3 7c-.1.3-.1.5-.1.7v2c0 1.1.9 2 2 2h6.3l-.9 4.5c-.1.5 0 1 .4 1.4l1.1 1.1 6.5-6.6c.4-.4.6-.9.6-1.4V5c-.1-1.1-1-2-2.1-2zm7.4 12.8h-2.9c-.4 0-.7-.3-.7-.7V3.9c0-.4.3-.7.7-.7h2.9c.4 0 .7.3.7.7V15c0 .4-.3.8-.7.8z"
fill="red"
/>
<path d="m8 12.5 5.1-2.9L8 6.7v5.8z" fill="#fff"/>
</svg>
<h1 style="margin-bottom: 0.75rem" title="__MSG_extensionName__">
__MSG_extensionName__
</h1>
<p style="color: var(--lightGrey)" title="__MSG_textDeveloper__">
__MSG_textDeveloper__
</p>
<button id="link_website" title="__MSG_linkWebsite__">
__MSG_linkWebsite__
</button>
<button style="margin-top: 0.3em; background: #ff0000" id="link_donate" title="__MSG_linkDonate__">
__MSG_linkDonate__
</button>
<button id="link_discord">Discord</button>
<br/>
<button style="margin-top: 0.3rem" id="link_faq" title="__MSG_linkFAQ__">
__MSG_linkFAQ__
</button>
<button id="link_github">GitHub</button>
<button style="margin-top: 0.3em" id="link_help" title="__MSG_linkHelp__">
__MSG_linkHelp__
</button>
<br>
<button style="margin-top: 0.3em" id="link_changelog" title="__MSG_linkChangelog__">
Change Log
</button>
<br/>
<br/>
<p style="display: none">API Status: <b id="status"></b></p>
<img
id="server-status"
style="display: none; width: 0.75rem; height: 0.75rem"
src="./icons/server.svg"
alt=""
/>
<path d="m8 12.5 5.1-2.9L8 6.7v5.8z" fill="#fff"/>
</svg>
<h1 style="margin-bottom: 0.75rem" title="__MSG_extensionName__">
__MSG_extensionName__
</h1>
<p style="color: var(--lightGrey)" title="__MSG_textDeveloper__">
__MSG_textDeveloper__
</p>
<button id="link_website" title="__MSG_linkWebsite__">
__MSG_linkWebsite__
</button>
<button
style="margin-top: 0.3em; background: #ff0000"
id="link_donate"
title="__MSG_linkDonate__"
>
__MSG_linkDonate__
</button>
<button id="link_discord">Discord</button>
<br/>
<button style="margin-top: 0.3rem" id="link_faq" title="__MSG_linkFAQ__">
__MSG_linkFAQ__
</button>
<button id="link_github">GitHub</button>
<button style="margin-top: 0.3em" id="link_help" title="__MSG_linkHelp__">
__MSG_linkHelp__
</button>
<br/>
<br/>
<p style="display: none">API Status: <b id="status"></b></p>
<img
id="server-status"
style="display: none; width: 0.75rem; height: 0.75rem"
src="./icons/server.svg"
alt=""
/>
<br/>
<br/>
</center>
<br/>
<br/>
</center>
<!-- top-right -->
<button id="advancedToggle">
@@ -108,6 +108,12 @@
<span class="switchLabel">Show rounded down numbers</span>
</label>
<br/>
<label class="switch" data-hover="Make likes and dislikes format consistent">
<input type="checkbox" id="number_reformat_likes"/>
<span class="slider"/>
<span class="switchLabel">Re-format like numbers</span>
</label>
<br/>
<div class="custom-select">
<label for="number_format">Number format:</label>
<select name="number_format" id="number_format">
+22
View File
@@ -7,6 +7,7 @@ const config = {
colorTheme: "classic",
numberDisplayFormat: "compactShort",
numberDisplayRoundDown: true,
numberDisplayReformatLikes: false,
showAdvancedMessage:
'<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor"><rect fill="none" height="24" width="24"/><path d="M19.5,12c0-0.23-0.01-0.45-0.03-0.68l1.86-1.41c0.4-0.3,0.51-0.86,0.26-1.3l-1.87-3.23c-0.25-0.44-0.79-0.62-1.25-0.42 l-2.15,0.91c-0.37-0.26-0.76-0.49-1.17-0.68l-0.29-2.31C14.8,2.38,14.37,2,13.87,2h-3.73C9.63,2,9.2,2.38,9.14,2.88L8.85,5.19 c-0.41,0.19-0.8,0.42-1.17,0.68L5.53,4.96c-0.46-0.2-1-0.02-1.25,0.42L2.41,8.62c-0.25,0.44-0.14,0.99,0.26,1.3l1.86,1.41 C4.51,11.55,4.5,11.77,4.5,12s0.01,0.45,0.03,0.68l-1.86,1.41c-0.4,0.3-0.51,0.86-0.26,1.3l1.87,3.23c0.25,0.44,0.79,0.62,1.25,0.42 l2.15-0.91c0.37,0.26,0.76,0.49,1.17,0.68l0.29,2.31C9.2,21.62,9.63,22,10.13,22h3.73c0.5,0,0.93-0.38,0.99-0.88l0.29-2.31 c0.41-0.19,0.8-0.42,1.17-0.68l2.15,0.91c0.46,0.2,1,0.02,1.25-0.42l1.87-3.23c0.25-0.44,0.14-0.99-0.26-1.3l-1.86-1.41 C19.49,12.45,19.5,12.23,19.5,12z M12.04,15.5c-1.93,0-3.5-1.57-3.5-3.5s1.57-3.5,3.5-3.5s3.5,1.57,3.5,3.5S13.97,15.5,12.04,15.5z"/></svg>',
hideAdvancedMessage:
@@ -19,6 +20,7 @@ const config = {
donate: "https://returnyoutubedislike.com/donate",
faq: "https://returnyoutubedislike.com/faq",
help: "https://returnyoutubedislike.com/help",
changelog: "/changelog/3/changelog_3.0.html"
},
};
@@ -49,6 +51,7 @@ createLink(config.links.discord, "link_discord");
createLink(config.links.faq, "link_faq");
createLink(config.links.donate, "link_donate");
createLink(config.links.help, "link_help");
createLink(config.links.changelog, "link_changelog");
function createLink(url, id) {
document.getElementById(id).addEventListener("click", () => {
@@ -83,6 +86,10 @@ document.getElementById("number_format").addEventListener("change", (ev) => {
chrome.storage.sync.set({ numberDisplayFormat: ev.target.value });
});
document.getElementById("number_reformat_likes").addEventListener("click", (ev) => {
chrome.storage.sync.set({ numberDisplayReformatLikes: ev.target.checked });
});
/* Advanced Toggle */
const advancedToggle = document.getElementById("advancedToggle");
advancedToggle.addEventListener("click", () => {
@@ -112,6 +119,7 @@ function initConfig() {
initializeColorTheme();
initializeNumberDisplayFormat();
initializeNumberDisplayRoundDown();
initializeNumberDisplayReformatLikes();
}
function initializeVersionNumber() {
@@ -204,6 +212,12 @@ function updateNumberDisplayFormatContent(roundDown) {
getNumberFormatter("standard").format(testValue);
}
function initializeNumberDisplayReformatLikes() {
chrome.storage.sync.get(["numberDisplayReformatLikes"], (res) => {
handleNumberDisplayReformatLikesChangeEvent(res.numberDisplayReformatLikes);
});
}
chrome.storage.onChanged.addListener(storageChangeHandler);
function storageChangeHandler(changes, area) {
@@ -229,6 +243,9 @@ function storageChangeHandler(changes, area) {
if (changes.numberDisplayFormat !== undefined) {
handleNumberDisplayFormatChangeEvent(changes.numberDisplayFormat.newValue);
}
if (changes.numberDisplayReformatLikes !== undefined) {
handleNumberDisplayReformatLikesChangeEvent(changes.numberDisplayReformatLikes.newValue);
}
}
function handleDisableVoteSubmissionChangeEvent(value) {
@@ -276,6 +293,11 @@ function handleNumberDisplayFormatChangeEvent(value) {
.querySelector('option[value="' + value + '"]').selected = true;
}
function handleNumberDisplayReformatLikesChangeEvent(value) {
config.numberDisplayReformatLikes = value;
document.getElementById("number_reformat_likes").checked = value;
}
function getNumberFormatter(optionSelect) {
let formatterNotation;
let formatterCompactDisplay;
+31 -2
View File
@@ -9,10 +9,9 @@ let extConfig = {
coloredThumbs: false,
coloredBar: false,
colorTheme: "classic", // classic, accessible, neon
// coloredThumbs: false,
// coloredBar: false,
numberDisplayFormat: "compactShort", // compactShort, compactLong, standard
numberDisplayRoundDown: true, // locale 'de' shows exact numbers by default
numberDisplayReformatLikes: false, // use existing (native) likes number
};
if (isChrome()) api = chrome;
@@ -86,6 +85,18 @@ api.runtime.onInstalled.addListener((details) => {
api.tabs.create({url: api.runtime.getURL("/changelog/3/changelog_3.0.html")});
})
// api.storage.sync.get(['lastShowChangelogVersion'], (details) => {
// if (extConfig.showUpdatePopup === true &&
// details.lastShowChangelogVersion !== chrome.runtime.getManifest().version
// ) {
// // keep it inside get to avoid race condition
// api.storage.sync.set({'lastShowChangelogVersion ': chrome.runtime.getManifest().version});
// // wait until async get runs & don't steal tab focus
// api.tabs.create({url: api.runtime.getURL("/changelog/3/changelog_3.0.html"), active: false});
// }
// });
async function sendVote(videoId, vote) {
api.storage.sync.get(null, async (storageResult) => {
if (!storageResult.userId || !storageResult.registrationConfirmed) {
@@ -271,6 +282,9 @@ function storageChangeHandler(changes, area) {
if (changes.numberDisplayFormat !== undefined) {
handleNumberDisplayFormatChangeEvent(changes.numberDisplayFormat.newValue);
}
if (changes.numberDisplayReformatLikes !== undefined) {
handleNumberDisplayReformatLikesChangeEvent(changes.numberDisplayReformatLikes.newValue);
}
}
function handleDisableVoteSubmissionChangeEvent(value) {
@@ -313,6 +327,10 @@ function handleColorThemeChangeEvent(value) {
extConfig.colorTheme = value;
}
function handleNumberDisplayReformatLikesChangeEvent(value) {
extConfig.numberDisplayReformatLikes = value;
}
api.storage.onChanged.addListener(storageChangeHandler);
function initExtConfig() {
@@ -322,6 +340,7 @@ function initExtConfig() {
initializeColorTheme();
initializeNumberDisplayFormat();
initializeNumberDisplayRoundDown();
initializeNumberDisplayReformatLikes();
}
function initializeDisableVoteSubmission() {
@@ -385,6 +404,16 @@ function initializeNumberDisplayFormat() {
});
}
function initializeNumberDisplayReformatLikes() {
api.storage.sync.get(["numberDisplayReformatLikes"], (res) => {
if (res.numberDisplayReformatLikes === undefined) {
api.storage.sync.set({ numberDisplayReformatLikes: false });
} else {
extConfig.numberDisplayReformatLikes = res.numberDisplayReformatLikes;
}
});
}
function isChrome() {
return typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined";
}
@@ -34,7 +34,6 @@ import {
} from "./src/utils";
import { createRateBar } from "./src/bar";
import {
sendVideoIds,
sendVote,
likeClicked,
dislikeClicked,
@@ -67,5 +66,3 @@ document.addEventListener("yt-navigate-finish", function (event) {
window.returnDislikeButtonlistenersSet = false;
setEventListeners();
});
setTimeout(() => sendVideoIds(), 2500);
+6 -3
View File
@@ -1,8 +1,9 @@
import { getButtons } from "./buttons";
import { likesDisabledState, extConfig, isMobile } from "./state";
import { extConfig, isMobile, isLikesDisabled } from "./state";
import { cLog, getColorFromTheme } from "./utils";
function createRateBar(likes, dislikes) {
if (!likesDisabledState) {
if (!isLikesDisabled()) {
let rateBar = document.getElementById("ryd-bar-container");
const widthPx =
@@ -61,7 +62,9 @@ function createRateBar(likes, dislikes) {
} else {
cLog("removing bar");
let ratebar = document.getElementById("ryd-bar-container");
ratebar.parentNode.removeChild(ratebar);
if(ratebar) {
ratebar.parentNode.removeChild(ratebar);
}
}
}
+7 -27
View File
@@ -21,32 +21,6 @@ function sendVote(vote) {
}
}
function sendVideoIds() {
let links = Array.from(
document.getElementsByClassName(
"yt-simple-endpoint ytd-compact-video-renderer"
)
).concat(
Array.from(
document.getElementsByClassName("yt-simple-endpoint ytd-thumbnail")
)
);
// Also try mobile
if (links.length < 1)
links = Array.from(
document.querySelectorAll(
".large-media-item-metadata > a, a.large-media-item-thumbnail-container"
)
);
const ids = links
.filter((x) => x.href && x.href.indexOf("/watch?v=") > 0)
.map((x) => getVideoId(x.href));
getBrowser().runtime.sendMessage({
message: "send_links",
videoIds: ids,
});
}
function likeClicked() {
if (checkForSignInButton() === false) {
if (storedData.previousState === DISLIKED_STATE) {
@@ -130,6 +104,9 @@ function storageChangeHandler(changes, area) {
if (changes.numberDisplayFormat !== undefined) {
handleNumberDisplayFormatChangeEvent(changes.numberDisplayFormat.newValue);
}
if (changes.numberDisplayReformatLikes !== undefined) {
handleNumberDisplayReformatLikesChangeEvent(changes.numberDisplayReformatLikes.newValue);
}
}
function handleDisableVoteSubmissionChangeEvent(value) {
@@ -157,9 +134,12 @@ function handleNumberDisplayRoundDownChangeEvent(value) {
extConfig.numberDisplayRoundDown = value;
}
function handleNumberDisplayReformatLikesChangeEvent(value) {
extConfig.numberDisplayReformatLikes = value;
}
export {
sendVote,
sendVideoIds,
likeClicked,
dislikeClicked,
addLikeDislikeEventListener,
+111 -19
View File
@@ -7,7 +7,7 @@ import {
numberFormat,
getColorFromTheme,
} from "./utils";
import { sendVideoIds } from "./events";
import { localize } from "./utils";
//TODO: Do not duplicate here and in ryd.background.js
const apiUrl = "https://returnyoutubedislikeapi.com";
@@ -15,8 +15,6 @@ const LIKED_STATE = "LIKED_STATE";
const DISLIKED_STATE = "DISLIKED_STATE";
const NEUTRAL_STATE = "NEUTRAL_STATE";
const DISLIKES_DISABLED_TEXT = "DISLIKES DISABLED";
let extConfig = {
disableVoteSubmission: false,
coloredThumbs: false,
@@ -24,6 +22,7 @@ let extConfig = {
colorTheme: "classic",
numberDisplayFormat: "compactShort",
numberDisplayRoundDown: true,
numberDisplayReformatLikes: false,
};
let storedData = {
@@ -32,8 +31,6 @@ let storedData = {
previousState: NEUTRAL_STATE,
};
let likesDisabledState = true;
function isMobile() {
return location.hostname == "m.youtube.com";
}
@@ -42,6 +39,56 @@ function isShorts() {
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() {
if (isMobile()) {
return (
@@ -78,7 +125,7 @@ function setLikes(likesCount) {
}
function setDislikes(dislikesCount) {
if (!likesDisabledState) {
if (!isLikesDisabled()) {
if (isMobile()) {
getButtons().children[1].querySelector(
".button-renderer-text"
@@ -91,11 +138,11 @@ function setDislikes(dislikesCount) {
if (isMobile()) {
getButtons().children[1].querySelector(
".button-renderer-text"
).innerText = DISLIKES_DISABLED_TEXT;
).innerText = localize("TextLikesDisabled");
return;
}
getButtons().children[1].querySelector("#text").innerText =
DISLIKES_DISABLED_TEXT;
localize("TextLikesDisabled");
}
}
@@ -115,15 +162,52 @@ function getLikeCountFromButton() {
function processResponse(response, storedData) {
const formattedDislike = numberFormat(response.dislikes);
setDislikes(formattedDislike);
if (extConfig.numberDisplayReformatLikes === true) {
const nativeLikes = getLikeCountFromButton();
if (nativeLikes !== false) {
setLikes(numberFormat(nativeLikes));
}
}
storedData.dislikes = parseInt(response.dislikes);
storedData.likes = getLikeCountFromButton() || parseInt(response.likes);
createRateBar(storedData.likes, storedData.dislikes);
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) {
storedData.previousState = isVideoDisliked()
? DISLIKED_STATE
@@ -144,14 +228,14 @@ async function setState(storedData) {
},
}
)
.then((response) => {
if (!response.ok) displayError(response.error);
return response;
})
.then((response) => response.json())
.catch();
.catch(displayError);
cLog("response from api:");
cLog(JSON.stringify(response));
likesDisabledState =
numberFormat(response.dislikes) == 0 &&
numberFormat(response.likes) == 0 &&
numberFormat(response.viewCount) == 0;
if (response !== undefined && !("traceId" in response) && !statsSet) {
processResponse(response, storedData);
}
@@ -159,9 +243,6 @@ async function setState(storedData) {
function setInitialState() {
setState(storedData);
setTimeout(() => {
sendVideoIds();
}, 1500);
}
function initExtConfig() {
@@ -171,6 +252,7 @@ function initExtConfig() {
initializeColorTheme();
initializeNumberDisplayFormat();
initializeNumberDisplayRoundDown();
initializeNumberDisplayReformatLikes();
}
function initializeDisableVoteSubmission() {
@@ -233,6 +315,16 @@ function initializeNumberDisplayFormat() {
});
}
function initializeNumberDisplayReformatLikes() {
getBrowser().storage.sync.get(["numberDisplayReformatLikes"], (res) => {
if (res.numberDisplayReformatLikes === undefined) {
getBrowser().storage.sync.set({ numberDisplayReformatLikes: false });
} else {
extConfig.numberDisplayReformatLikes = res.numberDisplayReformatLikes;
}
});
}
export {
isMobile,
isShorts,
@@ -250,5 +342,5 @@ export {
extConfig,
initExtConfig,
storedData,
likesDisabledState,
isLikesDisabled,
};
+5
View File
@@ -29,6 +29,10 @@ function numberFormat(numberState) {
);
}
function localize(localeString) {
return chrome.i18n.getMessage(localeString);
}
function getNumberFormatter(optionSelect) {
let formatterNotation;
let formatterCompactDisplay;
@@ -151,4 +155,5 @@ export {
isVideoLoaded,
cLog,
getColorFromTheme,
localize,
};