Skip to content

Instantly share code, notes, and snippets.

@cvrebert
Last active August 30, 2024 00:50
Show Gist options
  • Save cvrebert/fe13dcf9697fff037acd6ab7e8cb1041 to your computer and use it in GitHub Desktop.
Save cvrebert/fe13dcf9697fff037acd6ab7e8cb1041 to your computer and use it in GitHub Desktop.
Consumer Reports service reviews table to CSV
// 1. View: Full View
// 2. Scroll to bottom of page until all entries are lazy-loaded.
function downloadCSV(content) {
let filename = 'CR_' + (new Date()).toISOString().replaceAll(/[T:.]/g, '_') + '.csv';
let a = document.createElement('a');
a.download = filename;
a.href = 'data:text/csv,' + content; // TODO: %-encode
a.click();
}
let rowElems = Array.from(document.querySelectorAll('.chart__row__container'));
const TOOLTIP_SEL = '.ratings-attr__tooltip';
let colNames = [
'Name', 'Overall Score',
...Array.from(rowElems[0].querySelectorAll(TOOLTIP_SEL)).map(tooltip => tooltip.dataset.title)
];
let rows = rowElems.map((row) => {
let serviceName = row.querySelector('.list__model').innerText.trim();
let score = row.querySelector('.blob-ratings').dataset.score;
return [
serviceName, score,
...Array.from(row.querySelectorAll(TOOLTIP_SEL)).map(tooltip => tooltip.dataset.score)
];
});
rows.unshift(colNames);
let csv = rows.map(row => {
return row.map(cell => cell.replaceAll(/,|"/g, ' ').replaceAll(/\s+/g, ' ')).join(',');
}).join('\n');
downloadCSV(csv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment