Next Spaceflight Countdown

Adds a countdown timer to upcoming space launches on NextSpaceflight.com

// ==UserScript==
// @name         Next Spaceflight Countdown
// @version      1.1
// @description  Adds a countdown timer to upcoming space launches on NextSpaceflight.com
// @match        https://nextspaceflight.com/*
// @grant        none
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

(function() {
    'use strict';

    function updateCountdowns() {
        document.querySelectorAll('.mdl-card').forEach(card => {
            const timeElement = card.querySelector('.mdl-card__supporting-text span[id^="localized"]');
            if (!timeElement) return;

            const launchTime = new Date(timeElement.textContent.trim());
            if (isNaN(launchTime)) return;

            const tagsContainer = card.querySelector('.csscolumns');
            if (tagsContainer) {
                let countdownTag = tagsContainer.querySelector('.countdown-tag');
                if (!countdownTag) {
                    countdownTag = document.createElement('div');
                    countdownTag.className = 'countdown-tag';
                    countdownTag.style.marginTop = '5px';
                    countdownTag.style.color = 'rgb(209, 205, 199)';
                    tagsContainer.appendChild(countdownTag);
                }

                const now = new Date();
                let diff = launchTime - now;
                const prefix = diff < 0 ? 'T+' : 'T-';
                diff = Math.abs(diff);
                
                const days = Math.floor(diff / (1000 * 60 * 60 * 24));
                const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
                const seconds = Math.floor((diff % (1000 * 60)) / 1000);
                
                countdownTag.textContent = `${prefix}${days}d ${hours}h ${minutes}m ${seconds}s`;
            }
        });

        setTimeout(updateCountdowns, 1000 - new Date().getMilliseconds());
    }

    updateCountdowns();
})();