ZenMoney Currency Percents

On the ZenMoney service (zenmoney.ru) calculates the total percent each currency concludes and shows you alert if you need to buy or sell a currency. By default works for USD, EUR and RUB, with desired percents 30, 30 and 40 respectively. Change the _DESIRED settings below to your desired values. _TIMEOUT setting is required, because when the page loads, the objects are not ready yet.

当前为 2015-10-13 提交的版本,查看 最新版本

// ==UserScript==
// @name         ZenMoney Currency Percents
// @namespace    [email protected]
// @version      0.1
// @description  On the ZenMoney service (zenmoney.ru) calculates the total percent each currency concludes and shows you alert if you need to buy or sell a currency. By default works for USD, EUR and RUB, with desired percents 30, 30 and 40 respectively. Change the _DESIRED settings below to your desired values. _TIMEOUT setting is required, because when the page loads, the objects are not ready yet.
// @author       [email protected]
// @match        https://zenmoney.ru/a/*
// ==/UserScript==

var 
_TIMEOUT = 2000,
_DESIRED = {
    1: {
        percent: 0.30,
        criticalDiff: 500
    },
    2: {
        percent: 0.4,
        criticalDiff: 25000
    },
    3: {
        percent: 0.30,
        criticalDiff: 500
    }
}
;

function getTotalsByCurrency(profile, desired) {
    var accounts = profile.account;
    var defaultCurrency = profile.user.currency;
    var allCurrencies = profile.instrument;
    var totals = {};
    var currency, balance, key, rate, amount;
    for (var i = 0; i < profile.in_balance.length; i++) {
        key = profile.in_balance[i];
        currency = accounts[key].instrument;
        balance = parseFloat(accounts[key].balance);
        rate = allCurrencies[currency].value * allCurrencies[currency].multiplier;
        if (typeof totals[currency] === 'undefined') {
            totals[currency] = {
                balance: balance,
                inDefaultCurrency: balance * rate,
                rate: rate,
                symbol: allCurrencies[currency].symbol 
            };
        } else {
            totals[currency].balance += balance;
            totals[currency].inDefaultCurrency += balance * rate;
        }
    }
    for (currency in totals) {
        amount = totals[currency];
        amount.percent = Math.round(amount.inDefaultCurrency / profile.balance * 100);
        amount.needToBuy = (profile.balance / amount.rate) * desired[currency].percent - amount.balance;
        // Let us round up the values
        amount.balance = Math.round(amount.balance * 100) / 100;
        amount.inDefaultCurrency = Math.round(amount.inDefaultCurrency * 100) / 100;
        amount.needToBuy = Math.round(amount.needToBuy * 100) / 100;
        amount.alert = (Math.abs(amount.needToBuy) > desired[currency].criticalDiff);
    }
    return totals;
}

function drawTable(totals) {
    var table = '<table id="totals">'
        +'<thead><tr>'
        + '<th class="decimal">Баланс</th>'
        + '<th></th>'
        + '<th></td>'
        + '<th class="decimal">Курс</th>'
        + '<th class="decimal">Надо купить</th>'
        + '</tr></thead>'
        +'<tbody>';
    var amount, row;
    for (var currency in totals) {
        amount = totals[currency];
        row = '<tr>'
                + '<td class="decimal">' + amount.balance + '</td>'
                + '<td>' + amount.symbol + '</td>'
                + '<td>' + amount.percent + '%</td>'
                + '<td class="decimal">' + amount.rate + '</td>'
                + '<td class="decimal' + ((amount.alert) ? ' alert' : ' ') + '">' + amount.needToBuy + '</td>'
                + '</tr>'
            ;
        table += row;
    }
    table += '</tbody></table>';
    return table;
}

$(function () {
    var recalculate = function () {
        var totals = getTotalsByCurrency(zm.profile, _DESIRED), css;
        css = '<style>#totals {float: right} #totals td {padding:2px 5px} #totals td.decimal {text-align: right} #totals td.alert {color: red}</style>';
        $('#header').append(css);
        $('#header').append(drawTable(totals));
    };
    setTimeout(recalculate, _TIMEOUT);
});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址