Save a Text File with JavaScript
function saveFile(text, name = 'file', mime = 'text/plain') {
const blob = new Blob([
text
], { type: mime });
const a = document.createElement('a')
a.download = name + '.txt'
a.href = window.URL.createObjectURL(blob)
a.click();
}
// hacky way to create the button and have it call `saveFile`
// when clicked
document.body.appendChild(
Object.assign(
document.createElement`button`,
{ innerText: 'click me' })
).onclick = () => saveFile('Hello there')