Skip to content

Commit

Permalink
add option to save table as csv
Browse files Browse the repository at this point in the history
  • Loading branch information
joshparkerj committed Jan 8, 2022
1 parent 5e17fae commit cbfc4cf
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion worldometer/covid-table.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// @match https://www.worldometers.info/coronavirus/usa/*
// @match https://www.worldometers.info/coronavirus/country/*
// @icon https://www.google.com/s2/favicons?domain=worldometers.info
// @grant none
// @grant GM_download
// ==/UserScript==

(function worldometerCovidTable() {
Expand Down Expand Up @@ -65,5 +65,33 @@
document.styleSheets[0].insertRule('#covid-table th, #covid-table td { border: solid 0.2px darkgray; }');
document.styleSheets[0].insertRule('#covid-table th { position: sticky; top: -1px; background: lightblue; padding: 0.4rem 1rem; }');
document.styleSheets[0].insertRule('#covid-table td { padding: 0.2rem 1rem; }');

const saveButton = document.createElement('button');
saveButton.innerText = 'Save table as CSV';

saveButton.addEventListener('click', () => {
const csv = `${dataColumns.map(({ name }) => name).join(',')}\n${dates.map((date, i) => `${date},${dataColumns.map(({ data }) => data[i]).join(',')}`).join('\n')}`;

const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);

GM_download({
url,
name: 'covid-table.csv',
saveAs: true,
onerror: ({ error, details }) => {
console.log(error);
console.log(details);
if (error === 'not_whitelisted') {
const alert = document.createElement('div');
alert.innerText = 'You have to have .csv in your whitelisted extensions. Got to tampermonkey settings and make sure settings mode is beginner or advanced, not novice. Look for Downloads BETA.';
alert.style.setProperty('color', 'red');
saveButton.appendChild(alert);
}
},
});
});

document.querySelector('body').appendChild(saveButton);
});
}());

0 comments on commit cbfc4cf

Please sign in to comment.