function downloadCSV(csv, filename) {
let csvFile;
let downloadLink;
csvFile = new Blob([csv], { type: 'text/csv' });
downloadLink = document.createElement('a');
downloadLink.download = filename;
downloadLink.href = URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
function exportTableToCSV(filename, separator) {
const csv = [];
const rows = document.querySelectorAll("table tr");
for (let i = 0; i < rows.length; i++) {
let row = [];
const cols = rows[i].querySelectorAll("td, th");
for (let j = 0; j < cols.length; j++)
row.push(`"${cols[j].innerText}"`);
csv.push(row.join(separator));
}
downloadCSV(csv.join("\n"), filename);
}
document.querySelector('button').addEventListener('click', () => {
exportTableToCSV('filename.csv', ',');
});