Greasy Fork is available in English.

FinecoBank.com Inbox: Segna tutti i messaggi come letti e Cancella tutti i messaggi

Questo script aggiunge due bottoni nella pagina "Inbox" di FinecoBank.com che consentono di segnare tutti i messaggi come letti e di cancellare tutti i messaggi.

// ==UserScript==
// @name           FinecoBank.com Inbox: Mark all messages as read and Delete all messages
// @name:it        FinecoBank.com Inbox: Segna tutti i messaggi come letti e Cancella tutti i messaggi
// @description    This script adds two buttons in the page "Inbox" of FinecoBank.com that allow to mark all messages as read and to delete all messages.
// @description:it Questo script aggiunge due bottoni nella pagina "Inbox" di FinecoBank.com che consentono di segnare tutti i messaggi come letti e di cancellare tutti i messaggi.
// @match          https://finecobank.com/pvt/myfineco/mailbox*
// @grant          none
//// @run-at         document-start
// @version        1.0.4
// @author         Cyrano68
// @license        MIT
// @namespace https://greasyfork.org/users/788550
// ==/UserScript==

(function()
{
    "use strict";

    function console_log(text)
    {
        //let now = new Date().toISOString();
        let now = new Date().toLocaleString();
        console.log(`${now} ${text}`);
    }

    function setInterval2(callback, interval_ms)
    {
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: setInterval2 - STARTING TIMER - interval_ms=${interval_ms}`);
        let timerId = setInterval(callback, interval_ms);
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: setInterval2 - TIMER STARTED - timerId=${timerId}`);
        callback(timerId);
        return timerId;
    }

    console_log("==> FinecoBank_com_Inbox_DeleteAll: HELLO! Loading script...");

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

    function onDOMContentLoaded()
    {
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: onDOMContentLoaded - document.readyState=${document.readyState}`);

        let myCSS = document.createElement("style");

        // SEE: https://getcssscan.com/css-buttons-examples
        myCSS.textContent = `
            .button-3 {
                appearance: none;
                background-color: #2ea44f;
                border: 1px solid rgba(27, 31, 35, .15);
                border-radius: 6px;
                box-shadow: rgba(27, 31, 35, .1) 0 1px 0;
                box-sizing: border-box;
                color: #fff;
                cursor: pointer;
                display: inline-block;
                font-family: -apple-system,system-ui,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";
                font-size: 14px;
                font-weight: 600;
                line-height: 20px;
                padding: 6px 16px;
                position: relative;
                text-align: center;
                text-decoration: none;
                user-select: none;
                -webkit-user-select: none;
                touch-action: manipulation;
                vertical-align: middle;
                white-space: nowrap;
            }
            .button-3:focus:not(:focus-visible):not(.focus-visible) {
                box-shadow: none;
                outline: none;
            }
            .button-3:hover {
                background-color: #2c974b;
            }
            .button-3:focus {
                box-shadow: rgba(46, 164, 79, .4) 0 0 0 3px;
                outline: none;
            }
            .button-3:disabled {
                background-color: #94d3a2;
                border-color: rgba(27, 31, 35, .1);
                color: rgba(255, 255, 255, .8);
                cursor: default;
            }
            .button-3:active {
                background-color: #298e46;
                box-shadow: rgba(20, 70, 32, .2) 0 1px 0 inset;
            }
        `;

        document.body.appendChild(myCSS);
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: onDOMContentLoaded - myCSS.outerHTML='${myCSS.outerHTML}'`);
    }

    function onWindowLoaded()
    {
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: onWindowLoaded - document.readyState=${document.readyState}`);
        addMyButtons();
    }

    function addMyButtons()
    {
        console_log("==> FinecoBank_com_Inbox_DeleteAll: addMyButtons");

        let interval_ms = 250;
        let timerId = setInterval2((inputTimerId) =>
        {
            let effectiveTimerId = (inputTimerId === undefined) ? timerId : inputTimerId;
            console_log(`==> FinecoBank_com_Inbox_DeleteAll: addMyButtons - inputTimerId=${inputTimerId}, effectiveTimerId=${effectiveTimerId}`);

            let divInbox = document.querySelector("div#inbox-client-app");
            console_log(`==> FinecoBank_com_Inbox_DeleteAll: addMyButtons - divInbox=${divInbox}`);
            if (divInbox !== null)
            {
                console_log("==> FinecoBank_com_Inbox_DeleteAll: addMyButtons - data READY");

                clearInterval(effectiveTimerId);
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: addMyButtons - TIMER STOPPED - effectiveTimerId=${effectiveTimerId}`);

                // Create a new button that will allow to mark all messages as read.
                let myButton1 = Object.assign(document.createElement("button"), {id: "myButton1", textContent: "MARK ALL AS READ", className: "button-3", style: "margin-right: 5px"});
                myButton1.addEventListener("click", markAllAsRead);
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: addMyButtons - myButton1.outerHTML='${myButton1.outerHTML}'`);

                // The button is placed before the "divInbox".
                divInbox.before(myButton1);

                // Create a new button that will allow to delete all messages.
                let myButton2 = Object.assign(document.createElement("button"), {id: "myButton2", textContent: "DELETE ALL", className: "button-3", style: "margin-left: 5px"});
                myButton2.addEventListener("click", deleteAll);
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: addMyButtons - myButton2.outerHTML='${myButton2.outerHTML}'`);

                // The button is placed before the "divInbox".
                divInbox.before(myButton2);

                console_log(`==> FinecoBank_com_Inbox_DeleteAll: addMyButtons - DONE`);
            }
            else
            {
                console_log("==> FinecoBank_com_Inbox_DeleteAll: addMyButtons - data NOT READY... wait");
            }
        }, interval_ms);
    }

    async function markAllAsRead()
    {
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: markAllAsRead`);
        let counter = 0;
        while (true)
        {
            let divReadMessages = document.querySelectorAll("div#inbox-client-app div.messageRow.read.row");
            console_log(`==> FinecoBank_com_Inbox_DeleteAll: markAllAsRead - counter=${counter} - divReadMessages.length=${divReadMessages.length}`);

            let divUnreadMessages = document.querySelectorAll("div#inbox-client-app div.messageRow.messageunread.row");
            console_log(`==> FinecoBank_com_Inbox_DeleteAll: markAllAsRead - counter=${counter} - divUnreadMessages.length=${divUnreadMessages.length}`);
            if (divUnreadMessages.length == 0)
            {
                break;
            }

            let divUnreadMessage = divUnreadMessages[0];
            console_log(`==> FinecoBank_com_Inbox_DeleteAll: markAllAsRead - counter=${counter} - divUnreadMessage.outerHTML=${divUnreadMessage.outerHTML}`);

            let divButton = divUnreadMessage.querySelector("div[role=\"button\"]");
            console_log(`==> FinecoBank_com_Inbox_DeleteAll: markAllAsRead - counter=${counter} - divButton=${divButton}`);
            if (divButton !== null)
            {
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: markAllAsRead - counter=${counter} - divButton.outerHTML='${divButton.outerHTML}'`);
                await openCloseMessagePage(divButton);
                await messageListReady();
            }

            counter++;
        }
    }

    async function openCloseMessagePage(divButton)
    {
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: openCloseMessagePage`);
        divButton.click();  // Open the message-page.

        let promise = new Promise((resolve, reject) =>
        {
            let interval_ms = 250;
            let timerId = setInterval2((inputTimerId) =>
            {
                let effectiveTimerId = (inputTimerId === undefined) ? timerId : inputTimerId;
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: openCloseMessagePage - inputTimerId=${inputTimerId}, effectiveTimerId=${effectiveTimerId}`);

                let divMsgNavigator = document.querySelector("div#msg-navigator");
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: openCloseMessagePage - divMsgNavigator=${divMsgNavigator}`);
                if (divMsgNavigator !== null)
                {
                    let pathX = divMsgNavigator.querySelector("path[data-name|='Icons / Close / Solid']");
                    console_log(`==> FinecoBank_com_Inbox_DeleteAll: openCloseMessagePage - pathX=${pathX}`);
                    if (pathX !== null)
                    {
                        let buttonX = pathX.closest("button.btn.btn-secondary");
                        console_log(`==> FinecoBank_com_Inbox_DeleteAll: openCloseMessagePage - buttonX='${buttonX}'`);
                        if (buttonX !== null)
                        {
                            clearInterval(effectiveTimerId);
                            console_log(`==> FinecoBank_com_Inbox_DeleteAll: openCloseMessagePage - TIMER STOPPED - effectiveTimerId=${effectiveTimerId}`);

                            console_log(`==> FinecoBank_com_Inbox_DeleteAll: openCloseMessagePage - buttonX.outerHTML='${buttonX.outerHTML}'`);
                            buttonX.click();  // Close the message-page.
                            console_log(`==> FinecoBank_com_Inbox_DeleteAll: openCloseMessagePage - RESOLVE`);
                            resolve();
                        }
                    }
                }
            }, interval_ms);
        });

        return promise;
    }

    async function messageListReady()
    {
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: messageListReady`);
        let promise = new Promise((resolve, reject) =>
        {
            let interval_ms = 250;
            let timerId = setInterval2((inputTimerId) =>
            {
                let effectiveTimerId = (inputTimerId === undefined) ? timerId : inputTimerId;
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: messageListReady - inputTimerId=${inputTimerId}, effectiveTimerId=${effectiveTimerId}`);

                let divReadMessages = document.querySelectorAll("div#inbox-client-app div.messageRow.read.row");
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: messageListReady - divReadMessages.length=${divReadMessages.length}`);

                let divUnreadMessages = document.querySelectorAll("div#inbox-client-app div.messageRow.messageunread.row");
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: messageListReady - divUnreadMessages.length=${divUnreadMessages.length}`);

                if ((divReadMessages.length + divUnreadMessages.length) > 0)
                {
                    clearInterval(effectiveTimerId);
                    console_log(`==> FinecoBank_com_Inbox_DeleteAll: messageListReady - TIMER STOPPED - effectiveTimerId=${effectiveTimerId}`);

                    console_log(`==> FinecoBank_com_Inbox_DeleteAll: messageListReady - RESOLVE`);
                    resolve();
                }
            }, interval_ms);
        });

        return promise;
    }

    async function deleteAll()
    {
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll`);
        let counter = 0;
        while (true)
        {
            let divReadMessages = document.querySelectorAll("div#inbox-client-app div.messageRow.read.row");
            console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll - counter=${counter} - divReadMessages.length=${divReadMessages.length}`);

            let divUnreadMessages = document.querySelectorAll("div#inbox-client-app div.messageRow.messageunread.row");
            console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll - counter=${counter} - divUnreadMessages.length=${divUnreadMessages.length}`);
            if ((divReadMessages.length == 0) && (divUnreadMessages.length == 0))
            {
                break;
            }

            if (divReadMessages.length > 0)
            {
                let divReadMessage = divReadMessages[0];
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll - counter=${counter} - divReadMessage.outerHTML=${divReadMessage.outerHTML}`);

                let divButton = divReadMessage.querySelector("div[role=\"button\"]");
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll - counter=${counter} - divButton=${divButton}`);
                if (divButton !== null)
                {
                    console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll - counter=${counter} - divButton.outerHTML='${divButton.outerHTML}'`);
                    await openDeleteMessagePage(divButton);
                    if ((divReadMessages.length + divUnreadMessages.length) > 1)
                    {
                        await messageListReady();
                    }
                }

                counter++;
            }
            else if (divUnreadMessages.length > 0)
            {
                let divUnreadMessage = divUnreadMessages[0];
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll - counter=${counter} - divUnreadMessage.outerHTML=${divUnreadMessage.outerHTML}`);

                let divButton = divUnreadMessage.querySelector("div[role=\"button\"]");
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll - counter=${counter} - divButton=${divButton}`);
                if (divButton !== null)
                {
                    console_log(`==> FinecoBank_com_Inbox_DeleteAll: deleteAll - counter=${counter} - divButton.outerHTML='${divButton.outerHTML}'`);
                    await openDeleteMessagePage(divButton);
                    if ((divReadMessages.length + divUnreadMessages.length) > 1)
                    {
                        await messageListReady();
                    }
                }

                counter++;
            }
        }
    }

    async function openDeleteMessagePage(divButton)
    {
        console_log(`==> FinecoBank_com_Inbox_DeleteAll: openDeleteMessagePage`);
        divButton.click();  // Open the message-page.

        let promise = new Promise((resolve, reject) =>
        {
            let interval_ms = 250;
            let timerId = setInterval2((inputTimerId) =>
            {
                let effectiveTimerId = (inputTimerId === undefined) ? timerId : inputTimerId;
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: openDeleteMessagePage - inputTimerId=${inputTimerId}, effectiveTimerId=${effectiveTimerId}`);

                let divMsgNavigator = document.querySelector("div#msg-navigator");
                console_log(`==> FinecoBank_com_Inbox_DeleteAll: openDeleteMessagePage - divMsgNavigator=${divMsgNavigator}`);
                if (divMsgNavigator !== null)
                {
                    let buttonTrash = document.querySelector("button.btn-trash.btn.btn-secondary");
                    console_log(`==> FinecoBank_com_Inbox_DeleteAll: openDeleteMessagePage - buttonTrash='${buttonTrash}'`);
                    if (buttonTrash !== null)
                    {
                        clearInterval(effectiveTimerId);
                        console_log(`==> FinecoBank_com_Inbox_DeleteAll: openDeleteMessagePage - TIMER STOPPED - effectiveTimerId=${effectiveTimerId}`);

                        console_log(`==> FinecoBank_com_Inbox_DeleteAll: openDeleteMessagePage - buttonTrash.outerHTML='${buttonTrash.outerHTML}'`);
                        buttonTrash.click();  // Delete the message-page.
                        console_log(`==> FinecoBank_com_Inbox_DeleteAll: openDeleteMessagePage - RESOLVE`);
                        resolve();
                    }
                }
            }, interval_ms);
        });

        return promise;
    }

    console_log("==> FinecoBank_com_Inbox_DeleteAll: Script loaded");
})();