From 6e62ea0036e4486e26d4872f3be263bb409697a4 Mon Sep 17 00:00:00 2001 From: Bauke Date: Sun, 13 Aug 2023 11:40:40 +0200 Subject: [PATCH] Add the different replacement types for Anonymize Usernames. --- .../features/anonymize-usernames.ts | 44 ++++++++++++++++--- source/content-scripts/setup.tsx | 7 ++- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/source/content-scripts/features/anonymize-usernames.ts b/source/content-scripts/features/anonymize-usernames.ts index 7952653..e5761b2 100644 --- a/source/content-scripts/features/anonymize-usernames.ts +++ b/source/content-scripts/features/anonymize-usernames.ts @@ -1,15 +1,23 @@ -import {log, querySelectorAll} from "../../utilities/exports.js"; +import {hashSha256, log, querySelectorAll} from "../../utilities/exports.js"; +import { + type AnonymizeUsernamesData, + ReplacementType, +} from "../../storage/exports.js"; -export function runAnonymizeUsernamesFeature() { - const count = anonymizeUsernames(); +export async function runAnonymizeUsernamesFeature( + data: AnonymizeUsernamesData, +) { + const count = await anonymizeUsernames(data); log(`Anonymize Usernames: Initialized for ${count} user links.`); } -function anonymizeUsernames(): number { +async function anonymizeUsernames( + data: AnonymizeUsernamesData, +): Promise { const usernameElements = querySelectorAll( ".link-user:not(.trx-anonymized)", ); - const replacements = generateReplacements(usernameElements); + const replacements = await generateReplacements(usernameElements, data); for (const element of usernameElements) { let username = usernameFromElement(element); @@ -28,14 +36,36 @@ function anonymizeUsernames(): number { return usernameElements.length; } -function generateReplacements(elements: HTMLElement[]): Record { +async function generateReplacements( + elements: HTMLElement[], + data: AnonymizeUsernamesData, +): Promise> { 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 + 1}`; + switch (data.replacementType) { + case ReplacementType.Hashed: { + const hash = await hashSha256(username); + replacements[username] = hash.slice(0, 10).toUpperCase(); + break; + } + + case ReplacementType.Numerical: { + replacements[username] = `Anonymous ${index + 1}`; + break; + } + + default: { + throw new Error( + `Unknown ReplacementType in AnonymizeUsernamesData: ${JSON.stringify( + data, + )}`, + ); + } + } } return replacements; diff --git a/source/content-scripts/setup.tsx b/source/content-scripts/setup.tsx index 51a1370..b0c85d0 100644 --- a/source/content-scripts/setup.tsx +++ b/source/content-scripts/setup.tsx @@ -82,8 +82,11 @@ async function initialize() { } if (anonymizeUsernamesEnabled) { - observerFeatures.push(() => { - runAnonymizeUsernamesFeature(); + const anonymizeUsernamesData = await fromStorage( + Feature.AnonymizeUsernames, + ); + observerFeatures.push(async () => { + await runAnonymizeUsernamesFeature(anonymizeUsernamesData.value); }); }