Skip to content

Instantly share code, notes, and snippets.

@nero-dv
Created March 9, 2023 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nero-dv/2b2150623034584c1f0c78ed76d068cd to your computer and use it in GitHub Desktop.
Save nero-dv/2b2150623034584c1f0c78ed76d068cd to your computer and use it in GitHub Desktop.
ChatGPT Chat Exports via Tampermonkey
// ==UserScript==
// @name ChatGPT Exporter
// @namespace https://chat.openai.com/chat
// @version 0.1
// @description This script exports the chat history of the ChatGPT chatbot to a file. It is meant to be used with the Tampermonkey extension for Firefox and (Chrome untested).
// @author Louis Del Valle
// @match *://*/*
// @icon
// @grant none
// ==/UserScript==
(function () {
"use strict";
document.body
.getElementsByClassName("flex h-full flex-1 flex-col space-y-1 p-2")[0]
.appendChild(
document.createElement("div")
).innerHTML = `<a id="export-button" class="flex py-3 px-3 items-center gap-3 rounded-md hover:bg-gray-500/10 text-white cursor-pointer text-sm">
<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" height="1em" width="1em"
xmlns="http://www.w3.org/2000/svg">
<path d="M12 3C10.9767 3 9.95334 3.11763 8.95043 3.35288C6.17301 4.00437 4.00437 6.17301 3.35288 8.95043C2.88237 10.9563 2.88237 13.0437 3.35288 15.0496C4.00437 17.827 6.17301 19.9956 8.95044 20.6471C10.9563 21.1176 13.0437 21.1176 15.0496 20.6471C17.827 19.9956 19.9956 17.827 20.6471 15.0496C20.8824 14.0466 21 13.0233 21 12"></path>
</svg>Export Chat</a>`;
})();
function saveAs(data, fileName) {
// save data as txt file
var textToSaveAsBlob = new Blob([data], { type: "text/plain" }),
url = window.URL.createObjectURL(textToSaveAsBlob);
const a = document.createElement("a");
a.href = url;
a.download = fileName;
a.click();
}
function pullResponses() {
let items = [];
var response = "";
let chatHistory = document.getElementsByClassName(
"min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap"
);
for (var i = 0; i < chatHistory.length; i++) {
if (i % 2 == 0) {
response = "\n\nQuery: " + chatHistory[i].textContent;
} else {
response = "\n\nResponse: " + chatHistory[i].textContent;
}
items.push(response);
}
return items;
}
document.getElementById("export-button").addEventListener("click", function () {
var date = new Date().toISOString();
// var time = date.getTime().toISOString();
let items = pullResponses();
saveAs(items, "chatHistory_" + date + ".txt");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment