Created
July 7, 2021 13:55
-
-
Save idomitori/7c0fc38e79c23ed0751971e7e9fee5c6 to your computer and use it in GitHub Desktop.
Parse simracing.gp results table into CSV file
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
(() => { | |
function parseTable(tableSelector = 'table') { | |
const tableElement = document.querySelector(tableSelector); | |
const rows = tableElement.querySelectorAll(`tr`); | |
const table = Array.from(rows).map((row) => { | |
const cols = row.querySelectorAll('td'); | |
return Array.from(cols).map((col) => col.innerText); | |
}); | |
return table; | |
} | |
function createCSVDataUrl(content) { | |
return `data:text/csv;charset=utf-8,${encodeURIComponent(content)}`; | |
} | |
function downloadDataUrl(data, filename) { | |
const link = document.createElement('a'); | |
link.setAttribute('href', data); | |
link.setAttribute('target', '_blank'); | |
link.setAttribute('download', filename); | |
link.click(); | |
} | |
function serializeTableAsCSV(table, separator = ',') { | |
return table.map((row) => row.join(separator)).join('\n'); | |
} | |
function convertCellToTableDurationFormat(text) { | |
return text.replace(/:(?=\d\d\d)/, '.'); | |
} | |
const table = parseTable().map((row) => | |
row.map(convertCellToTableDurationFormat) | |
); | |
const csvDataUrl = createCSVDataUrl(serializeTableAsCSV(table)); | |
downloadDataUrl(csvDataUrl, 'sgpTable.csv'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment