import {log, querySelectorAll} from '../utilities/exports.js'; export function runAnonymizeUsernamesFeature() { const count = anonymizeUsernames(); log(`Anonymize Usernames: Initialized for ${count} user links.`); } function anonymizeUsernames(): number { const usernameElements = querySelectorAll( '.link-user:not(.trx-anonymized)', ); const replacements = generateReplacements(usernameElements); for (const element of usernameElements) { let username = usernameFromElement(element); const isMention = username.startsWith('@'); if (isMention) { username = username.slice(1); } const replacement = replacements[username]; element.textContent = isMention ? `@${replacement}` : `${replacement}`; element.classList.add('trx-anonymized'); element.dataset.trxUsername = username; } return usernameElements.length; } function generateReplacements(elements: HTMLElement[]): Record { const usernames = new Set( elements.map((element) => usernameFromElement(element).replace(/@/g, '')), ); const replacements: Record = {}; for (const [index, username] of Array.from(usernames).entries()) { replacements[username] = `Anonymous ${index}`; } return replacements; } function usernameFromElement(element: HTMLElement): string { return (element.textContent ?? '').trim(); }