This bookmarklet extracts data from worlometers country pages into a downloadable csv file.
To install it:
- If your bookmarks bar is hidden, display it: Ctrl + Shift + b.
- Copy the following code into your clipboard. You'll need it in a sec.
javascript:(function(){function dump_data(){return{x:Highcharts.charts[0].series[0].data.map(d=>d.category),y:Highcharts.charts.map(c=>{return{name:c.renderTo.id,y:c.series[0].data.map(d=>d.y)}})}}
function data2csv(data){var csv='Date,'+data.y.map(y=>y.name).join(',')+'\n';var i;for(i=0;i<data.x.length;i++){csv+=data.x[i]+','+data.y.map(y=>y.y[i]).join(',')+'\n'}
return csv}
function download(filename,text){var element=document.createElement('a');element.setAttribute('href','data:text/plain;charset=utf-8,'+encodeURIComponent(text));element.setAttribute('download',filename);element.style.display='none';document.body.appendChild(element);element.click();document.body.removeChild(element)}
download('data.csv',data2csv(dump_data()))}())
- Right click the bookmarks bar and select "Add Page".
- Name it whatever you like.
- Replace the URL field with the code you copied in step (2).
The bookmarklet is installed.
To use it:
- Go to a Countries page on Worldometers: US
- Click the bookmarklet.
- It should trigger a download for a CSV file that contains the extracted data.
Here is the un-minified code if you want to see what's going on.
(function() {
function dump_data() {
return {
x: Highcharts.charts[0].series[0].data.map(d => d.category),
y: Highcharts.charts.map(c => {return {name: c.renderTo.id, y: c.series[0].data.map(d=>d.y)}})
};
}
function data2csv(data) {
var csv = 'Date,' + data.y.map(y => y.name).join(',') + '\n';
var i;
for (i=0; i<data.x.length; i++) {
csv += data.x[i] + ',' + data.y.map(y => y.y[i]).join(',') + '\n';
}
return csv;
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
download('data.csv', data2csv(dump_data()))
}())
This probably only works for country pages, but should be easy to modify to extract data from any worldometers graph.