forked from Jamoxidase/Save-Claude-Convo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (109 loc) · 4.35 KB
/
script.js
File metadata and controls
133 lines (109 loc) · 4.35 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
// Configuration for the export
const config = {
includeTimestamps: true,
includeFileContent: true,
exportFormat: 'readable' // 'readable' or 'json'
};
// Format a readable timestamp
function formatTimestamp(isoString) {
return new Date(isoString).toLocaleString();
}
// Format a single message
function formatMessage(message, config) {
let formatted = '';
if (config.includeTimestamps) {
formatted += `[${formatTimestamp(message.created_at)}]\n`;
}
formatted += `${message.sender.charAt(0).toUpperCase() + message.sender.slice(1)}: `;
// Add message content
message.content.forEach(content => {
if (content.type === 'text') {
formatted += content.text;
}
});
// Add file content if present and enabled
if (config.includeFileContent && message.attachments && message.attachments.length > 0) {
message.attachments.forEach(attachment => {
if (attachment.extracted_content) {
formatted += '\n\n[Attached File Content]:\n' + attachment.extracted_content;
}
});
}
return formatted + '\n\n';
}
// Format the entire conversation
function formatConversation(data, config) {
if (config.exportFormat === 'json') {
return JSON.stringify(data, null, 2);
}
let output = 'Claude Chat Export\n';
output += `Timestamp: ${formatTimestamp(data.created_at)}\n\n`;
data.chat_messages.forEach(message => {
output += formatMessage(message, config);
});
return output;
}
// Download the formatted content
function downloadContent(content, format) {
const blob = new Blob([content], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
a.href = url;
a.download = `claude-chat-${timestamp}.${format === 'json' ? 'json' : 'txt'}`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Function to extract a snippet based on starting and ending indices
function extractSnippet(startIndex, endIndex) {
// Select all <script nonce> tags
const scriptTags = document.querySelectorAll('script[nonce]');
// Iterate through each script tag and check for "lastActiveOrg"
for (let script of scriptTags) {
const content = script.textContent;
console.log(content); // Output the content of each script tag
// Check if the content contains "lastActiveOrg"
const index = content.indexOf('lastActiveOrg');
if (index !== -1) {
console.log('Found "lastActiveOrg" in script content');
const snippet = content.substring(index + 28, index + 64);
console.log('Snippet:', snippet); // Log the extracted snippet
return snippet; // Return the extracted snippet
}
}
console.log('Finished checking all script tags');
return null; // Return null if "lastActiveOrg" is not found
}
// Main export function
async function exportConversation() {
try {
// Get chat UUID from URL
const chatId = window.location.pathname.split('/').pop();
console.log('Chat UUID:', chatId);
// Extract org ID using the new parsing logic
const orgId = extractSnippet(28, 64);
if (!orgId) {
throw new Error('Could not find organization ID');
}
console.log('Org ID:', orgId);
// Construct and fetch the API URL
const apiUrl = `https://claude.ai/api/organizations/${orgId}/chat_conversations/${chatId}?tree=True&rendering_mode=messages&render_all_tools=true`;
console.log('Fetching from:', apiUrl);
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`Failed to fetch conversation data: ${response.status} ${response.statusText}`);
}
const data = await response.json();
// Format and download the conversation
const formatted = formatConversation(data, config);
downloadContent(formatted, config.exportFormat);
console.log('Export completed successfully!');
} catch (error) {
console.error('Error exporting chat:', error);
alert('Error exporting chat: ' + error.message);
}
}
// Run the export
exportConversation();