Milva / OGMilva

// ==UserScript==
// @name         OGMilva
// @namespace    https://openuserjs.org/users/Milva
// @version      2.0.9
// @description  OGMilva script for OGame
// @author       nullNaN
// @license      MIT
// @copyright    2019, nullNaN
// @match        https://*.ogame.gameforge.com/game/*
// @updateURL    https://openuserjs.org/meta/Milva/OGMilva.meta.js
// @grant        GM_addStyle
// @run-at       document-start
// ==/UserScript==

let redirect = localStorage.getItem('ogl-milva-redirect');
if(redirect && redirect.indexOf('https') > -1)
{
    localStorage.setItem('ogl-milva-redirect', false);
    window.location.href = redirect;
}

class OGMilva
{
    constructor()
    {
        this.rawURL = new URL(window.location.href);
        this.page = this.rawURL.searchParams.get('component') || this.rawURL.searchParams.get('page');

        this.start();
    }

    start()
    {
        // ================ Calcul des dates de fin ================
        if(this.page == 'supplies' || this.page == 'overview')
        {
            let tempsRestant = document.querySelector('div#productionboxbuildingcomponent #buildingCountdown');

            if(tempsRestant){
                let tempsRestantText = tempsRestant.innerText;
                let dateFin = this.getDateFin(tempsRestantText);

                let div = document.querySelector('div#productionboxbuildingcomponent table.construction .ogl-dateFin') || document.querySelector('div#productionboxbuildingcomponent table.construction').appendChild(this.createDOM('div', {'class':'ogl-dateFin'}));
                div.textContent = 'Fin : ' + dateFin;
            }
        }
        if(this.page == 'research' || this.page == 'overview')
        {
            let tempsRestant = document.querySelector('div#productionboxresearchcomponent #researchCountdown');

            if(tempsRestant){
                let tempsRestantText = tempsRestant.innerText;
                let dateFin = this.getDateFin(tempsRestantText);

                let div = document.querySelector('div#productionboxresearchcomponent table.construction .ogl-dateFin') || document.querySelector('div#productionboxresearchcomponent table.construction').appendChild(this.createDOM('div', {'class':'ogl-dateFin'}));
                div.textContent = 'Fin : ' + dateFin;
            }
        }
        if(this.page == 'supplies' || this.page == 'shipyard' || this.page == 'defenses' || this.page == 'overview')
        {
            let tempsRestant = document.querySelector('div#productionboxshipyardcomponent #shipyardCountdown2');

            if(tempsRestant){
                let tempsRestantText = tempsRestant.innerText;
                let dateFin = this.getDateFin(tempsRestantText);

                let div = document.querySelector('div#productionboxshipyardcomponent table.construction .ogl-dateFin') || document.querySelector('div#productionboxshipyardcomponent table.construction').appendChild(this.createDOM('div', {'class':'ogl-dateFin'}));
                div.textContent = 'Fin : ' + dateFin;
            }
        }

        // ================ Nombre de foreuses constructibles par rapport a l'energie ================
        if(this.page == 'shipyard'){

            let energieDispo = document.querySelector('#resources_energy').innerText;
            energieDispo = energieDispo.split('.').join('');
            let nbCrawler = Math.floor(energieDispo / 50);

            let div = document.querySelector('#technologies_civil .ogl-nbCrawler') || document.querySelector('#technologies_civil').appendChild(this.createDOM('div', {'class':'ogl-nbCrawler'}));
            div.textContent = 'Nb crawler a construire : ' + nbCrawler;
        }

    }

    getDateFin(tempsRestant){
        const options = { day: 'numeric', month: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit' };

        const split = tempsRestant.split(' ');

        let dateFin = new Date();

        let secondsToAdd = 0;
        let minsToAdd = 0;
        let hoursToAdd = 0;
        let daysToAdd = 0;
        let weeksToAdd = 0;

        let index = 0;
        split.forEach(item =>
        {
            let dernierCaractere = item.substring(item.length - 1, item.length);

            switch (dernierCaractere) {
              case 's':
                    if(index == split.length-1){
                        secondsToAdd = item.substring(0, item.length - 1).trim();
                    }
                    else{
                        weeksToAdd = item.substring(0, item.length - 1).trim();
                    }
                break;
              case 'j':
                daysToAdd = item.substring(0, item.length - 1).trim();
                break;
              case 'h':
                hoursToAdd = item.substring(0, item.length - 1).trim();
                break;
              case 'm':
                minsToAdd = item.substring(0, item.length - 1).trim();
                break;
            }
            index++;
        }
        );
        
        dateFin.setTime(dateFin.getTime() + (weeksToAdd*7*24*60*60*1000) + (daysToAdd*24*60*60*1000) + (hoursToAdd*60*60*1000) + (minsToAdd*60*1000) + (secondsToAdd*1000));

        return dateFin.toLocaleDateString('fr-FR', options);
    }

    createDOM(element, options, content)
    {
        let e = document.createElement(element);

        for(var key in options)
        {
            e.setAttribute(key, options[key]);
        }

        if(content || content == 0) e.innerHTML = content;

        return e;
    }

    addStyle(option, style)
    {
        if(option != 'disabled')
        {
            GM_addStyle(style);
        }
    }

}

window.addEventListener ("DOMContentLoaded", () =>
{
    let oglight = new OGMilva();
});

GM_addStyle(`
.ogl-dateFin
{
    bottom:44px;
    color:#edff54;
    font-size:10px;
    position:absolute;
    right:20px;
}

.ogl-nbCrawler
{
    bottom:10px;
    color:#edff54;
    font-size:10px;
    position:absolute;
    right:20px;
}

.ogl-nbShipMax
{
    bottom:10px;
    color:#edff54;
    font-size:10px;
    position:absolute;
    right:20px;
}
`);