Skip to content

Instantly share code, notes, and snippets.

@dalhundal
Last active August 29, 2015 14:26
Show Gist options
  • Save dalhundal/a6fe9b27182484b7749e to your computer and use it in GitHub Desktop.
Save dalhundal/a6fe9b27182484b7749e to your computer and use it in GitHub Desktop.
Smile.co.uk statements fixer - add balance per row and pending transactions amount.
(function() {
function strToAmount(str) {
var parts = str.trim().match(/^£(\d+\.\d{2})(-)?/);
if (!parts) return 0;
return +(+parts[1] * (parts[2]?-1:1)).toFixed(2);
}
function amountToStr(amount) {
amount = amount.toFixed(2);
if (amount < 0) {
amount = Math.abs(amount)+'-';
};
return '£'+amount;
}
function el(type,attrs,text) {
var _el = document.createElement(type);
if (attrs) for (var a in attrs) {
_el.setAttribute(a,attrs[a]);
};
if (text) _el.appendChild(document.createTextNode(text));
return _el;
};
function getBalance() {
var balanceText = document.querySelector('.recentTransactionsAccountData table tr:nth-child(2) td:nth-child(2)').innerText;
return strToAmount(balanceText);
};
function getAvailable() {
var availableText = document.querySelector('.recentTransactionsAccountData table tr:nth-child(3) td:nth-child(2)').innerText;
return strToAmount(availableText);
};
function getOverdraft() {
var overdraftText = document.querySelector('.recentTransactionsAccountData table tr:nth-child(4) td:nth-child(2)').innerText;
return strToAmount(overdraftText);
};
function insertBalanceHeader() {
document.querySelector('.summaryTable thead tr th:nth-child(2)').width = '40%';
document.querySelector('.summaryTable thead tr').appendChild(el('th',{'class':'summaryHeaderBoldRight',width:'15%'},'balance'));
};
function insertPendingTransactions() {
var pending = +(getBalance()-(getAvailable()-getOverdraft())).toFixed(2);
elRow = el('tr');
elRow.appendChild(el('td',{},'Pending transactions'));
elRow.appendChild(el('td',{'class':'pinkText leftPadded'},'£'+pending.toFixed(2)));
elRow.appendChild(el('td'));
document.querySelector('.recentTransactionsAccountData table tbody').appendChild(elRow);
};
function insertBalanceRows() {
var balance = getBalance();
var rows = document.querySelectorAll('.summaryTable tbody tr:not(:last-child)');
rows = Array.prototype.slice.call(rows,rows);
rows.forEach(function(row) {
var credit = strToAmount(row.children[2].innerText);
var debit = strToAmount(row.children[3].innerText);
row.appendChild(el('td',{class:'summaryDetailR'},amountToStr(balance)));
balance = balance - credit + debit;
});
};
if (document.location.host == "banking.smile.co.uk" && (document.location.pathname=="/SmileWeb2/balances.do" || document.location.pathname=="/SmileWeb2/prepareDomesticRecentItems.do")) {
insertBalanceHeader();
insertBalanceRows();
insertPendingTransactions();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment