1
Fork 0
tildes-reextended/source/content-scripts/features/username-colors.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-06-23 10:52:03 +00:00
import {log, querySelectorAll} from "../../utilities/exports.js";
2023-06-27 11:51:04 +00:00
import {type UsernameColorsData} from "../../storage/exports.js";
2023-06-23 10:52:03 +00:00
export function runUsernameColorsFeature(
data: UsernameColorsData,
anonymizeUsernamesEnabled: boolean,
) {
const count = usernameColors(data, anonymizeUsernamesEnabled);
log(`Username Colors: Applied ${count} colors.`);
}
function usernameColors(
data: UsernameColorsData,
anonymizeUsernamesEnabled: boolean,
): number {
const usernameColors = new Map<string, string>();
for (const {color, username: usernames} of data) {
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");
const color = usernameColors.get(target);
if (color === undefined) {
continue;
}
element.style.color = color;
count += 1;
}
return count;
}