diff --git a/Extensions/UserScript/Return Youtube Dislike.user.js b/Extensions/UserScript/Return Youtube Dislike.user.js index cef0624..74fa8c6 100644 --- a/Extensions/UserScript/Return Youtube Dislike.user.js +++ b/Extensions/UserScript/Return Youtube Dislike.user.js @@ -29,10 +29,10 @@ var previousState = 3; //1=LIKED, 2=DISLIKED, 3=NEUTRAL var likesvalue = 0; var dislikesvalue = 0; -var isMobile = (location.hostname == "m.youtube.com"); +var isMobile = location.hostname == "m.youtube.com"; var mobileDislikes = 0; -function cLog(text, subtext = '') { - subtext = subtext.trim() === '' ? '' : `(${subtext})`; +function cLog(text, subtext = "") { + subtext = subtext.trim() === "" ? "" : `(${subtext})`; console.log(`[Return YouTube Dislikes] ${text} ${subtext}`); } @@ -59,14 +59,20 @@ function getDislikeButton() { function isVideoLiked() { if (isMobile) { - return getLikeButton().querySelector("button").getAttribute("aria-label") == "true"; + return ( + getLikeButton().querySelector("button").getAttribute("aria-label") == + "true" + ); } return getLikeButton().classList.contains("style-default-active"); } function isVideoDisliked() { if (isMobile) { - return getDislikeButton().querySelector("button").getAttribute("aria-label") == "true"; + return ( + getDislikeButton().querySelector("button").getAttribute("aria-label") == + "true" + ); } return getDislikeButton().classList.contains("style-default-active"); } @@ -108,7 +114,8 @@ function getState() { function setLikes(likesCount) { if (isMobile) { - getButtons().children[0].querySelector(".button-renderer-text").innerText = likesCount; + getButtons().children[0].querySelector(".button-renderer-text").innerText = + likesCount; return; } getButtons().children[0].querySelector("#text").innerText = likesCount; @@ -122,14 +129,14 @@ function setDislikes(dislikesCount) { getButtons().children[1].querySelector("#text").innerText = dislikesCount; } -(typeof GM_addStyle != 'undefined' - ? GM_addStyle - : styles => { - var styleNode = document.createElement("style") - styleNode.type = "text/css"; - styleNode.innerText = styles; - document.head.appendChild(styleNode); -})(` +(typeof GM_addStyle != "undefined" + ? GM_addStyle + : (styles) => { + var styleNode = document.createElement("style"); + styleNode.type = "text/css"; + styleNode.innerText = styles; + document.head.appendChild(styleNode); + })(` #return-youtube-dislike-bar-container { background: var(--yt-spec-icon-disabled); border-radius: 2px; @@ -215,7 +222,8 @@ function setState() { method: "GET", url: `https://www.youtube.com/watch?v=${getVideoId()}`, headers: { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.3674" + "User-Agent": + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.3674", }, onload: (response) => { let result = getDislikesFromYoutubeResponse(response.responseText); @@ -231,11 +239,10 @@ function setState() { statsSet = true; } } - } + }, }); - } - else { - fetch(`https://www.youtube.com/watch?v=${getVideoId()}`).then((response) => { + } else { + fetch(`https://youtube.com/watch?v=${getVideoId()}`).then((response) => { response.text().then((text) => { let result = getDislikesFromYoutubeResponse(text); if (result) { @@ -320,7 +327,7 @@ function setInitialState() { function getVideoId() { const urlObject = new URL(window.location.href); const pathname = urlObject.pathname; - if (pathname.startsWith('/clips')) { + if (pathname.startsWith("/clips")) { return document.querySelector("meta[itemprop='videoId']").content; } else { return urlObject.searchParams.get("v"); @@ -347,13 +354,20 @@ function roundDown(num) { } function numberFormat(numberState) { - const userLocales = document.documentElement.lang; + const userLocales = new URL( + Array.from(document.querySelectorAll("head > link[rel='search']")) + ?.find((n) => n?.getAttribute("href")?.includes("?locale=")) + ?.getAttribute("href") + )?.searchParams?.get("locale"); - const formatter = Intl.NumberFormat(userLocales, { - notation: "compact", - minimumFractionDigits: 1, - maximumFractionDigits: 1, - }); + const formatter = Intl.NumberFormat( + document.documentElement.lang || userLocales, + { + notation: "compact", + minimumFractionDigits: 1, + maximumFractionDigits: 1, + } + ); return formatter.format(roundDown(numberState)).replace(/\.0|,0/, ""); } @@ -391,7 +405,7 @@ function getDislikesFromYoutubeResponse(htmlResponse) { function setEventListeners(evt) { function checkForJS_Finish(check) { - console.log() + console.log(); if (getButtons()?.offsetParent && isVideoLoaded()) { clearInterval(jsInitChecktimer); const buttons = getButtons(); @@ -406,7 +420,10 @@ function setEventListeners(evt) { } } - if (window.location.href.indexOf("watch?") >= 0 || (isMobile && evt?.indexOf("watch?") >= 0)) { + if ( + window.location.href.indexOf("watch?") >= 0 || + (isMobile && evt?.indexOf("watch?") >= 0) + ) { cLog("Setting up..."); var jsInitChecktimer = setInterval(checkForJS_Finish, 111); } @@ -423,8 +440,9 @@ if (isMobile) { window.returnDislikeButtonlistenersSet = false; setEventListeners(args[2]); return originalPush.apply(history, args); - } + }; setInterval(() => { - getDislikeButton().querySelector(".button-renderer-text").innerText = mobileDislikes; + getDislikeButton().querySelector(".button-renderer-text").innerText = + mobileDislikes; }, 1000); } diff --git a/Extensions/chrome/return-youtube-dislike.script.js b/Extensions/chrome/return-youtube-dislike.script.js index 1d9d8b3..39f93a4 100644 --- a/Extensions/chrome/return-youtube-dislike.script.js +++ b/Extensions/chrome/return-youtube-dislike.script.js @@ -6,7 +6,7 @@ const NEUTRAL_STATE = "NEUTRAL_STATE"; let storedData = { likes: 0, dislikes: 0, - previousState: NEUTRAL_STATE + previousState: NEUTRAL_STATE, }; function cLog(message, writer) { @@ -19,9 +19,8 @@ const NEUTRAL_STATE = "NEUTRAL_STATE"; } function getButtons() { - //--- If Menu Element Is Displayed: ---// - if (document.getElementById('menu-container')?.offsetParent === null) { + if (document.getElementById("menu-container")?.offsetParent === null) { return document.querySelector( "ytd-menu-renderer.ytd-watch-metadata > div" ); @@ -83,8 +82,6 @@ const NEUTRAL_STATE = "NEUTRAL_STATE"; getButtons().children[1].querySelector("#text").innerText = dislikesCount; } - - function setState() { let statsSet = false; chrome.runtime.sendMessage( @@ -184,7 +181,7 @@ const NEUTRAL_STATE = "NEUTRAL_STATE"; function getVideoId(url) { const urlObject = new URL(url); const pathname = urlObject.pathname; - if (pathname.startsWith('/clips')) { + if (pathname.startsWith("/clips")) { return document.querySelector("meta[itemprop='videoId']").content; } else { return urlObject.searchParams.get("v"); @@ -203,14 +200,18 @@ const NEUTRAL_STATE = "NEUTRAL_STATE"; const int = Math.floor(Math.log10(num) - 2); const decimal = int + (int % 3 ? 1 : 0); const value = Math.floor(num / 10 ** decimal); - return value * (10 ** decimal); + return value * 10 ** decimal; } function numberFormat(numberState) { - const userLocales = navigator.language; + const userLocales = new URL( + Array.from(document.querySelectorAll("head > link[rel='search']")) + ?.find((n) => n?.getAttribute("href")?.includes("?locale=")) + ?.getAttribute("href") + )?.searchParams?.get("locale"); - const formatter = Intl.NumberFormat(userLocales, { - notation: 'compact' + const formatter = Intl.NumberFormat(document.documentElement.lang || userLocales || navigator.language, { + notation: "compact", }); return formatter.format(roundDown(numberState)); @@ -313,7 +314,5 @@ const NEUTRAL_STATE = "NEUTRAL_STATE"; setEventListeners(); }); - - setTimeout(() => sendVideoIds(), 2500); })(document.currentScript.getAttribute("extension-id")); diff --git a/Extensions/firefox/return-youtube-dislike.script.js b/Extensions/firefox/return-youtube-dislike.script.js index ca89025..78da455 100644 --- a/Extensions/firefox/return-youtube-dislike.script.js +++ b/Extensions/firefox/return-youtube-dislike.script.js @@ -6,7 +6,7 @@ if (!storedData) { var storedData = { likes: 0, dislikes: 0, - previousState: NEUTRAL_STATE + previousState: NEUTRAL_STATE, }; } @@ -186,7 +186,7 @@ function setInitialState() { function getVideoId(url) { const urlObject = new URL(url); const pathname = urlObject.pathname; - if (pathname.startsWith('/clips')) { + if (pathname.startsWith("/clips")) { return document.querySelector("meta[itemprop='videoId']").content; } else { return urlObject.searchParams.get("v"); @@ -211,10 +211,13 @@ function roundDown(num) { } function numberFormat(numberState) { - const userLocales = navigator.language; - - const formatter = Intl.NumberFormat(userLocales, { - notation: "compact" + const userLocales = new URL( + Array.from(document.querySelectorAll("head > link[rel='search']")) + ?.find((n) => n?.getAttribute("href")?.includes("?locale=")) + ?.getAttribute("href") + )?.searchParams?.get("locale"); + const formatter = Intl.NumberFormat(document.documentElement.lang || userLocales || navigator.language, { + notation: "compact", }); return formatter.format(roundDown(numberState));