Skip to content

Instantly share code, notes, and snippets.

@ben-xD
Created October 19, 2023 07:58
Show Gist options
  • Save ben-xD/99ad5494e322942c8f5d7c90323933da to your computer and use it in GitHub Desktop.
Save ben-xD/99ad5494e322942c8f5d7c90323933da to your computer and use it in GitHub Desktop.
Download current ChatGPT chat as markdown file. Just paste it into the dev tools console and it will download.
// Inspired by https://www.reddit.com/r/ChatGPT/comments/zm237o/save_your_chatgpt_conversation_as_a_markdown_file/ + my own customisations
function h(html) {
return html.replace(/<p>/g, '\n\n').replace(/<\/p>/g, '').replace(/<b>/g, '**').replace(/<\/b>/g, '**').replace(/<i>/g, '_').replace(/<\/i>/g, '_').replace(/<code[^>]*>/g, (match) => { const lm = match.match(/class="[^"]*language-([^"]*)"/); return lm ? '\n```' + lm[1] + '\n' : '```'; }).replace(/<\/code[^>]*>/g, '```').replace(/<[^>]*>/g, '').replace(/Copy code/g, '').replace(/This content may violate our content policy. If you believe this to be in error, please submit your feedback — your input will aid our research in this area./g, '').trim();
}
(() => {
const title = document.querySelector("a.dark\\:bg-gray-800 div")?.textContent || "Chat";
const filename = `${title}.md`;
let t = `# ${title}\n`;
const e = document.querySelectorAll(".text-base");
for(const s of e)
s.querySelector(".whitespace-pre-wrap") && (t += `**${s.querySelector('img')?'You':'ChatGPT'}**: ${h(s.querySelector(".whitespace-pre-wrap").innerHTML)}\n\n`);
const o = document.createElement("a");
o.download = filename;
o.href = URL.createObjectURL(new Blob([t]));
o.style.display = "none";
document.body.appendChild(o);
o.click();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment