Auto Sort Purelymail Routing Table by Domain

Automatically sort and group the Purelymail routing table by domain on page load with alternating colors for domain groups

Verzia zo dňa 11.10.2024. Pozri najnovšiu verziu.

// ==UserScript==
// @name         Auto Sort Purelymail Routing Table by Domain
// @namespace    http://tampermonkey.net/
// @version      3.2
// @description  Automatically sort and group the Purelymail routing table by domain on page load with alternating colors for domain groups
// @author       V3ctor Design
// @license      MIT
// @match        https://purelymail.com/manage/routing
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to sort and group the table by domain with alternating colors for groups
    function sortAndGroupTableByDomain() {
        const table = document.querySelector('table.striped'); // Get the table element

        if (!table) {
            console.log("Table not found yet.");
            return false; // Retry if table is not available
        }

        const domainColIndex = 0; // "Domain" is the first column
        const tbody = table.querySelector('tbody'); // Get tbody

        if (!tbody) {
            console.log("Tbody not found.");
            return false;
        }

        const rows = Array.from(tbody.querySelectorAll('tr')); // Get all rows

        if (rows.length === 0) {
            console.log("No rows found.");
            return false;
        }

        // Sort rows by domain
        const sortedRows = rows.sort((rowA, rowB) => {
            const domainA = rowA.querySelectorAll('td')[domainColIndex].querySelector('select').selectedOptions[0].textContent.trim().toLowerCase();
            const domainB = rowB.querySelectorAll('td')[domainColIndex].querySelector('select').selectedOptions[0].textContent.trim().toLowerCase();
            return domainA.localeCompare(domainB); // Sort domains alphabetically
        });

        tbody.innerHTML = ''; // Clear existing rows

        let lastDomain = null;
        let groupToggle = false; // To alternate group colors

        // Append sorted rows with alternating colors for different domain groups
        sortedRows.forEach(row => {
            const currentDomain = row.querySelectorAll('td')[domainColIndex].querySelector('select').selectedOptions[0].textContent.trim().toLowerCase();

            if (lastDomain && lastDomain !== currentDomain) {
                // Switch color for the next group when the domain changes
                groupToggle = !groupToggle;
            }

            // Apply alternating background color based on the groupToggle value
            row.style.backgroundColor = groupToggle ? '#ddd' : '#f9f9f9';

            tbody.appendChild(row); // Append the current row
            lastDomain = currentDomain;
        });

        console.log("Table sorted and grouped by domain with alternating colors.");
        return true;
    }

    // Function to poll for the table to be fully loaded
    function pollForTable() {
        const maxAttempts = 20; // Number of retries (adjust as needed)
        let attempts = 0;

        const interval = setInterval(() => {
            attempts++;
            const sorted = sortAndGroupTableByDomain();

            if (sorted) {
                clearInterval(interval); // Stop polling once sorting is successful
            }

            if (attempts >= maxAttempts) {
                clearInterval(interval); // Stop after max attempts
                console.log("Failed to sort and group the table after maximum attempts.");
            }
        }, 100); // Poll every 100ms
    }

    // Start polling for the table after the page has loaded
    window.addEventListener('load', pollForTable);
})();