Improve logging

Adds a log helper to easily prefix all output with the plugin name,
improving the console when multiple youtube-related plugins are used.

Also adds a couple extra log lines to make it easier to see when it's
loaded or sending the request in case things don't go quite right.
This commit is contained in:
Dave Shoreman committed 2021-11-21 12:12:22 +00:00
1 parent 1027f43503
commit 007a53f546
1 file changed
+22 -7
@@ -15,6 +15,11 @@
// @updateURL https://github.com/Anarios/return-youtube-dislike/raw/main/Extensions/UserScript/Return%20Youtube%20Dislike.user.js // @updateURL https://github.com/Anarios/return-youtube-dislike/raw/main/Extensions/UserScript/Return%20Youtube%20Dislike.user.js
// @grant GM_xmlhttpRequest // @grant GM_xmlhttpRequest
// ==/UserScript== // ==/UserScript==
function cLog(text, subtext = '') {
subtext = subtext.trim() === '' ? '' : `(${subtext})`;
console.log(`[Return Youtube Dislikes] ${text} ${subtext}`);
}
function getButtons() { function getButtons() {
return document return document
.getElementById("menu-container") .getElementById("menu-container")
@@ -52,6 +57,7 @@ function getState() {
if (isVideoDisliked()) { if (isVideoDisliked()) {
return "disliked"; return "disliked";
} }
return "neutral"; return "neutral";
} }
@@ -64,29 +70,32 @@ function setDislikes(dislikesCount) {
} }
function setState() { function setState() {
cLog('Fetching votes...');
GM_xmlhttpRequest({ GM_xmlhttpRequest({
method: "GET", method: "GET",
responseType: "json", responseType: "json",
url: url:
"https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" + "https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" +
getVideoId(), getVideoId(),
onload: function (response) { onload: function (xhr) {
if (response != undefined) { if (xhr != undefined) {
const formattedDislike = numberFormat(response.response.dislikes); const { dislikes } = xhr.response;
console.log(response);
setDislikes(formattedDislike); cLog(`Received count: ${dislikes}`);
setDislikes(numberFormat(dislikes));
} }
}, },
}); });
} }
function likeClicked() { function likeClicked() {
console.log("like" + getState()); cLog('Like clicked', getState());
setState(); setState();
} }
function dislikeClicked() { function dislikeClicked() {
console.log("dislike" + getState()); cLog('Dislike clicked', getState());
setState(); setState();
} }
@@ -97,11 +106,13 @@ function setInitalState() {
function getVideoId() { function getVideoId() {
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const videoId = urlParams.get("v"); const videoId = urlParams.get("v");
return videoId; return videoId;
} }
function isVideoLoaded() { function isVideoLoaded() {
const videoId = getVideoId(); const videoId = getVideoId();
return ( return (
document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
); );
@@ -110,6 +121,7 @@ function isVideoLoaded() {
function numberFormat(numberState) { function numberFormat(numberState) {
const userLocales = navigator.language; const userLocales = navigator.language;
const formatter = Intl.NumberFormat(userLocales, { notation: "compact" }); const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
return formatter.format(numberState); return formatter.format(numberState);
} }
@@ -118,7 +130,9 @@ function setEventListeners(evt) {
if (getButtons()?.offsetParent && isVideoLoaded()) { if (getButtons()?.offsetParent && isVideoLoaded()) {
clearInterval(jsInitChecktimer); clearInterval(jsInitChecktimer);
const buttons = getButtons(); const buttons = getButtons();
if (!window.returnDislikeButtonlistenersSet) { if (!window.returnDislikeButtonlistenersSet) {
cLog('Registering button listeners...');
buttons.children[0].addEventListener("click", likeClicked); buttons.children[0].addEventListener("click", likeClicked);
buttons.children[1].addEventListener("click", dislikeClicked); buttons.children[1].addEventListener("click", dislikeClicked);
window.returnDislikeButtonlistenersSet = true; window.returnDislikeButtonlistenersSet = true;
@@ -128,6 +142,7 @@ function setEventListeners(evt) {
} }
if (window.location.href.indexOf("watch?") >= 0) { if (window.location.href.indexOf("watch?") >= 0) {
cLog('Setting up...');
var jsInitChecktimer = setInterval(checkForJS_Finish, 111); var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
} }
} }