FinecoBank.com: Refresh periodically the currrent page

This script refreshes periodically the current page of FinecoBank.com in order to avoid (after 4 min.) the warning modal popup and (after 5 min.) the disconnection.

// ==UserScript==
// @name           FinecoBank.com: Refresh periodically the currrent page
// @name:it        FinecoBank.com: Aggiorna periodicamente la pagina corrente
// @description    This script refreshes periodically the current page of FinecoBank.com in order to avoid (after 4 min.) the warning modal popup and (after 5 min.) the disconnection.
// @description:it Questo script aggiorna periodicamente la pagina corrente di FinecoBank.com per evitare che (dopo 4 min.) venga visualizzato il popup modale di avviso e che (dopo 5 min.) venga eseguita la disconnessione.
// @match          https://finecobank.com/*
// @exclude        https://finecobank.com/app/powerbook/*
// @grant          none
//// @run-at         document-start
// @version        1.1.3
// @author         Cyrano68
// @license        MIT
// @namespace https://greasyfork.org/users/788550
// ==/UserScript==

(function()
{
    "use strict";

    function getZeroFilledMillisecs(dateNow)
    {
        const millisecs = dateNow.getMilliseconds();
        return ("00" + millisecs).slice(-3);
    }

    function consoleLog(text)
    {
        const dateNow = new Date();
        //const now = dateNow.toISOString();
        const now = dateNow.toLocaleString() + "." + getZeroFilledMillisecs(dateNow);
        console.log(`${now} ${text}`);
    }

    const myVersion = GM_info.script.version;
    consoleLog(`==> FinecoBank_com_RefreshPage: HELLO! Loading script (version: ${myVersion})...`);

    let currUrl = window.location.href;
    consoleLog(`==> FinecoBank_com_RefreshPage: currUrl='${currUrl}'`);

    document.addEventListener("DOMContentLoaded", onDOMContentLoaded);
    window.addEventListener("load", onWindowLoaded);

    // Sometimes it happens that the pages behave like SPA applications, i.e. they change URL without requiring full page reloads from the server
    // (and without load the proper greasemonkey-javascript script). In order to manage this problem I reduced the value of "interval_ms"
    // and in every timeout I check if the URL changed. If the URL effectively changed then the new page is fully reloaded.
    //

    //const interval_ms = 10000; // 10s
    const interval_ms = 1000; // 1s
    const refreshInterval_ms = (3*60 + 45)*1000; // 3m45s ---> less than 4m, in order to avoid the modal popup.
    const oldUrl = currUrl;

    let timerId = 0;
    let startTime;
    let lastPrintTime;

    function onDOMContentLoaded()
    {
        consoleLog(`==> FinecoBank_com_RefreshPage: onDOMContentLoaded - document.readyState=${document.readyState}`);
        // DO NOTHING!
    }

    function onWindowLoaded()
    {
        consoleLog(`==> FinecoBank_com_RefreshPage: onWindowLoaded - document.readyState=${document.readyState}`);
        // Start a timer that periodically checks if it's time to refresh the current page.
        consoleLog(`==> FinecoBank_com_RefreshPage: onWindowLoaded - STARTING TIMER 'checkRefreshPage' - interval_ms=${interval_ms}`);
        timerId = setInterval(checkRefreshPage, interval_ms);
        consoleLog(`==> FinecoBank_com_RefreshPage: onWindowLoaded - TIMER STARTED - timerId=${timerId}`);
        consoleLog(`==> FinecoBank_com_RefreshPage: onWindowLoaded - wait ${interval_ms/1e3} secs...`);
        startTime = new Date();
        lastPrintTime = startTime;
    }

    function checkRefreshPage()
    {
        currUrl = window.location.href;
        const currTime = new Date();
        const elapsedTime_ms = currTime - startTime;
        const elapsedTime_s = elapsedTime_ms / 1e3;

        if ((elapsedTime_ms >= refreshInterval_ms) || (currUrl !== oldUrl))
        {
            consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - elapsedTime_ms=${elapsedTime_ms} (${elapsedTime_s} secs)`);
            clearInterval(timerId);
            consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - TIMER STOPPED - timerId=${timerId}`);
            timerId = 0;
            consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - REFRESH PAGE - currUrl='${currUrl}', oldUrl=${oldUrl}`);
            window.location.reload();  // Refresh the current page
            oldUrl = currUrl;
        }
        else if (elapsedTime_ms > (refreshInterval_ms - interval_ms))
        {
            consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - elapsedTime_ms=${elapsedTime_ms} (${elapsedTime_s} secs)`);
            clearInterval(timerId);
            consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - TIMER STOPPED - timerId=${timerId}`);
            timerId = 0;
            const shortInterval_ms = refreshInterval_ms - elapsedTime_ms;
            const shortInterval_s = shortInterval_ms / 1e3;
            consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - STARTING TIMER 'checkRefreshPage' - shortInterval_ms=${shortInterval_ms}`);
            timerId = setInterval(checkRefreshPage, shortInterval_ms);
            consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - TIMER STARTED - timerId=${timerId}`);
            consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - wait ${shortInterval_s} secs...`);
        }
        else
        {
            const elapsedPrintTime_ms = currTime - lastPrintTime;
            const elapsedPrintTime_s = elapsedPrintTime_ms / 1e3;

            if (elapsedPrintTime_s >= 10)
            {
                consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - elapsedTime_ms=${elapsedTime_ms} (${elapsedTime_s} secs)`);
                const interval_s = interval_ms / 1e3;
                consoleLog(`==> FinecoBank_com_RefreshPage: checkRefreshPage - wait ${interval_s} secs...`);
                lastPrintTime = currTime;
            }
            // DO NOTHING!
        }
    }

    consoleLog("==> FinecoBank_com_RefreshPage: Script loaded");
})();