Fedaykin / Phalanx time stamp

// ==UserScript==
// @name        Phalanx time stamp
// @author      Fedaykin
// @description Phalanx time stamp
// @include *ogame.gameforge.com/game/index.php?page=ingame&component=galaxy*
// @include *ogame.gameforge.com/game/index.php?component=galaxy*
// @version 1.4.1
// @require     https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @license MIT
// @updateURL https://openuserjs.org/meta/Fedaykin/Phalanx_time_stamp.meta.js
// @downloadURL https://openuserjs.org/install/Fedaykin/Phalanx_time_stamp.user.js
// ==/UserScript==
"use strict";
(function () {
    let g = 0;
    let s = 0;
    let p = 0;
    let past = 0;
    let difference;
    let localPast;

    function formatTime(time) {
        time = parseInt(time);
        let date = new Date(time * 1000);
        //"[d].[m].[Y] [H]:[i]:[s]";
        return date.getDate() + "." + (date.getMonth() + 1) + "." + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
    }

    //Server time is calculated by taking local time and adding the timezone offset(its a negative number in + timezones)
    function formatServer(time) {
        time = parseInt(time);
        let date = new Date(time * 1000);
        return formatTime(time + date.getTimezoneOffset() * 60)
    }

    function clock() {
        let present = new Date();
        let time = Math.round(((present.getTime() / 1000 - localPast) + Number.EPSILON) * 10) / 10;
        let phalanxer_clock = $("#phalanx_clock");
        if (!phalanxer_clock.length) return;
        phalanxer_clock.html(" (+" + time + ")");
        let t = setTimeout(clock, 100);
    }

    unsafeWindow.$(document).ajaxSuccess(function (event, xhr, settings) {
        if (~settings.url.indexOf("phalanx") || ~settings.url.indexOf("phalanxsystemlayer")) {
            let lanx, con, regexp, sclock, lclock, diff, serverTime, coords;
            if (~settings.url.indexOf("phalanxsystemlayer")) {
                lanx = $("#phalanxsystemlayer");
                regexp = /.+&galaxy=(\d)&system=(\d+)/g; //regexp for coords
                coords = regexp.exec(settings.url);
                coords[3] = 0
            } else {
                regexp = /.+&galaxy=(\d)&system=(\d+)&position=(\d+)/g; //regexp for coords
                coords = regexp.exec(settings.url);

                lanx = $("#phalanx");
            }
            difference = 0;
            con = lanx.find('script').text(); //find the script tag
            regexp = /var mytime = (\d+)/g; //find the time inside the script tag
            serverTime = regexp.exec(con)[1];


            //New scan from new coordinates
            if (g === coords[1] && s === coords[2] && p === coords[3] && past !== 0) {
                difference = serverTime - past;
                g = coords[1];
                s = coords[2];
                p = coords[3];
            }

            past = serverTime;
            localPast = (new Date()).getTime() / 1000;
            g = coords[1];
            s = coords[2];
            p = coords[3];
            lclock = $('#phalanx_local'); //local clock
            if (lclock.length) lclock.remove();
            lclock = $('<div />', {
                    "class": 'ct_foot_row textCenter',
                    id: 'phalanx_local',
                    style: "color: #99cc00;",
                    text: formatTime(serverTime)
                }
            );
            sclock = $('#phalanx_server'); //server clock
            if (sclock.length) sclock.remove();
            sclock = $('<div />', {
                    "class": 'ct_foot_row textCenter',
                    id: 'phalanx_server',
                    style: "color: #fd8a1c;",
                    text: formatServer(serverTime)
                }
            );
            diff = $('#phalanx_difference'); //time since last scan
            if (diff.length) diff.remove();
            diff = $('<div />', {
                    "class": 'ct_foot_row textCenter',
                    id: 'phalanx_difference',
                    html: difference + "<span id='phalanx_clock' style='color: red;'></span>" //clock that increases as time goes by
                }
            );

            lanx.parent().prev().after(lclock);
            lanx.parent().prev().after(sclock);
            lanx.parent().prev().after(diff);
            clock();
        }
    });


    $(document).keydown(function (e) {
        if ((e.originalEvent && e.originalEvent.repeat) || e.repeat) return;
        if (e.keyCode === 82) {
            $(".refreshPhalanxLink").children().click();
        }
    });
})();