mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-01 18:31:24 +01:00
formatted with prettier
This commit is contained in:
1 parent
8ce61c8742
commit
2a4b1b95d1
1 file changed
+41
-40
@@ -14,44 +14,48 @@
|
|||||||
// @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 = '') {
|
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}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function doXHR(opts) {
|
function doXHR(opts) {
|
||||||
if (typeof GM_xmlhttpRequest === 'function') {
|
if (typeof GM_xmlhttpRequest === "function") {
|
||||||
return GM_xmlhttpRequest(opts);
|
return GM_xmlhttpRequest(opts);
|
||||||
}
|
}
|
||||||
if (typeof GM !== 'undefined') /*This will prevent from throwing "Uncaught ReferenceError: GM is not defined"*/ {
|
if (typeof GM !== "undefined") {
|
||||||
if (typeof GM.xmlHttpRequest === 'function') {
|
/*This will prevent from throwing "Uncaught ReferenceError: GM is not defined"*/ if (
|
||||||
|
typeof GM.xmlHttpRequest === "function"
|
||||||
|
) {
|
||||||
return GM.xmlHttpRequest(opts);
|
return GM.xmlHttpRequest(opts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.warn('Unable to detect UserScript plugin, falling back to native XHR.');
|
console.warn(
|
||||||
|
"Unable to detect UserScript plugin, falling back to native XHR."
|
||||||
|
);
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
xhr.open(opts.method, opts.url, true);
|
xhr.open(opts.method, opts.url, true);
|
||||||
if (opts.responseType === 'text') {
|
if (opts.responseType === "text") {
|
||||||
xhr.onload = () => opts.onload({
|
xhr.onload = () =>
|
||||||
response: xhr.responseText,
|
opts.onload({
|
||||||
});
|
response: xhr.responseText,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
xhr.onload = () => opts.onload({
|
xhr.onload = () =>
|
||||||
response: JSON.parse(xhr.responseText),
|
opts.onload({
|
||||||
});
|
response: JSON.parse(xhr.responseText),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
xhr.onerror = err => console.error('XHR Failed', err);
|
xhr.onerror = (err) => console.error("XHR Failed", err);
|
||||||
xhr.send();
|
xhr.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getButtons() {
|
function getButtons() {
|
||||||
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"
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
return document
|
return document
|
||||||
.getElementById("menu-container")
|
.getElementById("menu-container")
|
||||||
@@ -102,9 +106,7 @@ function setDislikes(dislikesCount) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createRateBar(likes, dislikes) {
|
function createRateBar(likes, dislikes) {
|
||||||
var rateBar = document.getElementById(
|
var rateBar = document.getElementById("return-youtube-dislike-bar-container");
|
||||||
"return-youtube-dislike-bar-container"
|
|
||||||
);
|
|
||||||
|
|
||||||
const widthPx =
|
const widthPx =
|
||||||
getButtons().children[0].clientWidth +
|
getButtons().children[0].clientWidth +
|
||||||
@@ -150,11 +152,11 @@ function createRateBar(likes, dislikes) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setState() {
|
function setState() {
|
||||||
cLog('Fetching votes...');
|
cLog("Fetching votes...");
|
||||||
let statsSet = false;
|
let statsSet = false;
|
||||||
doXHR({
|
doXHR({
|
||||||
method: 'GET',
|
method: "GET",
|
||||||
responseType: 'text',
|
responseType: "text",
|
||||||
url: `https://www.youtube.com/watch?v=${getVideoId()}`,
|
url: `https://www.youtube.com/watch?v=${getVideoId()}`,
|
||||||
onload: function (xhr) {
|
onload: function (xhr) {
|
||||||
if (xhr) {
|
if (xhr) {
|
||||||
@@ -162,17 +164,16 @@ function setState() {
|
|||||||
if (result) {
|
if (result) {
|
||||||
cLog("response from youtube:");
|
cLog("response from youtube:");
|
||||||
cLog(JSON.stringify(result));
|
cLog(JSON.stringify(result));
|
||||||
if (result.likes || result.dislikes) {
|
if (result.likes || result.dislikes) {
|
||||||
const formattedDislike = numberFormat(result.dislikes);
|
const formattedDislike = numberFormat(result.dislikes);
|
||||||
setDislikes(formattedDislike);
|
setDislikes(formattedDislike);
|
||||||
createRateBar(result.likes, result.dislikes);
|
createRateBar(result.likes, result.dislikes);
|
||||||
statsSet = true;
|
statsSet = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
doXHR({
|
doXHR({
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@@ -192,12 +193,12 @@ function setState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function likeClicked() {
|
function likeClicked() {
|
||||||
cLog('Like clicked', getState());
|
cLog("Like clicked", getState());
|
||||||
setState();
|
setState();
|
||||||
}
|
}
|
||||||
|
|
||||||
function dislikeClicked() {
|
function dislikeClicked() {
|
||||||
cLog('Dislike clicked', getState());
|
cLog("Dislike clicked", getState());
|
||||||
setState();
|
setState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,19 +226,19 @@ function roundDown(num) {
|
|||||||
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 = navigator.language;
|
||||||
|
|
||||||
const formatter = Intl.NumberFormat(userLocales, {
|
const formatter = Intl.NumberFormat(userLocales, {
|
||||||
notation: 'compact',
|
notation: "compact",
|
||||||
minimumFractionDigits: 1,
|
minimumFractionDigits: 1,
|
||||||
maximumFractionDigits: 1
|
maximumFractionDigits: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
return formatter.format(roundDown(numberState)).replace(/\.0|,0/, '');
|
return formatter.format(roundDown(numberState)).replace(/\.0|,0/, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDislikesFromYoutubeResponse(htmlResponse) {
|
function getDislikesFromYoutubeResponse(htmlResponse) {
|
||||||
@@ -278,7 +279,7 @@ function setEventListeners(evt) {
|
|||||||
const buttons = getButtons();
|
const buttons = getButtons();
|
||||||
|
|
||||||
if (!window.returnDislikeButtonlistenersSet) {
|
if (!window.returnDislikeButtonlistenersSet) {
|
||||||
cLog('Registering button listeners...');
|
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;
|
||||||
@@ -288,7 +289,7 @@ function setEventListeners(evt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (window.location.href.indexOf("watch?") >= 0) {
|
if (window.location.href.indexOf("watch?") >= 0) {
|
||||||
cLog('Setting up...');
|
cLog("Setting up...");
|
||||||
var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
|
var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user