-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcha.js
285 lines (253 loc) · 10.1 KB
/
cha.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// ==UserScript==
// @name CharacterAI Dumper
// @namespace Violentmonkey Scripts
// @match https://character.ai/*
// @grant none
// @version 1.4
// @author 0x000011b
// @description Allows downloading saved chat messages and character definitions from CharacterAI.
// @downloadURL https://github.com/0x000011b/characterai-dumper/raw/master/characterai-dumper.user.js
// @updateURL https://github.com/0x000011b/characterai-dumper/raw/master/characterai-dumper.user.js
// ==/UserScript==
const log = (firstArg, ...remainingArgs) =>
console.log(`[CharacterAI Dumper v1.4] ${firstArg}`, ...remainingArgs);
log.error = (firstArg, ...remainingArgs) =>
console.error(`[CharacterAI Dumper v1.4] ${firstArg}`, ...remainingArgs);
// Endpoints to intercept.
const CHARACTER_INFO_URL = "https://beta.character.ai/chat/character/info/";
const CHARACTER_EXTRA_INFO_URL = "https://beta.character.ai/chat/character/";
const CHARACTER_HISTORIES_URL =
"https://beta.character.ai/chat/character/histories/";
/** Maps a character's identifier to their basic info + chat histories. */
const characterToSavedDataMap = {};
/** Creates the "Download" link on the "View Saved Chats" page. */
const addDownloadLinkInSavedChats = (dataString, filename) => {
// Don't create duplicate links.
if (document.getElementById("injected-chat-dl-link")) {
return;
}
// We want to add a link next to the "your past conversations with XXX" text.
const suspectedElements = document.getElementsByClassName("home-sec-header");
for (const element of suspectedElements) {
if (!element.textContent.includes("Your Past Conversations with")) {
continue;
}
const dataBlob = new Blob([dataString], { type: "text/plain" });
const downloadLink = document.createElement("a");
downloadLink.id = "injected-chat-dl-link";
downloadLink.textContent = "Download";
downloadLink.href = URL.createObjectURL(dataBlob);
downloadLink.download = filename;
downloadLink.style = "padding-left: 8px";
element.appendChild(downloadLink);
}
};
/** Creates the "Download" link in the "Character Editor" page. */
const addDownloadLinkInCharacterEditor = (
dataString,
filename,
characterName
) => {
if (document.getElementById("injected-character-info-dl-link")) {
return;
}
const suspectedElements = document.querySelectorAll(
"div.p-0.m-1.mb-3.border.rounded.m-1"
);
for (const element of suspectedElements) {
if (!element.textContent.includes(characterName)) {
continue;
}
const dataBlob = new Blob([dataString], { type: "text/plain" });
const downloadLink = document.createElement("a");
downloadLink.id = "injected-character-info-dl-link";
downloadLink.textContent = "Download";
downloadLink.href = URL.createObjectURL(dataBlob);
downloadLink.download = filename;
downloadLink.style = "padding-left: 66px";
element.appendChild(downloadLink);
}
};
/** Escapes a string so it can be used inside a regex. */
const escapeStringForRegExp = (stringToGoIntoTheRegex) => {
return stringToGoIntoTheRegex.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
};
/** Takes in chat histories and anonymizes them. */
const anonymizeHistories = (histories) => {
const namesToReplace = new Set();
for (const history of histories.histories) {
for (const msg of history.msgs) {
if (msg.src.is_human) {
// 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);
// 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]";
msg.src.user.name = "[NAME_REDACTED]";
msg.src.name = "[NAME_REDACTED]";
msg.display_name = "[DISPLAY_NAME_REDACTED]";
} else {
// 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]";
msg.tgt.user.first_name = "[FIRST_NAME_REDACTED]";
msg.tgt.user.account.name = "[ACCOUNT_NAME_REDACTED]";
msg.tgt.user.name = "[NAME_REDACTED]";
msg.tgt.name = "[NAME_REDACTED]";
// 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.
namesToReplace.forEach((nameToReplace) => {
if (!nameToReplace) {
return;
}
const replacementRegex = new RegExp(
"\\b" + escapeStringForRegExp(nameToReplace) + "\\b",
"g"
);
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.
if (history.msgs.length) {
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
// call site even though it's technically useless (and slightly misleading).
return histories;
};
/** Configures XHook to intercept the endpoints we care about. */
const configureXHookIntercepts = () => {
xhook.after((_req, res) => {
try {
const endpoint = res.finalUrl;
if (
endpoint !== CHARACTER_INFO_URL &&
endpoint !== CHARACTER_HISTORIES_URL &&
endpoint !== CHARACTER_EXTRA_INFO_URL
) {
// We don't care about other endpoints.
return;
}
const data = JSON.parse(res.data);
let characterIdentifier;
if (res.finalUrl === CHARACTER_INFO_URL) {
characterIdentifier = data.character.name.trim();
data.character.user__username = "[BOT_CREATOR_NAME_REDACTED]";
log(`Got character info for ${characterIdentifier}, caching...`);
if (!characterToSavedDataMap[characterIdentifier]) {
characterToSavedDataMap[characterIdentifier] = {};
}
characterToSavedDataMap[characterIdentifier].info = data;
} else if (res.finalUrl === CHARACTER_HISTORIES_URL) {
characterIdentifier = data.histories[0].msgs[0].src.name.trim();
log(`Got chat histories for ${characterIdentifier}, caching...`);
if (!characterToSavedDataMap[characterIdentifier]) {
characterToSavedDataMap[characterIdentifier] = {};
}
characterToSavedDataMap[characterIdentifier].histories =
anonymizeHistories(data);
} else if (res.finalUrl === CHARACTER_EXTRA_INFO_URL) {
characterIdentifier = data.character.name.trim();
data.user__username = "[BOT_CREATOR_NAME_REDACTED]";
data.character.user__username = "[BOT_CREATOR_NAME_REDACTED]";
log(
`Got definitions for ${characterIdentifier}, creating download link.`
);
log("If it doesn't show up, here's the data:", JSON.stringify(data));
// The character editor returns all the info we want in a single
// request, so we can just create the button and return from this
// function already.
setTimeout(
() =>
addDownloadLinkInCharacterEditor(
JSON.stringify(data),
`${characterIdentifier} (Definitions).json`,
characterIdentifier
),
2000
);
return;
}
const currentCharacter = characterToSavedDataMap[characterIdentifier];
if (currentCharacter.info && currentCharacter.histories) {
// We have all the downloadable data for this character, and we're on the
// correct page. Create the download link.
log(
`Got all the data for ${characterIdentifier}, creating download link.`
);
log(
"If it doesn't show up, here's the data:",
JSON.stringify(currentCharacter)
);
// For some reason, the link doesn't get added if we call this right now,
// so we wait a little while instead. Probably React re-render fuckery.
setTimeout(
() =>
addDownloadLinkInSavedChats(
JSON.stringify(currentCharacter),
`${characterIdentifier}.json`
),
2000
);
}
} catch (err) {
log.error("ERROR:", err);
}
});
};
// This is where XHook (lib for intercepting XHR/AJAX calls) gets injected into
// the document, and once it gets properly parsed it'll call out to the setup
// function.
//
// Copy-pasted and slightly adapted from: https://stackoverflow.com/a/8578840
log("Injecting XHook to intercept XHR/AJAX calls.");
(function (document, elementTagName, elementTagId) {
var js,
fjs = document.getElementsByTagName(elementTagName)[0];
if (document.getElementById(elementTagId)) {
return;
}
js = document.createElement(elementTagName);
js.id = elementTagId;
js.onload = function () {
log("Done! Configuring intercepts.");
configureXHookIntercepts();
};
// Link to hosted version taken from the official repo:
// https://github.com/jpillora/xhook
js.src = "https://jpillora.com/xhook/dist/xhook.min.js";
fjs.parentNode.insertBefore(js, fjs);
})(document, "script", "xhook");