Skip to content

Instantly share code, notes, and snippets.

@melek
Created June 28, 2024 15:12
Show Gist options
  • Save melek/ae6b0f8a32a163c8546ce2ef37103077 to your computer and use it in GitHub Desktop.
Save melek/ae6b0f8a32a163c8546ce2ef37103077 to your computer and use it in GitHub Desktop.
Extract Simplified DocsBot Transcript
/**
* Extracts and formats the chat transcript from DocsBot.
*
* This function retrieves the chat messages from the DocsBot shadow DOM,
* formats them into a readable transcript, and converts any HTML links
* within the messages to Markdown format.
*
* @returns {string|null} A formatted transcript string or null if no messages are found.
*/
function getDocsBotTranscript() {
// Check if the shadow root is available
if (!DocsBotAI.el.shadowRoot) {
return null;
}
// Find the chat messages within the shadow root
const chatWrapper = DocsBotAI.el.shadowRoot.querySelector('.docsbot-wrapper');
if (!chatWrapper) {
return null;
}
const messages = chatWrapper.querySelectorAll('.docsbot-user-chat-message, .docsbot-chat-bot-message');
if (messages.length === 0) {
return null;
}
// Function to convert HTML links to Markdown
function convertLinksToMarkdown(text) {
return text.replace(/<a href="([^"]+)"[^>]*>([^<]+)<\/a>/g, '[$2]($1)');
}
// Build the transcript
let transcript = "DocsBot Transcript\n\n";
messages.forEach(message => {
if (message.classList.contains('docsbot-user-chat-message')) {
transcript += "USER: " + message.textContent.trim() + "\n\n";
} else if (message.classList.contains('docsbot-chat-bot-message')) {
let messageContent = message.querySelector('span').innerHTML.trim();
messageContent = convertLinksToMarkdown(messageContent);
transcript += "DOCSBOT: " + messageContent + "\n\n";
}
});
return transcript;
}
// Example usage:
const transcript = getDocsBotTranscript();
console.log(transcript);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment