neemanjabuge / Donesi.com orders price list

// ==UserScript==
// @name       Donesi.com orders price list
// @version    0.1
// @description  enter something useful
// @match      http://www.donesi.com/novisad/lat/group*
// @copyright  2012+, Me
// ==/UserScript==

var ordersDom = $('.order');

if (ordersDom.length == 0) {
	console.log('No orders');
    return;
}

var	orders = ordersDom.html().split('<hr>'),
    users = {}, 
    order,
    userAndPrice;

for (var i = 0, len = orders.length - 2; i < len; i++) {
    userAndPrice = getUserAndPrice(orders[i]);
    addUser(userAndPrice[0], userAndPrice[1]);
}

var total = 0;
var orderText = '';
for (var user in users) {
    orderText += user + ' ' + users[user] + '\n';
    total += users[user];
}
console.log(orderText);
console.log(total);

function addUser (name, price) {
    if (!users[name]) {
        var exists = false;
        for (var user in users) {
            if (user.indexOf(name) > -1 || name.indexOf(user) > -1) {
                name = user;
                exists = true;
                break;
            }
        }
        if (!exists)
            users[name] = 0;
    }
    users[name] = users[name] + parseInt(price.split(' ')[1]);
}

function getUserAndPrice (order) {   
    var orderSplit = order.split(new RegExp('\n')),
    	userAndPrice = orderSplit[orderSplit.length - 1],
    	userAndPriceSplit = userAndPrice.split('<br>');
    if (userAndPriceSplit.length == 2) {
        return [userAndPriceSplit[0], userAndPriceSplit[1]];
    } else {
        return ['moje', userAndPriceSplit[0]];
    }
    
}