Skip to content

Instantly share code, notes, and snippets.

@RohanAwhad
Created March 1, 2024 00:00
Show Gist options
  • Save RohanAwhad/e000ba63523e6289e3f187a8775c2c41 to your computer and use it in GitHub Desktop.
Save RohanAwhad/e000ba63523e6289e3f187a8775c2c41 to your computer and use it in GitHub Desktop.
(function() {
// Initialize an array to hold the conversation data
let conversationData = [];
// Select all conversation blocks
let conversationBlocks = document.querySelectorAll('[data-testid^="conversation-turn"]');
conversationBlocks.forEach(block => {
// Determine the role directly from data attributes in the HTML
let roleElement = block.querySelector('[data-message-author-role]');
let role = roleElement ? roleElement.getAttribute('data-message-author-role') : 'unknown';
// Extract the name directly from the HTML content, assuming it's indicated before the message content
let nameElement = block.querySelector('.font-semibold.select-none');
let name = nameElement ? nameElement.innerText : 'Unknown';
// Extract the textual content of the message
let contentElement = block.querySelector('[data-message-id]');
let content = contentElement ? contentElement.innerText : '';
conversationData.push({ role, name, content });
});
// Convert the conversation data to a JSON string
let jsonOutput = JSON.stringify(conversationData, null, 2);
// Function to download the data as a JSON file
function downloadDataAsJson(data, filename) {
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename || 'conversationData.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Call the download function with the JSON output
downloadDataAsJson(jsonOutput, 'conversationData.json');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment