Last active
December 4, 2021 21:24
-
-
Save tomhodgins/25cef90bfbbfffe7ff95789bf4d978d0 to your computer and use it in GitHub Desktop.
Paste this function into your JS console on CIBC's online banking website to scrape your account ledger into a CSV formatted file the browser can save that can be imported into Excel
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 cibc2csv() { | |
var table = document.querySelector('table.smart-account') | |
var csv = '' | |
var head = [] | |
var row = [] | |
// read header cells | |
table.querySelectorAll('thead th').forEach(th => { | |
head.push(`${trim(th.textContent)}`) | |
}) | |
// add header cells to CSV | |
csv += `${head.join(', ')}\n` | |
// read body table cells | |
table.querySelectorAll('tbody tr').forEach(tr => { | |
tr.querySelectorAll('td').forEach(td => { | |
row.push(`${trim(td.textContent)}`) | |
}) | |
// add each row's table cells to CSV | |
csv += `${row.join(', ')}\n` | |
row = [] | |
}) | |
function trim(string) { | |
return string | |
.replace(/,/g, '') // remove commas | |
.replace(/^\s+/, '') // remove leading whitespace | |
.replace(/\s+$/, '') // remove following whitespace | |
.replace(/\s+/g, ' ') // collapse adjacent spaces | |
.replace(/[\n\t]/g, ' ') // convert tabs and newlines to spaces | |
.replace(/Free transaction$/, '') // remove specific text | |
.replace(/³$/, '') // remove specific text | |
} | |
function format(string) { | |
return trim(string) | |
.replace(/\s/g, '-') // convert whitespace to dashes | |
.replace(/[\(\)]/g, '') // remove brackets | |
} | |
function download(file) { | |
var link = document.createElement('a') | |
var header = document.querySelector('.search.row header h3').textContent | |
link.download = `cibc-${format(header)}.csv` | |
link.href = `data:text/csv;${file}` | |
return link.dispatchEvent(new MouseEvent('click')) | |
} | |
return download(csv) | |
} | |
// Download CIBC data as CSV file | |
cibc2csv() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment