2023-06-23 10:52:03 +00:00
|
|
|
import {log, querySelectorAll} from "../../utilities/exports.js";
|
2022-02-23 17:17:22 +00:00
|
|
|
|
|
|
|
export function runAnonymizeUsernamesFeature() {
|
|
|
|
const count = anonymizeUsernames();
|
|
|
|
log(`Anonymize Usernames: Initialized for ${count} user links.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
function anonymizeUsernames(): number {
|
|
|
|
const usernameElements = querySelectorAll<HTMLElement>(
|
2023-06-23 10:52:03 +00:00
|
|
|
".link-user:not(.trx-anonymized)",
|
2022-02-23 17:17:22 +00:00
|
|
|
);
|
|
|
|
const replacements = generateReplacements(usernameElements);
|
|
|
|
|
|
|
|
for (const element of usernameElements) {
|
|
|
|
let username = usernameFromElement(element);
|
2023-06-23 10:52:03 +00:00
|
|
|
const isMention = username.startsWith("@");
|
2022-02-23 17:17:22 +00:00
|
|
|
if (isMention) {
|
|
|
|
username = username.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const replacement = replacements[username];
|
|
|
|
element.textContent = isMention ? `@${replacement}` : `${replacement}`;
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
element.classList.add("trx-anonymized");
|
2022-02-23 17:17:22 +00:00
|
|
|
element.dataset.trxUsername = username;
|
|
|
|
}
|
|
|
|
|
|
|
|
return usernameElements.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateReplacements(elements: HTMLElement[]): Record<string, string> {
|
|
|
|
const usernames = new Set(
|
2023-06-23 10:52:03 +00:00
|
|
|
elements.map((element) => usernameFromElement(element).replace(/@/g, "")),
|
2022-02-23 17:17:22 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const replacements: Record<string, string> = {};
|
|
|
|
for (const [index, username] of Array.from(usernames).entries()) {
|
2023-07-27 10:49:34 +00:00
|
|
|
replacements[username] = `Anonymous ${index + 1}`;
|
2022-02-23 17:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return replacements;
|
|
|
|
}
|
|
|
|
|
|
|
|
function usernameFromElement(element: HTMLElement): string {
|
2023-07-18 11:16:20 +00:00
|
|
|
return (element.textContent ?? "<unknown>").trim().toLowerCase();
|
2022-02-23 17:17:22 +00:00
|
|
|
}
|