function account_total_spent() {
// adds a "total spent on Steam" to the account details page
storage.get(function(settings) { // check chrome's storage to see if the user has set the option related to showing the total spent
if (settings.showtotal === undefined) { settings.showtotal = true; storage.set({'showtotal': settings.showtotal}); } // if the user hasn't picked either way, assume they want to see the total spent
if (settings.showtotal) { // if the user wants to see the total spent
if ($('.transactionRow').length !== 0) { // if the user has bought something
var currency_symbol = $(".accountBalance").html(); // figure out which currency the user's account balance is in
currency_symbol = currency_symbol.match(/(?:R\$|\$|€|£|pуб)/)[0]; // figure out currency continued
totaler = function (p, i) { // this is a function we'll use later to sum up prices
if (p.innerHTML.indexOf("class=\"transactionRowEvent walletcredit\">") < 0) { // given a particular transaction
var priceContainer = $(p).find(".transactionRowPrice"); // find out much we spent on this transaction
if (priceContainer.length > 0) { // if we spent something
var priceText = $(priceContainer).text(); // extract the price
var regex = /(\d+[.,]\d\d+)/, // make sure the price is in the appropriate format
price = regex.exec(priceText); // extract the price numerically
if (price !== null && price !== "Total") { // verify we're not getting junk data
var tempprice = price[0].toString(); // convert back, forcing it to be a string
tempprice = tempprice.replace(",", "."); // switch , for . to deal with european comma price nonsense
return parseFloat(tempprice); // convert it into a numerical value and return
}
}
}
};
game_prices = jQuery.map($('#store_transactions .transactionRow'), totaler); // sum up all game prices
ingame_prices = jQuery.map($('#ingame_transactions .transactionRow'), totaler); // sum up all IAP prices
market_prices = jQuery.map($('#market_transactions .transactionRow'), totaler); // sum up all market prices
var game_total = 0.0; // start our total game prices at 0
var ingame_total = 0.0; // start our total IAP prices at 0
var market_total = 0.0; // start our total marketplace prices at 0
jQuery.map(game_prices, function (p, i) { game_total += p; }); // take the prices we got above and add them into our running total one by one
jQuery.map(ingame_prices, function (p, i) { ingame_total += p; }); // ditto for IAP
jQuery.map(market_prices, function (p, i) { market_total += p; }); // ditto for marketplace
total_total = game_total + ingame_total + market_total; // your grand total is the sum of your subtotals
if (currency_symbol) { // make sure we've got a reference currency
switch (currency_symbol) { // which currency do we have?
case "€": // euro
game_total = formatMoney(parseFloat(game_total), 2, currency_symbol, ".", ",", true) // format total game spend in the right currency with the comma nonsense
ingame_total = formatMoney(parseFloat(ingame_total), 2, currency_symbol, ".", ",", true) // format IAP spend in the right currency with the comma nonsense
market_total = formatMoney(parseFloat(market_total), 2, currency_symbol, ".", ",", true) // format market spend in the right currency with the comma nonsense
total_total = formatMoney(parseFloat(total_total), 2, currency_symbol, ".", ",", true) // format grand total with the comma nonsense
break;
case "pуб": // ditto for rubles
currency_symbol = " " + currency_symbol;
game_total = formatMoney(parseFloat(game_total), 2, currency_symbol, ".", ",", true)
ingame_total = formatMoney(parseFloat(ingame_total), 2, currency_symbol, ".", ",", true)
market_total = formatMoney(parseFloat(market_total), 2, currency_symbol, ".", ",", true)
total_total = formatMoney(parseFloat(total_total), 2, currency_symbol, ".", ",", true)
break;
default: // everything else uses periods because we're god fearing americans U S A U S A
game_total = formatMoney(parseFloat(game_total), 2, currency_symbol, ",", ".", false)
ingame_total = formatMoney(parseFloat(ingame_total), 2, currency_symbol, ",", ".", false)
market_total = formatMoney(parseFloat(market_total), 2, currency_symbol, ",", ".", false)
total_total = formatMoney(parseFloat(total_total), 2, currency_symbol, ",", ".", false)
break;
}
var html = '<div class="accountRow accountBalance accountSpent">'; // add the code to display your total to the page
html += '<div class="accountData price">' + game_total + '</div>'; // game total
html += '<div class="accountLabel">' + localized_strings[language].store_transactions + ':</div></div>'; // putting the words Store Transactions in the right language
html += '<div class="accountRow accountBalance accountSpent">'; // add code to display iap total
html += '<div class="accountData price">' + ingame_total + '</div>'; // iap total
html += '<div class="accountLabel">' + localized_strings[language].game_transactions + ':</div></div>'; // putting words Game Transactions in the right language
html += '<div class="accountRow accountBalance accountSpent">'; // add code to display marketplace total
html += '<div class="accountData price">' + market_total + '</div>'; // marketplace total
html += '<div class="accountLabel">' + localized_strings[language].market_transactions + ':</div></div>'; // putting words Market Transactions in the right language
html += '<div class="inner_rule"></div>'; // horizontal line between the above and the below
html += '<div class="accountRow accountBalance accountSpent">'; // grand total
html += '<div class="accountData price">' + total_total + '</div>'; // grand total
html += '<div class="accountLabel">' + localized_strings[language].total_spent + ':</div></div>'; // words total spent in right language
html += '<div class="inner_rule"></div>'; // horizontal line
$('.accountInfoBlock .block_content_inner .accountBalance').before(html); // stick the code we just generated in the right place so it appears where we want on the page
}
}
}
});
}
function formatMoney (number, places, symbol, thousand, decimal, right) { // format money in the right format
places = !isNaN(places = Math.abs(places)) ? places : 2; // if we didn't specify a number of decimal places, it's two
symbol = symbol !== undefined ? symbol : "$"; // if we didn't specify a currency symbol, guess $
thousand = thousand || ","; // if we didn't specify a thousands unit separator, guess ,
decimal = decimal || "."; // if we didn't specify a decimal unit separator, guess .
var negative = number < 0 ? "-" : "", // if the number is negative, mark it with a negative
i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "", // converts number to a positive version of itself no more than ten decimal places
j = (j = i.length) > 3 ? j % 3 : 0; // figure out how many digits there are used for inserting the , into the right place in the number
if (right) { // all remaining code inserts comma into the right places and returns the final number with the currency symbol and stuff
return negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) + symbol: "");
} else {
return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
}
};