Skip to content

Instantly share code, notes, and snippets.

@apa6ec
Last active December 24, 2015 12:39
Show Gist options
  • Save apa6ec/6799561 to your computer and use it in GitHub Desktop.
Save apa6ec/6799561 to your computer and use it in GitHub Desktop.
Calculate money on page https://store.steampowered.com/account/ . It is jQuery variant this gist: https://gist.github.com/Sintetic/6797297)
var summ = {
store: {},
gifts: {},
market: {
sell: {},
purchased: {}
}
};
var selector = '.transactions .transactionRow, .hidden_transactions .transactionRow';
//get transaction blocks items
var transactions = {
store: jQuery('#store_transactions div.block').first().find(selector),
gifts: jQuery('#store_transactions div.block').last().find(selector),
market: jQuery('#market_transactions').find(selector)
}
//get only number from price (for example: "0.10" from "$0.10" and "24.04" from "24,04 руб.")
function getPrice(str) {
var rez = '';
for(i=0;i<str.length;i++) {
if (!isNaN(str[i])) { //if numeric
rez = rez.concat(str[i]);
} else if (str[i] === '.') { //if there is a separator
if (!isNaN(str[i-1])) { //if the number before the separator
rez = rez.concat('.');
}
}
}
return rez.replace(' ',''); //removing any spaces
}
function addPrice(item, rate, keyPlace) {
//get item price with currency
var line = jQuery(item).children('.transactionRowPrice').html().replace(',','.');
//get item price (number only)
var price = parseFloat(getPrice(line));
//universal get currency in this item (it's magic!)
var currency = line.replace(getPrice(line),'').replace(/[\d.]/,'');
if (keyPlace === 'market') {
if (!isNaN(price)) { //if price is number
if (typeof(summ[keyPlace][rate][currency]) !== 'undefined') {
summ[keyPlace][rate][currency]+= price;
} else {
summ[keyPlace][rate][currency] = price;
}
}
} else {
if (!isNaN(price)) { //if price is number
if (typeof(summ[keyPlace][currency]) !== 'undefined') {
summ[keyPlace][currency]+= price;
} else {
summ[keyPlace][currency] = price;
}
}
}
}
jQuery.each(transactions, function(keyPlace, place) { //actions in each transaction
jQuery.each(place,function(keyItem, item) { //actions in each item transaction
if (jQuery(item).children('.transactionRowEvent').is('.charge')) { //for all
addPrice(item,'purchased',keyPlace);
}
if (jQuery(item).children('.transactionRowEvent').is('.walletcredit')) { //only for market
if (keyPlace === 'market') {
addPrice(item,'sell',keyPlace);
}
}
});
});
console.log('Store: ',summ.store);
console.log('Gifts: ',summ.gifts);
console.log('Market:');
console.log(' Sell: ',summ.market.sell);
console.log(' Purchased: ',summ.market.purchased);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment