Add the different replacement types for Anonymize Usernames.
This commit is contained in:
parent
be5ccaf0c1
commit
6e62ea0036
|
@ -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() {
|
export async function runAnonymizeUsernamesFeature(
|
||||||
const count = anonymizeUsernames();
|
data: AnonymizeUsernamesData,
|
||||||
|
) {
|
||||||
|
const count = await anonymizeUsernames(data);
|
||||||
log(`Anonymize Usernames: Initialized for ${count} user links.`);
|
log(`Anonymize Usernames: Initialized for ${count} user links.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function anonymizeUsernames(): number {
|
async function anonymizeUsernames(
|
||||||
|
data: AnonymizeUsernamesData,
|
||||||
|
): Promise<number> {
|
||||||
const usernameElements = querySelectorAll<HTMLElement>(
|
const usernameElements = querySelectorAll<HTMLElement>(
|
||||||
".link-user:not(.trx-anonymized)",
|
".link-user:not(.trx-anonymized)",
|
||||||
);
|
);
|
||||||
const replacements = generateReplacements(usernameElements);
|
const replacements = await generateReplacements(usernameElements, data);
|
||||||
|
|
||||||
for (const element of usernameElements) {
|
for (const element of usernameElements) {
|
||||||
let username = usernameFromElement(element);
|
let username = usernameFromElement(element);
|
||||||
|
@ -28,14 +36,36 @@ function anonymizeUsernames(): number {
|
||||||
return usernameElements.length;
|
return usernameElements.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateReplacements(elements: HTMLElement[]): Record<string, string> {
|
async function generateReplacements(
|
||||||
|
elements: HTMLElement[],
|
||||||
|
data: AnonymizeUsernamesData,
|
||||||
|
): Promise<Record<string, string>> {
|
||||||
const usernames = new Set(
|
const usernames = new Set(
|
||||||
elements.map((element) => usernameFromElement(element).replace(/@/g, "")),
|
elements.map((element) => usernameFromElement(element).replace(/@/g, "")),
|
||||||
);
|
);
|
||||||
|
|
||||||
const replacements: Record<string, string> = {};
|
const replacements: Record<string, string> = {};
|
||||||
for (const [index, username] of Array.from(usernames).entries()) {
|
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;
|
return replacements;
|
||||||
|
|
|
@ -82,8 +82,11 @@ async function initialize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (anonymizeUsernamesEnabled) {
|
if (anonymizeUsernamesEnabled) {
|
||||||
observerFeatures.push(() => {
|
const anonymizeUsernamesData = await fromStorage(
|
||||||
runAnonymizeUsernamesFeature();
|
Feature.AnonymizeUsernames,
|
||||||
|
);
|
||||||
|
observerFeatures.push(async () => {
|
||||||
|
await runAnonymizeUsernamesFeature(anonymizeUsernamesData.value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue