Created
October 3, 2025 23:58
-
-
Save joallard/126afdcbec3544e2935e80d04404abab to your computer and use it in GitHub Desktop.
Extract Markdown transcript from Claude conversation (browser snippet)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// Function to extract text content while preserving line breaks | |
const extractText = element => { | |
let text = ''; | |
for (const node of element.childNodes) { | |
if (node.nodeType === 3) { // Text node | |
text += node.textContent; | |
} else if (node.nodeType === 1) { // Element node | |
// Check for paragraph tag or break | |
if (node.tagName === 'P' || node.tagName === 'DIV') { | |
const innerText = extractText(node); | |
if (innerText.trim()) { | |
text += innerText + '\n\n'; | |
} | |
} else if (node.tagName === 'BR') { | |
text += '\n'; | |
} else { | |
text += extractText(node); | |
} | |
} | |
} | |
return text; | |
}; | |
// Function to convert dialog to markdown | |
const convertToMarkdown = (messages) => { | |
let markdown = ''; | |
messages.forEach((message, index) => { | |
// Add separator between messages | |
if (index > 0) markdown += '\n\n---\n\n'; | |
// Look for artifact title buttons | |
const artifactButtons = message.querySelectorAll('button'); | |
artifactButtons.forEach(button => { | |
const titleDiv = button.querySelector('.text-sm.font-medium'); | |
if (titleDiv) { | |
markdown += `[Artifact: ${titleDiv.textContent}]\n\n`; | |
} | |
}); | |
let content = extractText(message); | |
content = content.replace(/\n{3,}/g, '\n\n').trim(); | |
markdown += content; | |
}); | |
return markdown; | |
}; | |
// (2025-10) | |
const SELECTORS = '.\\!font-user-message, .font-claude-response' | |
const messages = document.querySelectorAll(SELECTORS); | |
const markdown = convertToMarkdown(messages); | |
console.log(markdown); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment