fix: anonymization within message text

Hopefully for reals this time.
This commit is contained in:
11b 2022-12-21 14:19:27 -03:00
parent 21ebd5834e
commit ecf2e65e76
1 changed files with 32 additions and 6 deletions

View File

@ -3,7 +3,7 @@
// @namespace Violentmonkey Scripts
// @match https://beta.character.ai/*
// @grant none
// @version 1.1
// @version 1.2
// @author 0x000011b
// @description Allows downloading saved chat messages from CharacterAI.
// @downloadURL https://git.fuwafuwa.moe/waifu-collective/toolbox/raw/branch/master/extras/characterai-dumper/characterai-dumper.user.js
@ -11,9 +11,9 @@
// ==/UserScript==
const log = (firstArg, ...remainingArgs) =>
console.log(`[CharacterAI Dumper v1.1] ${firstArg}`, ...remainingArgs);
console.log(`[CharacterAI Dumper v1.2] ${firstArg}`, ...remainingArgs);
log.error = (firstArg, ...remainingArgs) =>
console.error(`[CharacterAI Dumper v1.1] ${firstArg}`, ...remainingArgs);
console.error(`[CharacterAI Dumper v1.2] ${firstArg}`, ...remainingArgs);
const CHARACTER_INFO_URL = "https://beta.character.ai/chat/character/info/";
const CHARACTER_HISTORIES_URL =
@ -95,15 +95,41 @@ const anonymizeHistories = (histories) => {
// Now, since this is a bot message, there's a chance that the bot
// uttered the user's name, so let's replace that inside the message
// text.
for (const nameToReplace in namesToReplace) {
namesToReplace.forEach((nameToReplace) => {
if (!nameToReplace) {
return;
}
const replacementRegex = new RegExp(
"\\b" + escapeStringForRegExp(nameToReplace) + "\\b",
"g"
);
msg.text.replace(replacementRegex, "[NAME_IN_MESSAGE_REDACTED]");
}
msg.text = msg.text.replace(
replacementRegex,
"[NAME_IN_MESSAGE_REDACTED]"
);
});
}
}
// And just being extra paranoid: by the time we've gone through both user
// _and_ bot messages, we might've seen more names to redact, so let's go
// back to the first message and attempt to redact it again just in case we
// have new names.
namesToReplace.forEach((nameToReplace) => {
if (!nameToReplace) {
return;
}
const replacementRegex = new RegExp(
"\\b" + escapeStringForRegExp(nameToReplace) + "\\b",
"g"
);
history.msgs[0].text = history.msgs[0].text.replace(
replacementRegex,
"[NAME_IN_MESSAGE_REDACTED]"
);
});
}
// This was modified in-place, but we return it here for simplicity at the