Skip to content

Instantly share code, notes, and snippets.

@kiittsunne
Forked from danharper/background.js
Last active September 30, 2022 15:20
Show Gist options
  • Save kiittsunne/ff4a5b6faba000fff4fbbd02c4d640d6 to your computer and use it in GitHub Desktop.
Save kiittsunne/ff4a5b6faba000fff4fbbd02c4d640d6 to your computer and use it in GitHub Desktop.
Bare minimum Chrome extension to inject a JS file into the given page when you click on the browser action icon. The script searches DOM nodes on TransitLink's transaction history results page, and totals fares for that period in a window alert popup.

Installation Instructions

  1. Download ZIP and unzip on your computer
  2. Open Chrome and navigate to "Manage Extensions" page (chrome://extensions/)
  3. Toggle On "Developer Mode" in top right hand corner
  4. Click "Load Unpacked" and select the unzipped file OR drag-and-drop file onto page
  5. Optionally: pin the extension to your toolbar for easy access

Toggle on "developer mode"

Usage Instructions

  1. Login to TransitLink's web app on Chrome (https://simplygo.transitlink.com.sg/Account/SignIn)
  2. Select your card and desired transaction period
  3. Once the results are loaded, click on the SimplyGo Pal extension
  4. A alert popup should appear displaying the total amount.

Example Popup

Note!

"Posted" (the weekly aggregate) fare & hidden fares (i.e. the individual fees of a multi-part trip) are not included in the calculation to avoid double count.

// this is the background code...
// listen for our browerAction to be clicked
chrome.action.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.scripting.executeScript(tab.ib, {
target: {tabId: tabId},
files: ['inject.js']
});
});
// this is the code which will be injected into a given page...
(function() {
const nodelist = document.querySelectorAll("td.col3:not(.hiddenRow)");
let amounts = [];
nodelist.forEach((node) => {
if (node.colSpan === 1) {
amounts.push(parseFloat(node.innerHTML.replace("$", "")));
}
});
let total = amounts.reduce((a, b) => a + b, 0).toFixed(2);
// Note: "posted" aggregate trip fares and hidden fares (i.e. breakdown of each trip) are not included.
// Reason is this would cause double count.
alert(`Total: $${total}`);
})();
{
"name": "SimplyGo Pal",
"version": "0.0.2",
"manifest_version": 3,
"description": "Summing trip fare on TransitLink SimplyGo page",
"homepage_url": "http://danharper.me",
"background": {
"service_worker": "background.js"
},
"action": {
"default_title": "simplyGO!"
},
"permissions": ["https://*/*", "http://*/*", "tabs"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment