function downloadTextAsFile(text, filename) {
const blob = new Blob([text], {type: 'text/plain;charset=UTF-8'});
const link = document.createElement('a');
link.setAttribute('download', filename);
link.setAttribute('type', 'hidden');
link.setAttribute('href', window.URL.createObjectURL(blob));
document.body.appendChild(link);
link.click();
setTimeout(() => {
window.URL.revokeObjectURL(link.href);
document.body.removeChild(link);
}, 100);
}
downloadTextAsFile('Hello world', 'hellofile.txt');