Files
return-youtube-dislike/Extensions/combined/src/utils.js
T
Yvon Cui b476c60fe0 cherry pick from previously broken and abandoned branch
colorize buttons and ratio bar

Resolves #218
Using user options (#387)

edit color schemes

logistics for the added options

handles storage, change, initialize
2022-01-18 09:54:17 -05:00

102 lines
2.4 KiB
JavaScript

import {
extConfig,
} from "./state";
function roundDown(num) {
if (num < 1000) return num;
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;
}
function numberFormat(numberState) {
let userLocales;
try {
userLocales = new URL(
Array.from(document.querySelectorAll("head > link[rel='search']"))
?.find((n) => n?.getAttribute("href")?.includes("?locale="))
?.getAttribute("href")
)?.searchParams?.get("locale");
} catch {}
const formatter = Intl.NumberFormat(
document.documentElement.lang || userLocales || navigator.language,
{
notation: "compact",
}
);
return formatter.format(roundDown(numberState));
}
function getBrowser() {
if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined") {
return chrome;
} else if (
typeof browser !== "undefined" &&
typeof browser.runtime !== "undefined"
) {
return browser;
} else {
console.log("browser is not supported");
return false;
}
}
function getVideoId(url) {
const urlObject = new URL(url);
const pathname = urlObject.pathname;
if (pathname.startsWith("/clip")) {
return document.querySelector("meta[itemprop='videoId']").content;
} else {
return urlObject.searchParams.get("v");
}
}
function isVideoLoaded() {
const videoId = getVideoId(window.location.href);
return (
document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null ||
// mobile: no video-id attribute
document.querySelector('#player[loading="false"]:not([hidden])') !== null
);
}
function cLog(message, writer) {
message = `[return youtube dislike]: ${message}`;
if (writer) {
writer(message);
} else {
console.log(message);
}
}
function getColorFromTheme(voteIsLike) {
let colorString;
switch(extConfig.colorTheme) {
case 'accessible':
if (voteIsLike === true) {
colorString = 'dodgerblue';
} else {
colorString = 'gold';
}
break;
case 'neon':
if (voteIsLike === true) {
colorString = 'aqua';
} else {
colorString = 'magenta';
}
break;
case 'classic':
default:
if (voteIsLike === true) {
colorString = 'lime';
} else {
colorString = 'red';
}
}
return colorString;
}
export { numberFormat, getBrowser, getVideoId, isVideoLoaded, cLog }