Dan D Kim

Let's share stories

Chrome Snippets - Automatically Refresh Data on Questrade

2020-04-13 Dan D. Kimdebugging

TLDR: Copy-paste this snippet into a new Chrome Snippet (DevTools -> Sources -> Snippets), run the snippet (Ctrl + Enter), then open Questrade.


Hello all.

If you are like me and you do some light trading on Questrade, you probably didn’t pay for any add-ons that give you live-time data.

But you might be annoyed having to click that refresh button every time you want to see the real-time stock quotes.

If so, I created a little snippet for myself that clicks the button automatically every second. This script is running as long as the stock market is open, and automatically shuts itself off when the stock market closes.

Snippet: Javascript code that you can save in your browser and run it on any page

So without further ah-dew, here it is:

📋 Steps

  1. Copy the script below
// Store loop id in the global scope
let loopId

function letsMakeMoney () {
  clearInterval(loopId)

  loopId = setInterval(clickRefreshButton, 1000)

  return loopId
}

function clickRefreshButton () {
  const mobileButtonSelector = 'body > app-root > app-layout > app-layout-sidebar > div > div > div > div > div.d-flex.justify-content-center.ng-star-inserted > div.d-flex.justify-content-center.position-fixed.snap-container > div > app-last-updated-time > div > div > button'
  const desktopButtonSelector = 'body > app-root > app-layout > app-layout-sidebar > div > div > div > div.sidebar.ml-40.ml-md-32.ml-lg-40 > div > app-last-updated-time > div > div > button'
  const mobileButton = document.querySelectorAll(mobileButtonSelector)
  const desktopButton = document.querySelectorAll(desktopButtonSelector)
  mobileButton.forEach(click)
  desktopButton.forEach(click)
}

function click (element) {
  if (!element) {
    return
  }

  const clickEvent = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': false
  })

  element.dispatchEvent(clickEvent)

  console.log('refreshed')
}

letsMakeMoney()
  1. Open Chrome DevTools F12
  2. Go to Sources > Snippets in Chrome DevTools.

Snippet in Chrome DevTools

  1. Create a new Snippet.

Create a new Snippet

  1. Paste it in

Pasted code inside snippet

  1. Pess Ctrl + Enter and you’re done!

Console output Chrome console With a Chrome Snippet, the button is refreshed every second

Here’s a GIF:

With a Chrome Snippet, the button is refreshed every second

  1. Don’t forget to Ctrl + S to save the snippet :)

⚠️ Troubleshooting

😢 Button not being refreshed

Chances are that the website is updated and my code is outdated.

But have no fear, you can update the code yourself!

Here’s how.

  1. Right-click on the refresh button > Inspect

Steps to update the code to fix Questrade automatic refresh button script

  1. This will open up the DevTools. You will need to repeat Step 1 to highlight the button in the DOM tree.

Steps to update the code to fix Questrade automatic refresh button script

  1. Find the <button> element. It’s usually right above the highlighted selection, but can be different depending on Questrade website updates.

Steps to update the code to fix Questrade automatic refresh button script

  1. Once you found the <button>, right-click > Copy > Copy selector

Steps to update the code to fix Questrade automatic refresh button script

  1. Now that you have copied the selector, replace the values between the quotes ' with your copied value in the highlighted line shown below:
// Store loop id in the global scope
let loopId

function letsMakeMoney () {
  clearInterval(loopId)

  loopId = setInterval(clickRefreshButton, 1000)

  return loopId
}

function clickRefreshButton () {
  const mobileButtonSelector = 'body > app-root > app-layout > app-layout-sidebar > div > div > div > div > div.d-flex.justify-content-center.ng-star-inserted > div.d-flex.justify-content-center.position-fixed.snap-container > div > app-last-updated-time > div > div > button'
  const desktopButtonSelector = 'body > app-root > app-layout > app-layout-sidebar > div > div > div > div.sidebar.ml-40.ml-md-32.ml-lg-40 > div > app-last-updated-time > div > div > button'  const mobileButton = document.querySelectorAll(mobileButtonSelector)
  const desktopButton = document.querySelectorAll(desktopButtonSelector)
  mobileButton.forEach(click)
  desktopButton.forEach(click)
}

function click (element) {
  if (!element) {
    return
  }

  const clickEvent = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': false
  })

  element.dispatchEvent(clickEvent)

  console.log('refreshed')
}

letsMakeMoney()
  1. Save your code in the Snippet, and re-run it with Ctrl + Enter!

😱 Suddenly stopped working

You will need to re-run the script it everytime you

  • refresh the page
  • close the tab or browser

🌃 (Optional) Ignore if the stock market is closed

For whatever reason, if you don’t want to be running document.querySelectorAll() outside of stock market hours, you can use the following script. The script below is also hosted here

Or do something better, go crazy :)

// Store loop id in the global scope
let loopId

function letsMakeMoney () {
  clearInterval(loopId)

  loopId = setInterval(clickRefreshButton, 1000)

  return loopId
}

function clickRefreshButton () {
  if (isStockMarketOpen()) {
    const mobileButtonSelector = 'body > app-root > app-layout > app-layout-sidebar > div > div > div > div > div.d-flex.justify-content-center.ng-star-inserted > div.d-flex.justify-content-center.position-fixed.snap-container > div > app-last-updated-time > div > div > button'
    const desktopButtonSelector = 'body > app-root > app-layout > app-layout-sidebar > div > div > div > div.sidebar.ml-40.ml-md-32.ml-lg-40 > div > app-last-updated-time > div > div > button'
    const mobileButton = document.querySelectorAll(mobileButtonSelector)
    const desktopButton = document.querySelectorAll(desktopButtonSelector)
    mobileButton.forEach(click)
    desktopButton.forEach(click)
  } else if (isStockMarketClosed()) {
    // No point in refreshing when the stock market is closed
    stopRefreshing()
  } else {
    const message = 'Stock market isn\'t open yet'
    console.log(message)
  }
}

function isStockMarketOpen () {
  let currentTime = new Date()
  currentTime.setMinutes(currentTime.getMinutes() + currentTime.getTimezoneOffset())

  // Based on UTC time, TSX NYX opens at 12:30 and closes at 20:00
  let openingTime = new Date(currentTime)
  openingTime.setHours(13, 30, 0, 0)

  let closingTime = new Date(currentTime)
  closingTime.setHours(20, 0, 0, 0)

  const isWeekday = currentTime.getDay() > 0 && currentTime.getDay() < 6
  const isOpeningHours = currentTime >= openingTime && currentTime <= closingTime

  if (isWeekday && isOpeningHours) {
    return true
  } else {
    return false
  }
}

function isStockMarketClosed () {
  let currentTime = new Date()
  currentTime.setMinutes(currentTime.getMinutes() + currentTime.getTimezoneOffset())

  // Based on UTC time, TSX NYX opens at 12:30 and closes at 20:00
  let closingTime = new Date(currentTime)
  closingTime.setHours(20, 0, 0, 0)

  const isWeekend = [0, 6].includes(currentTime.getDay())
  const isAfterClosingTime = currentTime > closingTime

  if (isWeekend || isAfterClosingTime) {
    // Closed on weekends or after 8 PM UTC
    return true
  } else {
    return false
  }
}

function click (element) {
  if (!element) {
    return
  }

  const clickEvent = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': false
  })

  element.dispatchEvent(clickEvent)

  console.log('refreshed')
}

function stopRefreshing () {
  clearInterval(loopId)
  console.log(`Stock market is currently closed!`)
}

letsMakeMoney()

That’s all. Hope you find this useful. Happy investing! :)