Skip to content

Instantly share code, notes, and snippets.

@Aupajo
Last active December 7, 2016 00:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aupajo/f350576dd307bc0835258d8a56e2e5f2 to your computer and use it in GitHub Desktop.
Save Aupajo/f350576dd307bc0835258d8a56e2e5f2 to your computer and use it in GitHub Desktop.

Problem

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.

Solution

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.

  1. Log into BNZ
  2. Download one statement so you can enter your 2FA NetGuard
  3. 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment