☰

Civitai Card Audio Indicator

Replaces the tip badge with an audio indicator (Cache + Virtual Scroll Proof)

Vous devrez installer une extension telle que Tampermonkey, Greasemonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Userscripts pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey pour installer ce script.

Vous devrez installer une extension de gestionnaire de script utilisateur pour installer ce script.

(J'ai déjà un gestionnaire de scripts utilisateur, laissez-moi l'installer !)

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

(J'ai déjà un gestionnaire de style utilisateur, laissez-moi l'installer!)

// ==UserScript==
// @name         Civitai Card Audio Indicator
// @namespace    http://tampermonkey.net/
// @version      4.0
// @description  Replaces the tip badge with an audio indicator (Cache + Virtual Scroll Proof)
// @author       Your Name Here
// @match        https://civitai.red/*
// @icon         https://civitai.red/favicon.ico
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Global cache to remember the audio status of videos we've already checked
    const audioCache = new Map(); // key: videoId (string), value: hasAudio (boolean)

    // Helper to extract the unique video ID from the card
    function getVideoId(card) {
        const link = card.querySelector('a[href^="/images/"]');
        if (link) {
            const match = link.getAttribute('href').match(/\/images\/(\d+)/);
            if (match) return match[1];
        }
        return null;
    }

    // Helper to style the badge based on state
    function updateBadgeStyle(badge, hasAudio) {
        if (hasAudio) {
            badge.textContent = '🔊 Audio';
            badge.style.backgroundColor = 'rgba(40, 167, 69, 0.2)';
            badge.style.color = '#28a745';
        } else {
            badge.badgeContent = '🔇 No Audio';
            badge.textContent = '🔇 No Audio';
            badge.style.backgroundColor = 'rgba(220, 53, 69, 0.2)';
            badge.style.color = '#dc3545';
        }
    }

    function scanForCards() {
        const videos = document.querySelectorAll('video');

        videos.forEach(video => {
            const card = video.closest('.flex-col.border');
            if (!card) return;

            const videoId = getVideoId(card);
            if (!videoId) return;

            const boltIcon = card.querySelector('svg.tabler-icon-bolt');
            if (!boltIcon) return;

            const tipButton = boltIcon.closest('button');
            const tipContainer = tipButton?.parentElement;
            if (!tipContainer) return;

            // VIRTUAL SCROLL RECYCLE CHECK
            let audioBadge = tipContainer.querySelector('.civ-audio-badge');

            if (audioBadge) {
                // If the badge exists and matches the current video ID, we are done here.
                if (audioBadge.dataset.videoId === videoId) return;

                // If it exists but the ID doesn't match, React recycled the DOM.
                // Remove the old badge so we can process the new video.
                audioBadge.remove();
            }

            // Hide the tip button safely
            tipButton.style.display = 'none';

            // Create our new badge
            audioBadge = document.createElement('div');
            audioBadge.className = 'civ-audio-badge';
            audioBadge.dataset.videoId = videoId; // Tag it with the ID to track recycled DOMs
            audioBadge.style.cssText = `
                display: flex; align-items: center; justify-content: center;
                padding: 4px 8px; border-radius: 4px; font-size: 12px;
                font-weight: 600; background-color: rgba(128, 128, 128, 0.2);
                color: #aaa; user-select: none;
            `;

            tipContainer.appendChild(audioBadge);

            // CACHE CHECK
            if (audioCache.has(videoId)) {
                updateBadgeStyle(audioBadge, audioCache.get(videoId));
                return; // Instantly load from cache without attaching event listeners
            }

            // Not in cache, start the live check
            audioBadge.textContent = 'Checking...';
            let hasDeterminedAudio = false;

            const finalizeAudio = (hasAudio) => {
                if (hasDeterminedAudio) return;
                hasDeterminedAudio = true;

                // Save to cache for future recycling
                audioCache.set(videoId, hasAudio);
                updateBadgeStyle(audioBadge, hasAudio);

                video.removeEventListener('timeupdate', checkAudio);
                video.removeEventListener('play', checkAudio);
            };

            const checkAudio = () => {
                if (hasDeterminedAudio) return;

                if (video.webkitAudioDecodedByteCount > 0) {
                    finalizeAudio(true);
                } else if (video.currentTime > 0.5) {
                    finalizeAudio(false);
                }
            };

            // If it happens to be playing immediately, check it
            if (video.currentTime > 0) checkAudio();

            video.addEventListener('timeupdate', checkAudio);
            video.addEventListener('play', checkAudio);
        });
    }

    // Interval handles infinite scroll / SPA navigation reliably
    setInterval(scanForCards, 500);
    scanForCards();

})();