├── example-images ├── 01.png ├── 02.png └── 03.png ├── README.md └── characterai-dumper.user.js /example-images/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x000011b/characterai-dumper/HEAD/example-images/01.png -------------------------------------------------------------------------------- /example-images/02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x000011b/characterai-dumper/HEAD/example-images/02.png -------------------------------------------------------------------------------- /example-images/03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x000011b/characterai-dumper/HEAD/example-images/03.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deprecation Notice 2 | 3 | The CharacterAI website has finally been updated to fix the wasteful serialization of conversation histories. Unfortunately, that change has broken this userscript. 4 | 5 | Due to time constraints I am no longer maintaining this repository, and instead recommend that you look for other tools to export your CAI conversations. One such tool I've been suggested is a browser extension called "CAI Tools". I have not tested it myself and as such cannot vouch for it - but if you need a replacement for this userscript, I suggest searching for it and giving it a shot. 6 | 7 | # CharacterAI Dumper Userscript 8 | 9 | This userscript allows you to download your saved messages with any bot you've ever talked to, given you can reach their chat history page. If you're a bot creator, it also allows you to separately download your bot's definitions. 10 | 11 | ## How to use 12 | 13 | - Install a userscript manager. 14 | - Personally I tested this with [Violentmonkey](https://violentmonkey.github.io/get-it/) on Firefox, but I think Greasemonkey and Tampermonkey should work as well. 15 | - Install the userscript [from here](https://github.com/0x000011b/characterai-dumper/raw/master/characterai-dumper.user.js). 16 | - Now, while you're talking to a character, click on "View Saved Chats" to go to their histories page: 17 | ![Where to find "View Saved Chats"](./example-images/01.png) 18 | - After a few seconds, a `Download` link should pop up next to the "Your past conversations with so-and-so" header: 19 | ![What the download link looks like](./example-images/02.png) 20 | - Clicking on the link will download a `.json` file containing the bot's basic info (name, description, greeting) and all the interactions you've ever had with it. 21 | - If you're a bot creator, you can also head over into the Character Editor to download a bot's definitions: 22 | ![Where to find the definitions download](./example-images/03.png) 23 | 24 | ## Other notes 25 | 26 | - If you've never used the "Save and Start New Chat" feature, you won't have the "View Saved Chats" option shown in the first screenshot. 27 | 28 | - That's fine, it just means you'll need to manually access the histories page by replacing the `/chat` path with `/histories` in the URL. For example, if you're at: 29 | 30 | https://beta.character.ai/chat?char={{BOT_ID_HERE}} 31 | 32 | Just rewrite the URL so it reads: 33 | 34 | https://beta.character.ai/histories?char={{BOT_ID_HERE}} 35 | 36 | And you'll reach the page that should show the `Download` link. 37 | 38 | - The script attempts to anonymize the dumped data (it scrubs known sensitive fields and attempts to replace any instances of your name within messages), but if you're paranoid, you should open the downloaded JSON and search for your username/email/display name just to make sure. 39 | 40 | ## Changelog 41 | 42 | - **v1.4:** 43 | - Fixed a bug where the `Download` link wouldn't show up for a given bot if you had a conversation with it where all of the messages were deleted. 44 | - **v1.3:** 45 | - Implemented support for downloading a character's definitions from the Character Editor page. 46 | - Fixed a bug where the `Download` link was not showing up on some bots that had trailing whitespace in their names (e.g. `2B `). 47 | - **v1.2:** 48 | - Fixed a bug where the user's display name was not being redacted inside messages. 49 | - **v1.1:** 50 | - File was renamed so userscript managers pick up that it's a userscript and offer to install automatically. 51 | - If you installed v1.0, you need to manually uninstall and re-install from the new link. 52 | - **v1.0:** 53 | - Initial release. 54 | 55 | ## Troubleshooting 56 | 57 | If the `Download` link doesn't show up after a few seconds and you're on the proper page, check the DevTools console for errors. 58 | 59 | Usually, failures are caused by some browser extension or configuration that stops the userscript from loading its external dependencies properly. However, there are two other known failure modes that seem to be _somewhat_ common: 60 | 61 | - Sometimes a character seems to have mismatched names depending on which API endpoint is being hit. In those cases, you'll need to manually download the response data instead of using the userscript, as described [here](https://github.com/0x000011b/characterai-dumper/issues/5#issuecomment-1436033283). 62 | - For whatever reason, some people report certain combinations of browser/OS/userscript managers not working, but upon switching to another browser/userscript manager everything works fine. If the download link never shows up, consider trying another browser or userscript manager (or both). 63 | -------------------------------------------------------------------------------- /characterai-dumper.user.js: -------------------------------------------------------------------------------- 1 | // ==UserScript== 2 | // @name CharacterAI Dumper 3 | // @namespace Violentmonkey Scripts 4 | // @match https://beta.character.ai/* 5 | // @grant none 6 | // @version 1.4 7 | // @author 0x000011b 8 | // @description Allows downloading saved chat messages and character definitions from CharacterAI. 9 | // @downloadURL https://github.com/0x000011b/characterai-dumper/raw/master/characterai-dumper.user.js 10 | // @updateURL https://github.com/0x000011b/characterai-dumper/raw/master/characterai-dumper.user.js 11 | // ==/UserScript== 12 | 13 | const log = (firstArg, ...remainingArgs) => 14 | console.log(`[CharacterAI Dumper v1.4] ${firstArg}`, ...remainingArgs); 15 | log.error = (firstArg, ...remainingArgs) => 16 | console.error(`[CharacterAI Dumper v1.4] ${firstArg}`, ...remainingArgs); 17 | 18 | // Endpoints to intercept. 19 | const CHARACTER_INFO_URL = "https://beta.character.ai/chat/character/info/"; 20 | const CHARACTER_EXTRA_INFO_URL = "https://beta.character.ai/chat/character/"; 21 | const CHARACTER_HISTORIES_URL = 22 | "https://beta.character.ai/chat/character/histories/"; 23 | 24 | /** Maps a character's identifier to their basic info + chat histories. */ 25 | const characterToSavedDataMap = {}; 26 | 27 | /** Creates the "Download" link on the "View Saved Chats" page. */ 28 | const addDownloadLinkInSavedChats = (dataString, filename) => { 29 | // Don't create duplicate links. 30 | if (document.getElementById("injected-chat-dl-link")) { 31 | return; 32 | } 33 | 34 | // We want to add a link next to the "your past conversations with XXX" text. 35 | const suspectedElements = document.getElementsByClassName("home-sec-header"); 36 | for (const element of suspectedElements) { 37 | if (!element.textContent.includes("Your Past Conversations with")) { 38 | continue; 39 | } 40 | 41 | const dataBlob = new Blob([dataString], { type: "text/plain" }); 42 | const downloadLink = document.createElement("a"); 43 | downloadLink.id = "injected-chat-dl-link"; 44 | downloadLink.textContent = "Download"; 45 | downloadLink.href = URL.createObjectURL(dataBlob); 46 | downloadLink.download = filename; 47 | downloadLink.style = "padding-left: 8px"; 48 | element.appendChild(downloadLink); 49 | } 50 | }; 51 | 52 | /** Creates the "Download" link in the "Character Editor" page. */ 53 | const addDownloadLinkInCharacterEditor = ( 54 | dataString, 55 | filename, 56 | characterName 57 | ) => { 58 | if (document.getElementById("injected-character-info-dl-link")) { 59 | return; 60 | } 61 | 62 | const suspectedElements = document.querySelectorAll( 63 | "div.p-0.m-1.mb-3.border.rounded.m-1" 64 | ); 65 | for (const element of suspectedElements) { 66 | if (!element.textContent.includes(characterName)) { 67 | continue; 68 | } 69 | 70 | const dataBlob = new Blob([dataString], { type: "text/plain" }); 71 | const downloadLink = document.createElement("a"); 72 | downloadLink.id = "injected-character-info-dl-link"; 73 | downloadLink.textContent = "Download"; 74 | downloadLink.href = URL.createObjectURL(dataBlob); 75 | downloadLink.download = filename; 76 | downloadLink.style = "padding-left: 66px"; 77 | element.appendChild(downloadLink); 78 | } 79 | }; 80 | 81 | /** Escapes a string so it can be used inside a regex. */ 82 | const escapeStringForRegExp = (stringToGoIntoTheRegex) => { 83 | return stringToGoIntoTheRegex.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); 84 | }; 85 | 86 | /** Takes in chat histories and anonymizes them. */ 87 | const anonymizeHistories = (histories) => { 88 | const namesToReplace = new Set(); 89 | 90 | for (const history of histories.histories) { 91 | for (const msg of history.msgs) { 92 | if (msg.src.is_human) { 93 | // First, we save the original name so we can search for it and redact 94 | // it in the messages. 95 | namesToReplace.add(msg.src.user.username); 96 | namesToReplace.add(msg.src.user.first_name); 97 | namesToReplace.add(msg.src.user.account.name); 98 | namesToReplace.add(msg.src.user.name); 99 | namesToReplace.add(msg.src.name); 100 | namesToReplace.add(msg.display_name); 101 | 102 | // Then, we anonymize `src` (since the source is the human). 103 | msg.src.user.username = "[USERNAME_REDACTED]"; 104 | msg.src.user.first_name = "[FIRST_NAME_REDACTED]"; 105 | msg.src.user.account.name = "[ACCOUNT_NAME_REDACTED]"; 106 | msg.src.user.name = "[NAME_REDACTED]"; 107 | msg.src.name = "[NAME_REDACTED]"; 108 | msg.display_name = "[DISPLAY_NAME_REDACTED]"; 109 | } else { 110 | // Same logic as above. 111 | namesToReplace.add(msg.tgt.user.username); 112 | namesToReplace.add(msg.tgt.user.first_name); 113 | namesToReplace.add(msg.tgt.user.account.name); 114 | namesToReplace.add(msg.tgt.user.name); 115 | namesToReplace.add(msg.tgt.name); 116 | 117 | // Need to anonymize `tgt`. 118 | msg.tgt.user.username = "[USERNAME_REDACTED]"; 119 | msg.tgt.user.first_name = "[FIRST_NAME_REDACTED]"; 120 | msg.tgt.user.account.name = "[ACCOUNT_NAME_REDACTED]"; 121 | msg.tgt.user.name = "[NAME_REDACTED]"; 122 | msg.tgt.name = "[NAME_REDACTED]"; 123 | 124 | // Now, since this is a bot message, there's a chance that the bot 125 | // uttered the user's name, so let's replace that inside the message 126 | // text. 127 | namesToReplace.forEach((nameToReplace) => { 128 | if (!nameToReplace) { 129 | return; 130 | } 131 | 132 | const replacementRegex = new RegExp( 133 | "\\b" + escapeStringForRegExp(nameToReplace) + "\\b", 134 | "g" 135 | ); 136 | msg.text = msg.text.replace( 137 | replacementRegex, 138 | "[NAME_IN_MESSAGE_REDACTED]" 139 | ); 140 | }); 141 | } 142 | } 143 | 144 | // And just being extra paranoid: by the time we've gone through both user 145 | // _and_ bot messages, we might've seen more names to redact, so let's go 146 | // back to the first message and attempt to redact it again just in case we 147 | // have new names. 148 | if (history.msgs.length) { 149 | namesToReplace.forEach((nameToReplace) => { 150 | if (!nameToReplace) { 151 | return; 152 | } 153 | 154 | const replacementRegex = new RegExp( 155 | "\\b" + escapeStringForRegExp(nameToReplace) + "\\b", 156 | "g" 157 | ); 158 | history.msgs[0].text = history.msgs[0].text.replace( 159 | replacementRegex, 160 | "[NAME_IN_MESSAGE_REDACTED]" 161 | ); 162 | }); 163 | } 164 | } 165 | 166 | // This was modified in-place, but we return it here for simplicity at the 167 | // call site even though it's technically useless (and slightly misleading). 168 | return histories; 169 | }; 170 | 171 | /** Configures XHook to intercept the endpoints we care about. */ 172 | const configureXHookIntercepts = () => { 173 | xhook.after((_req, res) => { 174 | try { 175 | const endpoint = res.finalUrl; 176 | if ( 177 | endpoint !== CHARACTER_INFO_URL && 178 | endpoint !== CHARACTER_HISTORIES_URL && 179 | endpoint !== CHARACTER_EXTRA_INFO_URL 180 | ) { 181 | // We don't care about other endpoints. 182 | return; 183 | } 184 | 185 | const data = JSON.parse(res.data); 186 | let characterIdentifier; 187 | 188 | if (res.finalUrl === CHARACTER_INFO_URL) { 189 | characterIdentifier = data.character.name.trim(); 190 | data.character.user__username = "[BOT_CREATOR_NAME_REDACTED]"; 191 | 192 | log(`Got character info for ${characterIdentifier}, caching...`); 193 | 194 | if (!characterToSavedDataMap[characterIdentifier]) { 195 | characterToSavedDataMap[characterIdentifier] = {}; 196 | } 197 | characterToSavedDataMap[characterIdentifier].info = data; 198 | } else if (res.finalUrl === CHARACTER_HISTORIES_URL) { 199 | characterIdentifier = data.histories[0].msgs[0].src.name.trim(); 200 | log(`Got chat histories for ${characterIdentifier}, caching...`); 201 | 202 | if (!characterToSavedDataMap[characterIdentifier]) { 203 | characterToSavedDataMap[characterIdentifier] = {}; 204 | } 205 | characterToSavedDataMap[characterIdentifier].histories = 206 | anonymizeHistories(data); 207 | } else if (res.finalUrl === CHARACTER_EXTRA_INFO_URL) { 208 | characterIdentifier = data.character.name.trim(); 209 | data.user__username = "[BOT_CREATOR_NAME_REDACTED]"; 210 | data.character.user__username = "[BOT_CREATOR_NAME_REDACTED]"; 211 | 212 | log( 213 | `Got definitions for ${characterIdentifier}, creating download link.` 214 | ); 215 | 216 | log("If it doesn't show up, here's the data:", JSON.stringify(data)); 217 | 218 | // The character editor returns all the info we want in a single 219 | // request, so we can just create the button and return from this 220 | // function already. 221 | setTimeout( 222 | () => 223 | addDownloadLinkInCharacterEditor( 224 | JSON.stringify(data), 225 | `${characterIdentifier} (Definitions).json`, 226 | characterIdentifier 227 | ), 228 | 2000 229 | ); 230 | return; 231 | } 232 | 233 | const currentCharacter = characterToSavedDataMap[characterIdentifier]; 234 | if (currentCharacter.info && currentCharacter.histories) { 235 | // We have all the downloadable data for this character, and we're on the 236 | // correct page. Create the download link. 237 | log( 238 | `Got all the data for ${characterIdentifier}, creating download link.` 239 | ); 240 | 241 | log( 242 | "If it doesn't show up, here's the data:", 243 | JSON.stringify(currentCharacter) 244 | ); 245 | 246 | // For some reason, the link doesn't get added if we call this right now, 247 | // so we wait a little while instead. Probably React re-render fuckery. 248 | setTimeout( 249 | () => 250 | addDownloadLinkInSavedChats( 251 | JSON.stringify(currentCharacter), 252 | `${characterIdentifier}.json` 253 | ), 254 | 2000 255 | ); 256 | } 257 | } catch (err) { 258 | log.error("ERROR:", err); 259 | } 260 | }); 261 | }; 262 | 263 | // This is where XHook (lib for intercepting XHR/AJAX calls) gets injected into 264 | // the document, and once it gets properly parsed it'll call out to the setup 265 | // function. 266 | // 267 | // Copy-pasted and slightly adapted from: https://stackoverflow.com/a/8578840 268 | log("Injecting XHook to intercept XHR/AJAX calls."); 269 | (function (document, elementTagName, elementTagId) { 270 | var js, 271 | fjs = document.getElementsByTagName(elementTagName)[0]; 272 | if (document.getElementById(elementTagId)) { 273 | return; 274 | } 275 | js = document.createElement(elementTagName); 276 | js.id = elementTagId; 277 | js.onload = function () { 278 | log("Done! Configuring intercepts."); 279 | configureXHookIntercepts(); 280 | }; 281 | // Link to hosted version taken from the official repo: 282 | // https://github.com/jpillora/xhook 283 | js.src = "https://jpillora.com/xhook/dist/xhook.min.js"; 284 | fjs.parentNode.insertBefore(js, fjs); 285 | })(document, "script", "xhook"); 286 | --------------------------------------------------------------------------------