Last active
January 29, 2022 02:19
-
-
Save cvrebert/6092924132f2416252c436a566879fea to your computer and use it in GitHub Desktop.
Consumer Reports grid to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
// Otherwises causes false-positive for column labeling | |
document.querySelectorAll('.recommended-with-category, .classic-view__printing-description').forEach(el => el.remove()); | |
let rowElems = Array.from(document.querySelectorAll('.classic-view__model-row-content')); | |
let colNames = Array.from( | |
rowElems[0].querySelectorAll('.classic-view__body__item .shared-crux-label:not(.fraction)') | |
).filter(colLabelElem => !colLabelElem.closest('.classic-view__overall-score') | |
).map(colLabelElem => colLabelElem.textContent.trim() | |
); | |
colNames.unshift("CR Price"); | |
colNames.unshift("Product"); | |
let rows = rowElems.map((row) => { | |
let productName = row.querySelector('.crux-product-title').textContent.trim(); | |
let price = row.querySelector('.classic-view__price>span').replaceAll(/\s/g, ''); | |
// Yes, some elements have non-unique IDs. Life is terrible. | |
let colElems = Array.from(row.querySelectorAll('.classic-view__body__item:not([id="0-description"])')); | |
let colValues = colElems.map((col) => { | |
let scoreElem = col.querySelector('.crux-numbers'); | |
if (scoreElem) { | |
return scoreElem.firstChild.data.trim(); | |
} | |
let prose = col.querySelector('.classic-view__text-cell'); | |
if (prose) { | |
return prose.textContent.trim(); | |
} | |
return "<unknown>"; | |
}); | |
colValues.unshift(price); | |
colValues.unshift(productName); | |
return colValues; | |
}); | |
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