Huggle/JS/traffic counter.js

From MediaWiki.org
< Huggle‎ | JS
Jump to navigation Jump to search
// Global vars
var is_visible = false;
var menu_id = null;
var overlay_id = -1;
var timer = null;

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 += "<br>";
    stats += "OUT: " + human_unit(huggle_query.get_all_bytes_sent() + huggle_feed.get_all_bytes_sent());
    return stats;
}

function refresh()
{
    huggle_ui.set_overlay_text(overlay_id, get_stats());
}

function show()
{
    overlay_id = huggle_ui.show_persistent_overlay(get_stats(), 10, 10, 125, 60);
    timer = huggle.create_timer(1000, "refresh");
}

function hide()
{
    huggle_ui.destroy_persistent_overlay(overlay_id);
    huggle.destroy_timer(timer);
}

function toggle()
{
    is_visible = !is_visible;
    if (is_visible)
    {
        show();
    } else
    {
        hide();
    }
    huggle_ui.menu_item_set_checked(menu_id, is_visible);
}

function register_menus()
{
    menu_id = huggle_ui.create_menu_item(huggle_ui_menu_tools, "Traffic counter", "toggle", true);
}

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

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