You have been asked for your last three months' bank statements. Easy enough, right? Except your stupid engineer brain thought it would be a good idea to sign up to BNZ's YouMoney and divide your finances into neat boxes helpfully labeled “Rent”, “Power”, “Whisky”, and so on. Now you have 29 bank accounts, and you need to download 87 different statements.
This is somewhat vexing. The process will take Some Time. So your stupid engineer brain comes up with a new idea (hint: more technology is always the answer) - maybe you can automate this!
You estimate this task will take about an hour or so to do, and maybe about an half an hour to automate.
You consult the chart for good measure.
You decide to ignore the chart and hack together something anyway.
Warning: If you can't read this script and understand exactly what it does – don't use it! Don't paste random bits of code from strangers on the internet into your browser.
The older this document is, the less likely this will work, as it hinges on selectors and elements being in a particular structure. This is liable to change in the future.
- Log into BNZ
- Download one statement so you can enter your 2FA NetGuard
- Pop open Chrome's console and drop in the following:
findSelector = (selector) => {
let POLL_TIMEOUT = 2000
let POLL_FREQUENCY = 100
return new Promise((resolve, reject) => {
let startedAt = new Date()
let pollForSelector = () => {
let now = new Date()
let node = document.querySelector(selector)
if (node) {
resolve(node)
} else if(now - startedAt > POLL_TIMEOUT) {
reject(`Couldn't find ${selector}`)
} else {
setTimeout(() => pollForSelector(), POLL_FREQUENCY)
}
}
pollForSelector()
})
}
processNext = (list) => {
let accountNode = list.shift()
if (!accountNode) {
console.log('Finished!')
return
}
console.log('Fetching', accountNode.querySelector('.account-name').innerText)
accountNode.click()
findSelector('.AccountHeaderBar .js-statements')
.then((statementsButton) => {
statementsButton.click()
findSelector('.Documents-statements .List').then((rows) => {
let rowNodes = Array.prototype.slice.apply(rows.querySelectorAll('a'))
let lastThreeMonths = rowNodes.slice(0, 3)
lastThreeMonths.forEach(link => link.click())
document.querySelector('.Modal-content .Form-footer-actions .js-close').click()
document.querySelector('.js-close-modal-button').click()
processNext(list)
})
})
.catch(error => console.warn(error))
}
accountNodes = Array.prototype.slice.apply(document.querySelectorAll('.account-info'))
processNext(accountNodes)