About: JavaScript expert, mentor, and Open Source developer
Location:
Poland
Joined:
Jul 4, 2018
Save chatGPT conversation as HTML file
Publish Date: Dec 3 '22
42 41
I've started to play with chatGPT from OpenAI and wanted to save the conversation. My first attempt was using GoFullScreen browser extension but the output file didn't look good. So I've figure out that I create some code that will download the file in HTML format.
TL;DR at the end there is bookmarklet that you can copy.
So first we need is to grab div with the thread. It's nice that it's ReactJS with styled components so beginning of the component is nice class name the same as component name.
This is the HTML:
NOTE: this is a view of the DOM in Google Dev Tools (to open right click anywhere on the page and in context menu pick "inspect element").
as you can see the class starts with "ThreadLayout__NodeWrapper" so to select that element you need this code:
It's attribute selector that match beginning of the text.
Next is grabbing the HTML of that element and converting it to blob. As I never remember how to convert String to blob (you don't have to remember this stuff if you don't do them often). I did a Google Search and found javascript.info article about blob it looks like this:
constblob=newBlob([html],{type:'text/html'});
If you ask why we need blob object, it's because we want to convert that to URL, we could use Data URI, but the content may be too big for a URL. So we need to create what so called Object URL. As with Blob I didn't remembered what was the API, so I've searched "Blob to URL JavaScript" and first result was MDN and createObjectURL. So we need to do:
consturl=URL.createObjectURL(blob);
So now if we inspect the url you will see something like this:
It's object created from blob in memory and if you use this browser will use it as reference to HTML content we created.
To download the file there is this trick that I keep using, if you know better way let me know.
functiondownload(url,filename){consta=document.createElement('a');a.href=url;a.download=filename;// we need add link to body since some browsers// will ignore the download otherwisedocument.body.appendChild(a);a.click();// you can also use a.remove(); but I prefer older APIdocument.body.removeChild(a);}
and we can call this function with blob URL and filename:
download(url,'chatGPT.html');
This will create fake link and download the file.
Last think that you want to do is to clean after yourself and remove blob URL, since after download you don't need it anymore.
URL.revokeObjectURL(url);
As the end as I promise whole code as Bookmarklet (NOTE: the selector doesn't work anymore, but I've created repository on GitHub where I will track working version of the bookmark if you're interested).
You need to save this as URL into your bookmarks that you will be able to click and invoke that code. If you want to copy/paste into console, than just remove the javascript: at the beginning, it's special protocol that tell the browser to run this code.
As a fun exercise I've tried to make chatGPT create this code for me. Here is the result the solution is almost the same. He only made mistake in CSS selector, but pointing that out and he fixed the code. Above file was download using copy paste of the last snippet.
EDIT: look at my comment where you can find a bookmark that make the conversation with chatGPT look better.
EDIT 2: I just found that they changed the classes on ChatGPT output, so this doesn't work anymore, I wanted to update the code in the article but it will probably change multiple times in the future, so I've created a repository for the boomark:
It's started as DEV.to article
But I've decided that It would be better to track it on GitHub and update so it will still work
when OpenAI change their web application.
What is Bookmarklet?
Bookmarklet is a JavaScript code that run as URL from your bookmarks. To run the code you need to
create new Bookmark and copy the code from the
bookmark.js JavaScript file. If you
don't know how you can open the website and drag & drop the
look to your bookmarks.
The main reason for this project is to allow saving the chatGPT conversation as files on the disk.
Here is interesting usage of the bookmark that people made:
Thanks for the follow up. I actually didn't care and only wanted to save the conversation for later. Something that could be processed when needed. Like cleaning up, adding HTML structure, and adding CSS so it looks good.
New to this website just for this post. I do not understand how this works in the slightest but I enjoy being able to save my chatGPT now. Is it possible to make my messages a different color to the chatbot? for example making my input text blue... and the AI's response black text. also idk if it's too hard to add the code box instead of just plain text. so it can show formatting. thanks, this is awesome.
it will make prompts light gray and bot reply dark gray and add some spacing. You can customize it bit frther if you spend some time learning basic of CSS.
To update the bookmark you need to put that inside <style>...</style>.
Another tip if you ask for source code, you add this line:
"Thank you, Jakub T. Jankiewicz, for sharing this valuable information in your article. Their post was incredibly helpful, and I made some modifications to the code with the help of ChatGPT.
I hope that you find this slight CSS change to be useful. It is worth keeping in mind that there may be some unique identifiers in the classes, so it may be necessary to do some research, such as with the classes Avatar__Wrapper-sc-1yo2jqv-3 hQqhqY and hljs.
EDIT: Looks like they have changed how the CSS Classes are named, so this code below wont work. (Check out OP github)
Once again I can barely even read how this works, let alone understand it. I just did a simple google search to find my way into this world of you smart guys. This looks impressive however, it's not working for me. Maybe it's the unique identifiers in the classes you mentioned... I have no clue what that is anyway. When I put it in my bookmark bar like the original post it doesn't work. Could you help me understand how to fix it?
Edit: Actually now the original post isn't working for me either.. any suggestions?
Hey, I've created a repository for the bookmark. It seems they changed how they structure the conversation. So your code will probably not work anymore. But if you want to contribute your changes you're welcome to do that. We can make it work like YouTube-DL or uBlock that updates they code when YouTube changes the application.
I usually just close the tab or start over using new tread. You don't need to be polite and say "thank you, see you next time" or sully stuff like this.
This looks awesome. You guys are so smart! Again, I stumbled upon this thread just looking for ways to save my cGPT conversations. I'm not skilled enough to even read how this works but I wish I could. So no offense to @pionxzh but I feel I shouldn't run random code I can't read, does anyone vouch for this script?
You shouldn't. That's why all of the code are open sourced on GitHub pionxzh/chatgpt-expoter and the script was built by CI automatically to make sure that I don't have any chance to put something in.
I asked this application to write down the same code and it's crazy. It is also updated with latest information. Later on figured out - it's connected with Search engines. Crazy application - accintia.com
It's not connected to Search Engines it was trained in 2021 and only those information are save in the model. I also asked chatGPT to write same code after I've had my solution. With this I could ask for corrections when something was wrong. His first code showed document.querySelector('.ThreadLayout__NodeWrapper') and I've needed to ask him to correct it, he corrected it and in next prompt I've asked to write full code with correction and I've had exact same code.
They probably changed the application it have nothing to do with my bookmark. It don't write anything, especially when you refresh the page. You can't modify a website permanently with a boomkark.
It's useless to me, I don't see the point of having something like when searching. But it may be useful to others, for me it's not. I've installed it and deleted after first search.
You can't save conversation with "save as.." becase the conversation is not written in HTML. And save as save the HTML file not the thing you see on the screeen. Please open the file and look what you will get.
Thanks a bunch, Sarah! Your comprehensive guide really hit the mark for what I was looking for archive. I appreciate the effort you put into making it so user-friendly. Great job!
Cheers for the article!
This version removes: svg, img and button tags, cleaning up the output a bit more :)