Skip to content

Instantly share code, notes, and snippets.

@duplaja
Created March 24, 2022 14:55
Show Gist options
  • Save duplaja/316539af2cb20956d5e34df14d4ef93b to your computer and use it in GitHub Desktop.
Save duplaja/316539af2cb20956d5e34df14d4ef93b to your computer and use it in GitHub Desktop.
Export Table as List - Text File
function generateDownload(filename, text, type='text/plain') {
// Create an invisible A element
const a = document.createElement('a');
a.style.display = 'none';
document.body.appendChild(a);
// Set the HREF to a Blob representation of the data to be downloaded
a.href = window.URL.createObjectURL(
new Blob([text], { type })
);
// Use download attribute to set set desired file name
a.setAttribute('download', filename);
// Trigger the download by simulating click
a.click();
// Cleanup
window.URL.revokeObjectURL(a.href);
document.body.removeChild(a);
}
function startDownload() {
const collection = document.getElementsByClassName('item-cell');
let origTitle = document.getElementById('list-title').innerText;
let today = new Date().toISOString().replace(/T.*/,'');
let lowerTitle = origTitle.toLowerCase();
let title = lowerTitle.replace(/[^a-z0-9]/gi, '')+'-'+today+'.txt';
var fullList = '';
for (let i = 0; i < collection.length; i++) {
var content = collection[i].innerText;
fullList = fullList+content;
if(i != parseInt(collection.length) - 1) {
fullList=fullList+'\r\n';
}
}
generateDownload(title, fullList);
}
window.addEventListener('load', function () {
var downloadLink = document.getElementById('download-link');
downloadLink.addEventListener('click', function(e) {
startDownload();
e.preventDefault();
});
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment