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 NEUTRAL_STATE = "NEUTRAL_STATE";
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}`);
}
@@ -56,14 +56,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");
}
@@ -94,7 +100,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;
@@ -108,14 +115,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;
@@ -201,7 +208,8 @@ function setState() {
method: "GET",
url: `https://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);
@@ -215,10 +223,9 @@ function setState() {
statsSet = true;
}
}
}
},
});
}
else {
} else {
fetch(`https://youtube.com/watch?v=${getVideoId()}`).then((response) => {
response.text().then((text) => {
let result = getDislikesFromYoutubeResponse(text);
@@ -267,7 +274,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");
@@ -294,13 +301,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(
userLocales || document.documentElement.lang,
{
notation: "compact",
minimumFractionDigits: 1,
maximumFractionDigits: 1,
}
);
return formatter.format(roundDown(numberState)).replace(/\.0|,0/, "");
}
@@ -338,7 +352,7 @@ function getDislikesFromYoutubeResponse(htmlResponse) {
function setEventListeners(evt) {
function checkForJS_Finish(check) {
console.log()
console.log();
if (getButtons()?.offsetParent && isVideoLoaded()) {
clearInterval(jsInitChecktimer);
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...");
var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
}
@@ -370,8 +387,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);
}