forked from DarkIlluminatus/ChatGPT-Exported-conversations.json-parser-viewer-and-splitter-web-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
335 lines (280 loc) · 14.7 KB
/
script.js
File metadata and controls
335 lines (280 loc) · 14.7 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { saveAs } from 'https://cdn.skypack.dev/file-saver';
import JSZip from 'https://cdn.skypack.dev/jszip';
document.addEventListener('DOMContentLoaded', () => {
const fileInput = document.getElementById('json-file-input');
const outputDiv = document.getElementById('conversations-output');
const downloadAllBtn = document.getElementById('download-all-btn');
const downloadAllTextBtn = document.getElementById('download-all-text-btn'); // New element
let allConversations = []; // Store conversations globally once loaded
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (!file) {
outputDiv.innerHTML = '<p>No file selected.</p>';
downloadAllBtn.disabled = true;
downloadAllTextBtn.disabled = true; // Disable new button
return;
}
// Check file extension
if (!file.name.endsWith('.json')) {
outputDiv.innerHTML = '<p class="error-message">Please select a .json file.</p>';
downloadAllBtn.disabled = true;
downloadAllTextBtn.disabled = true; // Disable new button
return;
}
const reader = new FileReader();
reader.onload = (e) => {
try {
const jsonContent = JSON.parse(e.target.result);
if (!Array.isArray(jsonContent)) {
outputDiv.innerHTML = '<p class="error-message">Invalid JSON structure. Expected an array of conversations.</p>';
downloadAllBtn.disabled = true;
downloadAllTextBtn.disabled = true; // Disable new button
return;
}
allConversations = jsonContent; // Store the loaded conversations
displayConversations(allConversations);
downloadAllBtn.disabled = allConversations.length === 0; // Enable if conversations exist
downloadAllTextBtn.disabled = allConversations.length === 0; // Enable new button
} catch (error) {
outputDiv.innerHTML = `<p class="error-message">Error parsing JSON file: ${error.message}</p>`;
console.error("Error parsing JSON:", error);
downloadAllBtn.disabled = true;
downloadAllTextBtn.disabled = true; // Disable new button
}
};
reader.onerror = () => {
outputDiv.innerHTML = `<p class="error-message">Error reading file: ${reader.error.message}</p>`;
console.error("Error reading file:", reader.error);
downloadAllBtn.disabled = true;
downloadAllTextBtn.disabled = true; // Disable new button
};
reader.readAsText(file);
});
downloadAllBtn.addEventListener('click', async () => {
if (allConversations.length === 0) {
alert("No conversations to download. Please load a JSON file first.");
return;
}
downloadAllBtn.disabled = true;
const originalText = downloadAllBtn.textContent;
downloadAllBtn.textContent = 'Zipping JSON...';
const zip = new JSZip();
let fileCount = 0;
allConversations.forEach(conv => {
try {
const title = conv.title || 'Untitled Conversation';
const createTime = conv.create_time ? new Date(conv.create_time * 1000).toISOString().slice(0, 10) : 'N/A';
const fileName = `${sanitizeFilename(title)}_chatgpt_${createTime}.json`;
const convJson = JSON.stringify(conv, null, 2);
zip.file(fileName, convJson);
fileCount++;
} catch (error) {
console.error(`Error processing conversation ${conv.title}:`, error);
}
});
if (fileCount === 0) {
outputDiv.insertAdjacentHTML('afterbegin', '<p class="error-message">No valid conversations found to zip.</p>');
downloadAllBtn.textContent = originalText;
downloadAllBtn.disabled = false;
return;
}
try {
const content = await zip.generateAsync({ type: "blob" });
const zipFileName = `all_chatgpt_conversations_${new Date().toISOString().slice(0, 10)}.zip`;
saveAs(content, zipFileName);
} catch (error) {
outputDiv.insertAdjacentHTML('afterbegin', `<p class="error-message">Error generating ZIP file: ${error.message}</p>`);
console.error("Error generating ZIP:", error);
} finally {
downloadAllBtn.textContent = originalText;
downloadAllBtn.disabled = false;
}
});
// New event listener for downloading all as plain text
downloadAllTextBtn.addEventListener('click', async () => {
if (allConversations.length === 0) {
alert("No conversations to download. Please load a JSON file first.");
return;
}
downloadAllTextBtn.disabled = true;
const originalText = downloadAllTextBtn.textContent;
downloadAllTextBtn.textContent = 'Zipping Text...';
const zip = new JSZip();
let fileCount = 0;
allConversations.forEach(conv => {
try {
const title = conv.title || 'Untitled Conversation';
const createTime = conv.create_time ? new Date(conv.create_time * 1000).toISOString().slice(0, 10) : 'N/A';
const fileName = `${sanitizeFilename(title)}_chatgpt_${createTime}.txt`; // .txt extension
const convText = formatConversationAsPlainText(conv); // Use new format function
zip.file(fileName, convText);
fileCount++;
} catch (error) {
console.error(`Error processing conversation ${conv.title} for text export:`, error);
}
});
if (fileCount === 0) {
outputDiv.insertAdjacentHTML('afterbegin', '<p class="error-message">No valid conversations found to zip as text.</p>');
downloadAllTextBtn.textContent = originalText;
downloadAllTextBtn.disabled = false;
return;
}
try {
const content = await zip.generateAsync({ type: "blob" });
const zipFileName = `all_chatgpt_conversations_text_${new Date().toISOString().slice(0, 10)}.zip`;
saveAs(content, zipFileName);
} catch (error) {
outputDiv.insertAdjacentHTML('afterbegin', `<p class="error-message">Error generating Text ZIP file: ${error.message}</p>`);
console.error("Error generating Text ZIP:", error);
} finally {
downloadAllTextBtn.textContent = originalText;
downloadAllTextBtn.disabled = false;
}
});
function displayConversations(conversations) {
outputDiv.innerHTML = ''; // Clear previous content
if (conversations.length === 0) {
outputDiv.innerHTML = '<p>No conversations found in the file.</p>';
return;
}
conversations.forEach(conv => {
const convDiv = document.createElement('div');
convDiv.classList.add('conversation-item');
const title = conv.title || 'Untitled Conversation';
// ChatGPT export `create_time` is a Unix timestamp in seconds
const createTime = conv.create_time ? new Date(conv.create_time * 1000).toLocaleString() : 'N/A';
const titleElem = document.createElement('h3');
titleElem.textContent = title;
const timeElem = document.createElement('p');
timeElem.textContent = `Created: ${createTime}`;
const actionsDiv = document.createElement('div');
actionsDiv.classList.add('conversation-actions');
const viewButton = document.createElement('button');
viewButton.classList.add('view-btn');
viewButton.innerHTML = '👁️ View'; // Eye icon
viewButton.title = 'View Conversation';
const downloadButton = document.createElement('button');
downloadButton.classList.add('download-btn');
downloadButton.innerHTML = '💾 Download JSON'; // Disk icon, text updated
downloadButton.title = 'Download Conversation JSON';
const downloadTextButton = document.createElement('button'); // New button
downloadTextButton.classList.add('download-text-btn');
downloadTextButton.innerHTML = '📄 Download Text'; // Document icon
downloadTextButton.title = 'Download Conversation as Plain Text';
actionsDiv.appendChild(viewButton);
actionsDiv.appendChild(downloadButton);
actionsDiv.appendChild(downloadTextButton); // Add new button
const conversationContentDiv = document.createElement('div');
conversationContentDiv.classList.add('conversation-content');
conversationContentDiv.style.display = 'none'; // Initially hidden
viewButton.addEventListener('click', () => {
// Toggle visibility
const isHidden = conversationContentDiv.style.display === 'none';
conversationContentDiv.style.display = isHidden ? 'block' : 'none';
if (isHidden && conversationContentDiv.children.length === 0) {
// Populate content only if hidden and not already populated
const messages = getConversationMessages(conv);
if (messages.length === 0) {
conversationContentDiv.innerHTML = '<p>No messages found for this conversation.</p>';
} else {
messages.forEach(msg => {
const msgDiv = document.createElement('div');
msgDiv.classList.add('message-item');
msgDiv.classList.add(msg.role === 'user' ? 'user-message' : 'assistant-message');
const roleElem = document.createElement('strong');
roleElem.textContent = `${msg.role}: `;
const contentElem = document.createElement('p');
contentElem.textContent = msg.content;
msgDiv.appendChild(roleElem);
msgDiv.appendChild(contentElem);
conversationContentDiv.appendChild(msgDiv);
});
}
}
});
downloadButton.addEventListener('click', () => {
const convJson = JSON.stringify(conv, null, 2);
const blob = new Blob([convJson], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
const fileName = `${sanitizeFilename(title)}_chatgpt_${new Date(conv.create_time * 1000).toISOString().slice(0, 10)}.json`;
a.download = fileName;
document.body.appendChild(a); // Append to body to make it clickable
a.click();
document.body.removeChild(a); // Clean up
URL.revokeObjectURL(url); // Release the object URL
});
// New event listener for downloading as plain text
downloadTextButton.addEventListener('click', () => {
const textContent = formatConversationAsPlainText(conv);
const blob = new Blob([textContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
const fileName = `${sanitizeFilename(title)}_chatgpt_${new Date(conv.create_time * 1000).toISOString().slice(0, 10)}.txt`; // .txt extension
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
convDiv.appendChild(titleElem);
convDiv.appendChild(timeElem);
convDiv.appendChild(actionsDiv);
convDiv.appendChild(conversationContentDiv);
outputDiv.appendChild(convDiv);
});
}
// Helper function to sanitize filenames
function sanitizeFilename(filename) {
return filename.replace(/[/\\?%*:|"<>]/g, '-').replace(/\s+/g, '_').substring(0, 100);
}
// Function to extract messages from the conversation object
function getConversationMessages(conversation) {
const messages = [];
const mapping = conversation.mapping;
const nodeMap = new Map();
for (const key in mapping) {
nodeMap.set(key, mapping[key]);
}
// Iterate through all nodes in the mapping
for (const nodeId in mapping) {
const node = mapping[nodeId];
// Check if the node contains a message with content
if (node.message && node.message.content && node.message.content.parts) {
messages.push({
role: node.message.author.role,
content: node.message.content.parts.join('\n'), // Join parts if multiple
create_time: node.message.create_time, // Message has its own creation time
id: nodeId // Keep ID for potential future use or debugging
});
}
}
// Sort all extracted messages by their creation time to ensure chronological order
messages.sort((a, b) => a.create_time - b.create_time);
return messages;
}
// New helper function to format conversation as plain text
function formatConversationAsPlainText(conversation) {
const title = conversation.title || 'Untitled Conversation';
const createTime = conversation.create_time ? new Date(conversation.create_time * 1000).toLocaleString() : 'N/A';
const messages = getConversationMessages(conversation);
let text = `Title: ${title}\n`;
text += `Created: ${createTime}\n`;
if (conversation.id) { // Add conversation URL if ID is available
text += `URL: https://chat.openai.com/c/${conversation.id}\n`;
}
text += `\n`; // Add an empty line for separation
if (messages.length === 0) {
text += "--- No messages found for this conversation ---\n";
} else {
messages.forEach(msg => {
const role = msg.role.charAt(0).toUpperCase() + msg.role.slice(1); // Capitalize role (User, Assistant)
text += `--- ${role} ---\n`;
text += `${msg.content}\n\n`; // Add an extra newline after each message for readability
});
}
return text.trim(); // Remove any trailing whitespace/newlines at the very end
}
});