Skip to content

Commit

Permalink
add option to download goodreads data table as csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
joshparkerj committed Jan 9, 2022
1 parent 2365777 commit f2e0b83
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions goodreads/series-csv.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
thead.appendChild(headerRow);
table.appendChild(thead);
table.appendChild(tbody);
document.querySelectorAll('.responsiveBook').map((rb) => {
[...document.querySelectorAll('.responsiveBook')].map((rb) => {
const title = rb.querySelector('.gr-h3 span[itemprop="name"]').textContent;
const author = rb.querySelector('span[itemprop="author"]').textContent;
const rating = rb.querySelector('.communityRating__starsWrapper ~ .gr-metaText').textContent;
Expand All @@ -42,12 +42,30 @@
.forEach((row) => {
csv += row;
const bodyRow = document.createElement('tr');
row.split(',').forEach((d) => {
row.split('","').forEach((d) => {
const td = document.createElement('td');
td.textContent = d.replaceAll('"', '');
bodyRow.appendChild(td);
});

tbody.appendChild(bodyRow);
});

document.querySelector('.responsiveMainContentContainer').appendChild(table);

const downloadButton = document.createElement('button');
downloadButton.textContent = 'Save table as csv';
downloadButton.addEventListener('click', () => {
const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${document.title.split('Series')[0].trim().replace(/\s+/g, '-')}-series.csv`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});

document.querySelector('.responsiveMainContentContainer').appendChild(downloadButton);
}());

0 comments on commit f2e0b83

Please sign in to comment.