-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommbank-credit-card-usage.user.js
157 lines (132 loc) · 5.49 KB
/
commbank-credit-card-usage.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// ==UserScript==
// @name CommBank - Credit Card Usage
// @namespace https://github.com/MattFaz/Userscripts
// @match https://www1.my.commbank.com.au/netbank/TransactionHistory/History.aspx*
// @match https://www2.my.commbank.com.au/netbank/TransactionHistory/History.aspx*
// @grant none
// @version 1.1
// @author https://github.com/MattFaz
// @description Displays a custom box with Credit Card Balance (including pending).
// @downloadURL https://github.com/MattFaz/Userscripts/raw/refs/heads/main/commbank-credit-card-usage.user.js
// @updateURL https://github.com/MattFaz/Userscripts/raw/refs/heads/main/commbank-credit-card-usage.user.js
// ==/UserScript==
(function () {
"use strict";
// Function to extract number from currency string
function extractNumber(str) {
if (!str) {
// console.log('No string provided to extractNumber');
return 0;
}
// console.log('Processing string:', str);
// Handle the + sign and currency format
let cleaned = str
.replace(/[+$,]/g, "") // Remove +, $, and commas
.trim(); // Remove any whitespace
// console.log('Cleaned string:', cleaned);
return parseFloat(cleaned) || 0;
}
// Function to format number as currency
function formatCurrency(number) {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(number);
}
// Function to calculate and display the difference
function calculateUsage() {
// Remove any existing display boxes
const existingBox = document.getElementById("credit-usage-display");
if (existingBox) {
existingBox.remove();
}
// Get credit limit
const creditLimitElement = document.querySelector(
".liCardLimit .currencyUICredit span"
);
// console.log('Credit limit element found:', creditLimitElement);
// Get available amount - get the parent element to include the decimal part
const availableElement = document.querySelector(
"#ctl00_BodyPlaceHolder_summaryAvailableBalance_CC"
);
// console.log('Available element found:', availableElement);
// Only proceed if both elements are found
if (!creditLimitElement || !availableElement) {
console.log("Required elements not found yet");
return false;
}
// Get the text content carefully
const creditLimitText = creditLimitElement.textContent || "";
const availableText = availableElement.textContent || "";
// console.log('Credit limit text:', creditLimitText);
// console.log('Available text:', availableText);
// Extract numbers
const creditLimit = extractNumber(creditLimitText);
const available = extractNumber(availableText);
// console.log('Parsed credit limit:', creditLimit);
// console.log('Parsed available:', available);
// Calculate the difference
const used = creditLimit - available;
// Create display element
const displayDiv = document.createElement("div");
displayDiv.id = "credit-usage-display";
displayDiv.style.cssText = `
position: fixed;
top: 10px;
right: 10px;
background-color: #f0f0f0;
padding: 10px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
z-index: 9999;
font-family: Arial, sans-serif;
`;
displayDiv.innerHTML = `
<div style="margin-bottom: 5px"><strong>Credit Card Usage:</strong></div>
<div>Credit Limit: ${formatCurrency(creditLimit)}</div>
<div>Available: ${formatCurrency(available)}</div>
<div style="border-top: 1px solid #ccc; margin: 5px 0;"></div>
<div><strong>Currently Used: ${formatCurrency(used)}</strong></div>
`;
// Add to page
document.body.appendChild(displayDiv);
return true;
}
// Function to start observing DOM changes
function startObserving() {
let attempts = 0;
const maxAttempts = 25; // Maximum number of attempts
// console.log('Starting observation of DOM changes...');
// Create an observer instance
const observer = new MutationObserver((mutations, obs) => {
attempts++;
console.log(`Attempt ${attempts} to find elements...`);
if (calculateUsage()) {
// If calculation succeeds, disconnect the observer
// console.log('Elements found and calculation complete!');
obs.disconnect();
return;
}
if (attempts >= maxAttempts) {
// console.log('Maximum attempts reached, stopping observation');
obs.disconnect();
return;
}
});
// Start observing the document with the configured parameters
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Try immediate calculation in case elements are already present
calculateUsage();
}
// Start when the DOM is ready
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", startObserving);
} else {
startObserving();
}
})();