2023-07-10 10:14:39 +00:00
|
|
|
import {type UsernameColorsData} from "../../storage/exports.js";
|
|
|
|
import {
|
|
|
|
log,
|
|
|
|
querySelectorAll,
|
|
|
|
getColorFromStringHash,
|
|
|
|
isColorBright,
|
|
|
|
} from "../../utilities/exports.js";
|
2023-06-23 10:52:03 +00:00
|
|
|
|
2023-07-09 15:15:10 +00:00
|
|
|
export async function runUsernameColorsFeature(
|
2023-06-23 10:52:03 +00:00
|
|
|
data: UsernameColorsData,
|
|
|
|
anonymizeUsernamesEnabled: boolean,
|
2023-07-09 15:15:10 +00:00
|
|
|
randomizeUsernameColorsEnabled: boolean,
|
2023-06-23 10:52:03 +00:00
|
|
|
) {
|
2023-07-10 10:14:39 +00:00
|
|
|
const count = await usernameColors(
|
|
|
|
data,
|
|
|
|
anonymizeUsernamesEnabled,
|
|
|
|
randomizeUsernameColorsEnabled,
|
|
|
|
);
|
2023-06-23 10:52:03 +00:00
|
|
|
log(`Username Colors: Applied ${count} colors.`);
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:15:10 +00:00
|
|
|
async function usernameColors(
|
2023-06-23 10:52:03 +00:00
|
|
|
data: UsernameColorsData,
|
|
|
|
anonymizeUsernamesEnabled: boolean,
|
2023-07-09 15:15:10 +00:00
|
|
|
randomizeUsernameColorsEnabled: boolean,
|
|
|
|
): Promise<number> {
|
2023-06-23 10:52:03 +00:00
|
|
|
const usernameColors = new Map<string, string>();
|
2023-06-28 16:46:27 +00:00
|
|
|
for (const {
|
2023-07-10 10:14:39 +00:00
|
|
|
value: {color, username: usernames},
|
2023-06-28 16:46:27 +00:00
|
|
|
} of data) {
|
2023-06-23 10:52:03 +00:00
|
|
|
for (const username of usernames.split(",")) {
|
|
|
|
usernameColors.set(username.trim().toLowerCase(), color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let count = 0;
|
|
|
|
const usernameElements = querySelectorAll<HTMLElement>(
|
|
|
|
".link-user:not(.trx-username-colors)",
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const element of usernameElements) {
|
|
|
|
if (element.classList.contains("trx-username-colors")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let target =
|
|
|
|
element.textContent?.replace(/@/g, "").trim().toLowerCase() ??
|
|
|
|
"<unknown>";
|
|
|
|
if (anonymizeUsernamesEnabled) {
|
|
|
|
target = element.dataset.trxUsername?.toLowerCase() ?? target;
|
|
|
|
}
|
|
|
|
|
|
|
|
element.classList.add("trx-username-colors");
|
2023-07-10 10:14:39 +00:00
|
|
|
const color = usernameColors.get(target);
|
|
|
|
if (color !== undefined) {
|
2023-07-09 15:15:10 +00:00
|
|
|
element.style.color = color;
|
|
|
|
} else if (randomizeUsernameColorsEnabled) {
|
2023-07-10 10:14:39 +00:00
|
|
|
element.classList.add("trx-random-username-color");
|
2023-07-09 15:15:10 +00:00
|
|
|
const randomColor = await getColorFromStringHash(target);
|
2023-07-10 10:14:39 +00:00
|
|
|
const fontColor = isColorBright(randomColor) ? "#000" : "#FFF";
|
2023-07-09 15:15:10 +00:00
|
|
|
element.style.setProperty("--background-color", randomColor);
|
2023-07-10 10:14:39 +00:00
|
|
|
// Specifically use "--link-color" here so Tildes's link color doesn't
|
|
|
|
// override ours.
|
|
|
|
element.style.setProperty("--link-color", fontColor);
|
2023-07-09 15:15:10 +00:00
|
|
|
} else {
|
2023-06-23 10:52:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-07-10 10:14:39 +00:00
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|