checks for youtube locale and uses navigator.language as fallback

This commit is contained in:
DARKDRAGON532 committed 2021-12-07 17:12:59 +05:30
1 parent 6da6e73698
commit 4ba6ef9f9e
3 files changed
+70 -50

No files matched your search

@@ -26,10 +26,10 @@ 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";
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}`);
} }
@@ -56,14 +56,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");
} }
@@ -94,7 +100,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;
@@ -108,14 +115,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;
@@ -201,7 +208,8 @@ function setState() {
method: "GET", method: "GET",
url: `https://youtube.com/watch?v=${getVideoId()}`, url: `https://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);
@@ -215,10 +223,9 @@ function setState() {
statsSet = true; statsSet = true;
} }
} }
} },
}); });
} } else {
else {
fetch(`https://youtube.com/watch?v=${getVideoId()}`).then((response) => { fetch(`https://youtube.com/watch?v=${getVideoId()}`).then((response) => {
response.text().then((text) => { response.text().then((text) => {
let result = getDislikesFromYoutubeResponse(text); let result = getDislikesFromYoutubeResponse(text);
@@ -267,7 +274,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");
@@ -294,13 +301,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", userLocales || document.documentElement.lang,
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/, "");
} }
@@ -338,7 +352,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();
@@ -353,7 +367,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);
} }
@@ -370,8 +387,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);
} }
@@ -5,7 +5,7 @@ const NEUTRAL_STATE = "NEUTRAL_STATE";
(function (extensionId) { (function (extensionId) {
let storedData = { let storedData = {
dislikes: 0, dislikes: 0,
previousState: NEUTRAL_STATE previousState: NEUTRAL_STATE,
}; };
function cLog(message, writer) { function cLog(message, writer) {
@@ -18,9 +18,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"
); );
@@ -74,8 +73,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(
@@ -125,7 +122,7 @@ const NEUTRAL_STATE = "NEUTRAL_STATE";
} }
function likeClicked() { function likeClicked() {
console.log(storedData.previousState) console.log(storedData.previousState);
if (storedData.previousState == DISLIKED_STATE) { if (storedData.previousState == DISLIKED_STATE) {
storedData.dislikes--; storedData.dislikes--;
setDislikes(numberFormat(storedData.dislikes)); setDislikes(numberFormat(storedData.dislikes));
@@ -159,7 +156,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");
@@ -178,14 +175,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(userLocales || navigator.language, {
notation: 'compact' notation: "compact",
}); });
return formatter.format(roundDown(numberState)); return formatter.format(roundDown(numberState));
@@ -288,7 +289,5 @@ const NEUTRAL_STATE = "NEUTRAL_STATE";
setEventListeners(); setEventListeners();
}); });
setTimeout(() => sendVideoIds(), 2500); setTimeout(() => sendVideoIds(), 2500);
})(document.currentScript.getAttribute("extension-id")); })(document.currentScript.getAttribute("extension-id"));
@@ -5,7 +5,7 @@ const NEUTRAL_STATE = "NEUTRAL_STATE";
if (!storedData) { if (!storedData) {
var storedData = { var storedData = {
dislikes: 0, dislikes: 0,
previousState: NEUTRAL_STATE previousState: NEUTRAL_STATE,
}; };
} }
@@ -121,10 +121,10 @@ function setState() {
} }
function likeClicked() { function likeClicked() {
if (storedData.previousState === 'disliked') { if (storedData.previousState === "disliked") {
storedData.dislikes--; storedData.dislikes--;
setDislikes(numberFormat(storedData.dislikes)); setDislikes(numberFormat(storedData.dislikes));
storedData.previousState = 'liked'; storedData.previousState = "liked";
} }
} }
@@ -151,7 +151,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");
@@ -174,10 +174,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(userLocales || navigator.language, {
notation: "compact",
}); });
return formatter.format(roundDown(numberState)); return formatter.format(roundDown(numberState));