Huggle/JS/traffic status.js

From MediaWiki.org
< Huggle‎ | JS
Jump to navigation Jump to search
var sb_id;

function ext_is_working()
{
    return true;
}

function human_round(bytes)
{
    return Math.round(bytes * 100) / 100;
}

function human_unit(bytes)
{
    if (bytes < 1024)
        return human_round(bytes) + "B";
    bytes = bytes / 1024;
    if (bytes < 1024)
        return human_round(bytes) + "KB";
    bytes = bytes / 1024;
    return human_round(bytes) + "MB";
}

function get_stats()
{
    var stats = "IN: " + human_unit(huggle_query.get_all_bytes_received() + huggle_feed.get_all_bytes_received());
    stats += " OUT: " + human_unit(huggle_query.get_all_bytes_sent() + huggle_feed.get_all_bytes_sent());
    return stats;
}

function update_main(text)
{
    huggle_ui.set_status_bar_text(sb_id, get_stats());
    return text;
}

function register_status()
{
    sb_id = huggle_ui.create_status_bar_label(get_stats());
}

function ext_init()
{
    if (!huggle.register_hook("statusbar_update", "update_main"))
    {
        return false;
    }
    if (huggle_ui.mainwindow_is_loaded())
    {
        register_status()
    } else if (!huggle.register_hook("main_open", "register_status"))
    {
        return false;
    }
    return true;
}

function ext_get_info()
{
    var info = {};
    info["name"] = "traffic_info";
    info["version"] = "1.1.0";
    info["description"] = "Shows a traffic counter in status bar of HG";
    info["author"] = "Petr Bena";
    info["min_huggle_version"] = "3.4.5";
    info["required_context"] = "huggle_ui";
    info["requires_unsafe"] = false;
    return info;
}