Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save democratize-technology-code-developer/d0747dedb36bd42c6955dd3e9bc03496 to your computer and use it in GitHub Desktop.
Save democratize-technology-code-developer/d0747dedb36bd42c6955dd3e9bc03496 to your computer and use it in GitHub Desktop.
Claude Conversation Download
(async () => {
const saveData = (() => {
const a = document.createElement('a');
document.body.appendChild(a);
a.style = 'display: none';
return (data, fileName) => {
const json = JSON.stringify(data),
blob = new Blob([data], { type: 'text/markdown' }),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
})();
const getCookie = (name) => {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
};
const getSupportedTypes = (type) => {
const types = {
text: ({ text }) => text,
thinking: ({ thinking }) =>
`
<think>
${thinking}
</think>
`,
tool_use: ({ input: { content, language = '' } }) =>
content !== undefined
? `
\`\`\`${language}
${content}
\`\`\`
`
: '',
tool_result: ({ name, content: [{ type: toolResultType, ...rest }] }) => `
<${name}>
${getSupportedTypes(toolResultType)(rest)}
</${name}>
`,
default: () => '',
};
return types[type] ?? types['default'];
};
const pieces = window.location.pathname.split('/');
const orgId = getCookie('lastActiveOrg');
const chatId = pieces[pieces.length - 1];
const { chat_messages: messages, name: title } = await fetch(
`https://claude.ai/api/organizations/${orgId}/chat_conversations/${chatId}?tree=True&rendering_mode=messages&render_all_tools=true`,
).then((r) => r.json());
saveData(
messages.reduce(
(acc, { sender, content }) =>
(acc += `
**${sender.toUpperCase()}**
${content
.map(({ type, ...item }) => getSupportedTypes(type)(item))
.filter(Boolean)
.join('\n')}
`),
`# ${title}\n`,
),
`${chatId}.md`,
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment