From f2e0b83f961410fa4d286c671a9f07e766cedbeb Mon Sep 17 00:00:00 2001 From: Josh Parker Date: Sat, 8 Jan 2022 22:20:51 -0700 Subject: [PATCH] add option to download goodreads data table as csv file --- goodreads/series-csv.user.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/goodreads/series-csv.user.js b/goodreads/series-csv.user.js index f26bc84..df7a2c7 100644 --- a/goodreads/series-csv.user.js +++ b/goodreads/series-csv.user.js @@ -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; @@ -42,7 +42,7 @@ .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); @@ -50,4 +50,22 @@ 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); }());