Skip to content

Instantly share code, notes, and snippets.

@Thisura98
Created October 8, 2023 15:50
Show Gist options
  • Save Thisura98/64c86a6928972a561dd9234fff1a39c3 to your computer and use it in GitHub Desktop.
Save Thisura98/64c86a6928972a561dd9234fff1a39c3 to your computer and use it in GitHub Desktop.
Finds the total GB used for a month in the MySLT Portals' Broadband > Daily Usage tab
/**
* INSTRUCTIONS:
*
* Make sure you are on the Broadband > Daily Usage tab of the MySLT Portal
*
* Right click any where on the web page and click "Inspect".
* The Inspect window will open, go to the "Console" tab.
*
* Select the month you want to the total for in the MySLT Daily Usage tab
* Copy & paste in the following code in the Console tab of the Inspector window.
*
* Press "Enter" on your keyboard and you should get a total of the GB used
* (for example: "33.0 GB")
*/
Array.from(document.querySelectorAll(".table tr"))
.filter((e, i) => i > 0)
.map((e) => {
let gb = e.querySelectorAll("td:nth-of-type(2)")[0].innerText;
return Number.parseFloat(gb.replace("GB", "")); })
.reduce((acc, c) => acc + c)
.toFixed(2) + " GB"
/**
* CODE EXPLANATION (do not copy and paste this part. Only copy the Javascript code above):
*
* We first select all the table row elements from (probably) the only table element
* currently loaded on the web page - that is the table displaying the daily network usage.
*
* We skip the first row which is just the table headers.
*
* For each table row, we find the 2nd column which contains the usage information.
* Since we use querySelectorAll here, it returns an array but we only need the first element
* so we extract is using [0]. Afterwards, we extract the text inside the "td" (table cell).
* Finally, we replace the "GB" portion of that text and convert the number part from
* the String type to the Number type (so we can add them later).
*
* The .reduce() function can be used to do the same operation over all the elements in the array.
* For each callback, it provides the last result and the current item to process (respectively, acc and c).
* Since we want to find the total GB used, we add the last result and current item (both numbers) together.
*
* Finally, we convert the floating point number result to a string with only 2 decimal places and
* add the " GB" text to the end.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment