From a8dfd396ccc38393eddd091c13f85bbec8b2ada4 Mon Sep 17 00:00:00 2001 From: 0x000011b <0x000011b@waifu.club> Date: Wed, 21 Dec 2022 13:41:19 -0300 Subject: [PATCH] fix: more aggressive anonymization within message text --- extras/characterai-dumper/README.md | 2 +- ...per-user.js => characterai-dumper.user.js} | 45 ++++++++++++------- 2 files changed, 30 insertions(+), 17 deletions(-) rename extras/characterai-dumper/{characterai-dumper-user.js => characterai-dumper.user.js} (82%) diff --git a/extras/characterai-dumper/README.md b/extras/characterai-dumper/README.md index 169e670..95f63c4 100644 --- a/extras/characterai-dumper/README.md +++ b/extras/characterai-dumper/README.md @@ -6,7 +6,7 @@ This userscript allows you to download your saved messages with any bot you've e - Install a userscript manager. - Personally I tested this with [Violentmonkey](https://violentmonkey.github.io/get-it/) on Firefox, but I think Greasemonkey and Tampermonkey should work as well. -- Install the userscript [from here](https://git.fuwafuwa.moe/waifu-collective/toolbox/raw/branch/master/extras/characterai-dumper/characterai-dumper-user.js). +- Install the userscript [from here](https://git.fuwafuwa.moe/waifu-collective/toolbox/raw/branch/master/extras/characterai-dumper/characterai-dumper.user.js). - Now, while you're talking to a character, click on "View Saved Chats" to go to their histories page: ![Where to find "View Saved Chats"](./example-images/01.png) - After a few seconds, a `Download` link should pop up next to the "Your past conversations with so-and-so" header: diff --git a/extras/characterai-dumper/characterai-dumper-user.js b/extras/characterai-dumper/characterai-dumper.user.js similarity index 82% rename from extras/characterai-dumper/characterai-dumper-user.js rename to extras/characterai-dumper/characterai-dumper.user.js index 7c2c22c..2b6e39a 100644 --- a/extras/characterai-dumper/characterai-dumper-user.js +++ b/extras/characterai-dumper/characterai-dumper.user.js @@ -3,17 +3,17 @@ // @namespace Violentmonkey Scripts // @match https://beta.character.ai/* // @grant none -// @version 1.0 +// @version 1.1 // @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 -// @updateURL https://git.fuwafuwa.moe/waifu-collective/toolbox/raw/branch/master/extras/characterai-dumper/characterai-dumper-user.js +// @downloadURL https://git.fuwafuwa.moe/waifu-collective/toolbox/raw/branch/master/extras/characterai-dumper/characterai-dumper.user.js +// @updateURL https://git.fuwafuwa.moe/waifu-collective/toolbox/raw/branch/master/extras/characterai-dumper/characterai-dumper.user.js // ==/UserScript== const log = (firstArg, ...remainingArgs) => - console.log(`[CharacterAI Dumper v1.0] ${firstArg}`, ...remainingArgs); + console.log(`[CharacterAI Dumper v1.1] ${firstArg}`, ...remainingArgs); log.error = (firstArg, ...remainingArgs) => - console.error(`[CharacterAI Dumper v1.0] ${firstArg}`, ...remainingArgs); + console.error(`[CharacterAI Dumper v1.1] ${firstArg}`, ...remainingArgs); const CHARACTER_INFO_URL = "https://beta.character.ai/chat/character/info/"; const CHARACTER_HISTORIES_URL = @@ -56,15 +56,21 @@ const escapeStringForRegExp = (stringToGoIntoTheRegex) => { }; const anonymizeHistories = (histories) => { - let nameToReplace; + const namesToReplace = new Set(); for (const history of histories.histories) { for (const msg of history.msgs) { if (msg.src.is_human) { - // First, save the user's name so we can replace it in the messages. - nameToReplace ||= msg.display_name; + // First, we save the original name so we can search for it and redact + // it in the messages. + namesToReplace.add(msg.src.user.username); + namesToReplace.add(msg.src.user.first_name); + namesToReplace.add(msg.src.user.account.name); + namesToReplace.add(msg.src.user.name); + namesToReplace.add(msg.src.name); + namesToReplace.add(msg.display_name); - // Need to anonymize `src`. + // Then, we anonymize `src` (since the source is the human). msg.src.user.username = "[USERNAME_REDACTED]"; msg.src.user.first_name = "[FIRST_NAME_REDACTED]"; msg.src.user.account.name = "[ACCOUNT_NAME_REDACTED]"; @@ -72,7 +78,12 @@ const anonymizeHistories = (histories) => { msg.src.name = "[NAME_REDACTED]"; msg.display_name = "[DISPLAY_NAME_REDACTED]"; } else { - nameToReplace ||= msg.tgt.name; + // Same logic as above. + namesToReplace.add(msg.tgt.user.username); + namesToReplace.add(msg.tgt.user.first_name); + namesToReplace.add(msg.tgt.user.account.name); + namesToReplace.add(msg.tgt.user.name); + namesToReplace.add(msg.tgt.name); // Need to anonymize `tgt`. msg.tgt.user.username = "[USERNAME_REDACTED]"; @@ -81,14 +92,16 @@ const anonymizeHistories = (histories) => { msg.tgt.user.name = "[NAME_REDACTED]"; msg.tgt.name = "[NAME_REDACTED]"; - // And since this is a bot message, there's a chance that the bot + // 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. - const replacementRegex = new RegExp( - "\\b" + escapeStringForRegExp(nameToReplace) + "\\b", - "g" - ); - msg.text.replace(replacementRegex, "[DISPLAY_NAME_REDACTED]"); + for (const nameToReplace in namesToReplace) { + const replacementRegex = new RegExp( + "\\b" + escapeStringForRegExp(nameToReplace) + "\\b", + "g" + ); + msg.text.replace(replacementRegex, "[NAME_IN_MESSAGE_REDACTED]"); + } } } }