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() {
|
||||
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<number> {
|
||||
const usernameElements = querySelectorAll<HTMLElement>(
|
||||
".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<string, string> {
|
||||
async function generateReplacements(
|
||||
elements: HTMLElement[],
|
||||
data: AnonymizeUsernamesData,
|
||||
): Promise<Record<string, string>> {
|
||||
const usernames = new Set(
|
||||
elements.map((element) => usernameFromElement(element).replace(/@/g, "")),
|
||||
);
|
||||
|
||||
const replacements: Record<string, string> = {};
|
||||
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;
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue