Gamer_Hardox / OGame : Available Fields

// ==UserScript==
// @name OGame : Available Fields
// @description  Displays in the small list of planets the number of fields that are available.
// @version      1.90
// @author       Hardox - jun'21
// @copyright    2020
// @license      MIT
// @icon         https://gf1.geo.gfsrv.net/cdnc2/67c1f8f4d2ba0951521c3a7cc6c41c.png
// @homepageURL  https://openuserjs.org/scripts/Gamer_Hardox/
// @updateURL    https://openuserjs.org/meta/Gamer_Hardox/OGame_Available_Fields.meta.js
// @downloadURL  https://openuserjs.org/install/Gamer_Hardox/OGame_Available_Fields.user.js
// @include      https://*.ogame.gameforge.com/game/index.php?page=*
// @run-at       document-end
// @grant        GM_addStyle
//
// ==/UserScript==

/*
   Known issues:
   If Hide Planet / Moon Tooltip is on, Infocompte7 stops working because it looks for data in the "title" tags. Infocompte7 should take the information from a more stable source.
   This script saves the title in the data-title tag
*/

(function () {

     // ================ SETTING =================
     var red_level = 3;       /* set the value to your desire */
     var yellow_level = 6;    /* set the value to your desire */
     var view_avail = true;   /* true/false to see the available / used */
     var hide_tooltip = true; /* true/false to hide the planets and moons tooltips */

     // ================ SCRIPT ==================

     GM_addStyle('#rechts #myPlanets div.smallplanet a.moonlink {width: 48px; height: 52px; left: 100px !important; top: 0 !important;}');
     GM_addStyle('#rechts #myPlanets div.smallplanet a.moonlink .icon-moon {width: 20px; height: 20px; position: relative; left: 0; top: 10px;}');
     GM_addStyle('#planetbarcomponent #rechts #myPlanets .smallplanet a.moonlink {left: 0px; top: 20px;}');

     $('.smallplanet').each(function( index ) {
        var fields = 0;
        var moon_text = "";
        var moon_flag = false;
        var availableString = "";
        var base = $(this).html();
        var name = $(this).find(".planet-name").text();
        if (base.search("km") == -1) { return index; }
        if (base.search('moonlink active') !== -1) {
            name = base.split('title="<b>')[2].split(" [")[0]; // gets moon name
            fields = (base.split("km (")[2]).split(")")[0];
            moon_text = "  ";
            moon_flag = true;
        }
        else { fields = (base.split("km (")[1]).split(")")[0]; }
        if (fields.search("overmark") != -1) { fields = fields.replace("<span class='overmark' >", "").replace("</span>","");}
        var used = fields.split("/")[0];
        var maxim = fields.split("/")[1];
        var available = maxim - used;
        switch (true) {
            case (available <= red_level):
                availableString = '<font style="color: DeepPink; font-size: 12px;';
                break;
            case (available > yellow_level):
                availableString= '<font style=\"color: lime; font-size: 11px;';
                break;
            default:
                availableString = '<font style=\"color: orange; font-size: 11px;';
        }
        if (view_avail == false) { available = used };
        availableString = availableString + 'position: relative; top: -1px;">' + moon_text + available + "/" + maxim +'</font> ';
        var newText = moon_flag ? name + availableString : availableString + name;
        $(this).find(".planet-name").html( newText );
    });

    if (hide_tooltip == true ) {
        $('.smallplanet a').each(function(){
            let t = $(this).attr('title');
            $(this).attr('data-title', t);
            $(this).attr('title', "");
        });
    }

})();