m0n5tr / MineThings backlight melds

// ==UserScript==
// @name        MineThings backlight melds
// @namespace   zzzz
// @include     http://*.minethings.com/*
// @exclude     http://*.minethings.com/miners/profile*
// @exclude     http://*.minethings.com/*/map/*
// @exclude     http://*.minethings.com/*/login/*
// @version     1.1.3
// @grant GM_deleteValue
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==


var debug_text;
var debug_need = false;

var city ="";
var serv = "";
var do_reload_inventory =false;
var do_print_inventory =false;
var need_select_all = false;
setTimeout(main,1000);



function create_div(){
    text =
        "<div style='font-family: \"Courier New\", Courier, monospace;	font-size: 13px; position: fixed;  top: 5px; right: 5px; background: #fff;'>" +
        "{<input type='checkbox' id='click_check_box' /> select}" +
        "<div name='work_speace'> <ul id='ul_inventory'> </ul> </div>" +
        "<div style='font-family: \"Courier New\", Courier, monospace;	font-size: 13px;' id='debug_text'>"+
        "0"+
        "</div></div>";
    var opts = document.createElement("span");
    opts.innerHTML = text;
    opts.style.zIndex = "50";
    document.body.insertBefore(opts, document.body.firstChild);
}

function debuger(txt){
    if(!debug_need)return;
    var inner = debug_text.innerHTML;
    if(inner.length>1000) inner = inner.substr(50);
    debug_text.innerHTML = inner +"<br/>" +  txt;
}

function delete_from_inventory(){
    debuger("del in " + city);
    var q=0;
    while(GM_getValue(get_id(q), "")!==""){
        GM_deleteValue(get_id(q));
        GM_deleteValue(get_id_q(q));
        q++;
    }
    debuger("delete " + q);
}

function have_in_inventory(txt){
    var q=0;
    while(GM_getValue(get_id(q), "")!=="") {
        if(GM_getValue(get_id(q))==txt) return q;
        q++;
    }
    return -1;
}

function have_city(new_city){
    var q=0;
    while(GM_getValue(get_city_id(q), "")!=="") {
        if(GM_getValue(get_city_id(q), "")==new_city) return q;
        q++;
    }
    debuger("no city=" + new_city);
    return -1;
}

function last_city(){
    var q=0;
    while(GM_getValue(get_city_id(q), "")!=="") {
        q++;
    }
    return q;
}

function add_city() {
    var navcontainer = document.getElementById("navcontainer");
    new_city = navcontainer.getElementsByTagName("img")[0].alt;
    if(have_city(new_city)<0){
        GM_setValue(get_city_id(last_city()), new_city);
        debuger("add city=" + new_city + " id=" + get_city_id(last_city()));
    }
    return new_city;
}

function get_city_id(q) {
    return "city_"+serv+"_" +q;
}
function get_id(q, any_city) {
    return (any_city?any_city:city) + "_" + q;
}
function get_id_q(q, any_city) {
    return (any_city?any_city:city) + "_" + q + "_q";
}

function reload_inventory(ThingsDiv) {
    if(do_reload_inventory) return;
    delete_from_inventory();
    var ash = ThingsDiv.getElementsByTagName("a");
    debuger("find " + ash.length + " in " + city);
    var q=0;
    for(var z=0;z<ash.length;z++){
        var item = ash[z].innerHTML;
        var id = have_in_inventory(item);
        if(id < 0) { id = q; q++;}
        GM_setValue(get_id(id), item);
        GM_setValue(get_id_q(id), GM_getValue(get_id_q(id), 0) + 1);
        debuger(id +"="+ item + " q=" +GM_getValue(get_id_q(id), 0) );
    }
    do_reload_inventory = true;
}

function print_inventory(){
    var ul = document.getElementById('ul_inventory');

    debuger("last_city=" +last_city());
    for(var s=0; s<last_city();s++) {
        var li = document.createElement("li");
        var current =false;
        if(s!=0) {
            li.innerHTML = " ";
            ul.appendChild(li);
            li = document.createElement("li");
        }
        var this_city = GM_getValue(get_city_id(s));
        if(this_city == city) current=true;
        li.innerHTML = "===="+this_city+"====";
        if(current) li.style.fontWeight = "bold";
        li.style.fontSize = "16px";
        ul.appendChild(li);
        var q=0;
        while(GM_getValue(get_id(q, this_city),"")!==""){
            li = document.createElement("li");
            if(current) li.style.fontWeight = "bold";
            if(current) li.style.background = '#df1';

//            if(current) li.style.fontSize = "16px";

            li.innerHTML = GM_getValue(get_id_q(q, this_city), 0) + " - " + GM_getValue(get_id(q, this_city), "");
            ul.appendChild(li);
            q++;
        }
    }
    do_print_inventory = true;
}


// <b style="background: #df1;">Bolt</b>


function select_all(ItemsDiv) {
    if(!need_select_all) return;
    var q=0;
    var txt  = ItemsDiv.innerHTML;
    while(GM_getValue(get_id(q),"")!=="") {             // #df1
        txt =txt.replace(new RegExp(GM_getValue(get_id(q),""), 'g'), "<b style='background: #df1;'>" + GM_getValue(get_id(q),"") + "</b>");
        q++;
    }
    ItemsDiv.innerHTML = txt;
    need_select_all = false;
}

function add_serv() {
//    debuger("url=" + document.URL);
//    url=http://ebeko.minethings.com/miners/list_miners
    var url= document.URL;
    debuger(url);
    url = url.substr(url.indexOf("://")+3, url.indexOf(".") - url.indexOf("://")-3);
    return url;
}

function main(){
    setTimeout(main,900);
    debug_text = document.getElementById("debug_text");
    if(!debug_text){ create_div(); return; }

    if(serv==="") serv = add_serv();
    if(city==="") city = add_city();


    var ThingsDiv = document.getElementById("ThingsDiv");
    if(ThingsDiv) reload_inventory(ThingsDiv);
    if(!do_print_inventory)print_inventory();
    var chb = document.getElementById('click_check_box');
    if(chb.checked){
        need_select_all = true;
        chb.checked = false;
    }
    var ItemsDiv = document.getElementById('ItemsDiv');
    if(ItemsDiv) select_all(ItemsDiv);
}