mcosti / Vault balance

// ==UserScript==
// @name         Vault balance
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Calculates your and your spouses vault balance
// @author       Costi Mihai
// @match        https://www.torn.com/properties.php
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    console.log("Vault balance plugin active");
    $( document ).ajaxComplete(function() {
        main();
    });

    function main() {
        createButton();
    }
    
    function createButton() {
    	let buttonExists = $("#custom-balance").length;
    	if(!buttonExists) {
	    	let header = $("#top-page-links-list");
	        header.append('<a role="button" id="custom-balance" aria-labelledby="city" class=" city t-clear h c-pointer  m-icon line-h24 right last">Calculate vault balance</a>');
	        header.click(handleButtonClick);
    	}
    }

    function handleButtonClick() {
    	let onVault = $(".vault-trans-wrap").length;
    	if (!onVault) {
    		alert("Not on vault page");
    		return;
    	}

    	parseTransactions();
    }

    function parseTransactions() {
    	let transactionElements = $('li[transaction_id');
    	let balances = {};

    	var formatter = new Intl.NumberFormat('en-US', {
  			style: 'currency',
 			currency: 'USD',
		});

    	if (!transactionElements.length) {
    		alert("No transactions found on page, maybe wait for the page to fully load");
    	}
    	let lastDate = '';

    	$.each(transactionElements, function (key, transaction){
    		transaction = $(transaction);
    		let user = transaction.find('span[title]').attr('title');
    		let isDeposit = transaction.find('.type').html().trim() == 'Deposit' ? 1 : 0;
    		let amount = transaction.find('.amount').html().match(/(\d+,?)+/)[0];
    		amount = parseInt(amount.replace(/,/g, ''));  // remove commas

    		lastDate = transaction.find('.date').html();

    		if (typeof balances[user] == 'undefined') {
    			balances[user] = {
    				0: [], // withdrawals
    				1: [] // deposits
    			};
    		}
    		balances[user][isDeposit].push(amount);
    	}); // end .each

    	let text = '<div class="vault-custom-balance title m-bottom10 delimiter-999">';
    	let totals = [];
    	for (let [key, value] of Object.entries(balances)) {
    		let totalWidthdrawals = value[0].reduce(function(acc, val) { return acc + val; }, 0);
    		let totalDeposits = value[1].reduce(function(acc, val) { return acc + val; }, 0);
    		let net = totalDeposits - totalWidthdrawals;
    		totals.push(net);
    		text += '<p class="player">' + key + ':</p>';
    		text += '<p class="widthdrawals">Widthdrawals: ' + formatter.format(totalWidthdrawals) + '</p>';
    		text += '<p class="deposits">Deposits: ' + formatter.format(totalDeposits) + '</p>';
    		text += '<p class="net"> <b>Net: ' + formatter.format(net) + '</b></p>';
    		text += '</br>';
    	}
    	let difference = Math.abs(totals[0] - totals[1]);
    	text += '<p class="difference"> <b>Difference: ' + formatter.format(difference) + '</p></b>';
    	text += '<p class="transactions-count">Transaction elements: ' + transactionElements.length + '</p>';
    	text += '<p>Last date: ' + lastDate + '</p>';
    	text += '</div>';
    	
    	let vaultLeft = $('.vault-cont.left').first();
    	vaultLeft.append(text);
    	$('.vault-custom-balance').last().get(0).scrollIntoView();
    }

})();