Fix .dms download issue in FireFox

We are messing about with some ChatGPT API chatbots as part of the AI Detox this year. I modified a little API-based chatbot to make it goofier. Turns out the download content option worked fine in Chrome but downloaded as a .dms file in FireFox.

This works in both browsers and gets the file as the intended .txt file.

function saveTheChat(){
    const chats = document.querySelectorAll('.speech');//get all the instances of the class speech
    let chatHistory = '';
    chats.forEach((chat) => {
        let content = chat.innerHTML;//get the inner content
        if(chat.classList.contains("speech-ai")){
            chatHistory = `\n${chatHistory} \n AI: ${content}\n\n`;//add the AI: to stuff written by the machine and end with two line breaks
        } else {
            chatHistory = `\n${chatHistory} \n Human: ${content}\n\n`;//add Human: to stuff written by the human and end with two line breaks
        }
    })
    const a = document.createElement('a'); // Create "a" element
    const blob = new Blob([chatHistory], {type: 'text/plain'}); // Create a blob (file-like object) the second part sets the mime type
    const url = URL.createObjectURL(blob); // Create an object URL from blob
    a.setAttribute('href', url); // Set "a" element link
    a.setAttribute('download', 'chat-history.txt'); // Set download filename, make it .txt as FireFox wants extra guidance.
    a.click() // Start downloading
}

Leave a Reply