Merge pull request #239 from Anarios/patch-1

checks for youtube locale and uses navigator.language as fallback
This commit is contained in:
DARKDRAGON532 authored and GitHub committed 2021-12-09 15:55:56 +05:30
commit 1959c8ba23
3 files changed
+68 -48

No files matched your search

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