☰

R34 Thumbnail always active

Обложки видео всегда активны на всех страницах

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu budete musieť nainštalovať rozšírenie, ako je napríklad Tampermonkey alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name         R34 Thumbnail always active
// @namespace    http://tampermonkey.net/
// @version      3.0.1
// @description  Обложки видео всегда активны на всех страницах
// @author       Gemini
// @match        https://rule34video.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=rule34video.com
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // Определение параметров конфигурации
    const DELAY_START = 0;       // мс
    const BACKGROUND_PLAY = 1;   // 1 = играть всегда, 0 = только в зоне видимости
    const FIX_INTERVAL = 3000;   // мс
    const STUCK_THRESHOLD = 5;   // сек без прогресса -> фикс

    const ITEM_SELECTOR = '.item.thumb';
    const WRAP_SELECTOR = '.img.wrap_image';

    // Инициализация структур хранения
    const videoPool = new Map();
    const trackedVideos = new Set();
    const visibilityState = new WeakMap();
    const pendingRoots = new Set();

    let started = false;
    let fixInterval = null;
    let processRafId = null;

    // Внедрение стилей оформления
    const style = document.createElement('style');
    style.textContent = `
        .wrap_image {
            position: relative;
        }
        .wrap_image[data-has-trailer="1"] > img.thumb {
            display: none;
        }
        .wrap_image video[our-trailer] {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            z-index: 1;
            pointer-events: none;
            background: #000;
        }
        .wrap_image > :not(img):not(video) {
            z-index: 2;
        }
    `;
    document.head.appendChild(style);

    // Создание наблюдателя видимости
    const visibilityObserver = BACKGROUND_PLAY === 0 ? new IntersectionObserver((entries) => {
        for (let i = 0; i < entries.length; i++) {
            const entry = entries[i];
            const item = entry.target;
            visibilityState.set(item, entry.isIntersecting);

            const video = item.querySelector('video[our-trailer]');
            if (!video) continue;

            if (entry.isIntersecting) {
                video.play().catch(() => {});
            } else {
                video.pause();
            }
        }
    }, { threshold: 0.15 }) : null;

    // Проверка видимости элемента
    function isInViewport(el) {
        if (!el) return false;
        const rect = el.getBoundingClientRect();
        return rect.top < window.innerHeight && rect.bottom > 0 &&
               rect.left < window.innerWidth && rect.right > 0;
    }

    // Проверка условий воспроизведения
    function shouldPlay(item) {
        if (BACKGROUND_PLAY) return true;
        return visibilityState.get(item) ?? isInViewport(item);
    }

    // Назначение слушателей видео
    function registerVideo(item, video) {
        trackedVideos.add(video);
        video.lastTime = 0;
        video.stuckCheckTime = Date.now();

        video.addEventListener('canplay', () => {
            if (shouldPlay(item)) video.play().catch(() => {});
        }, { once: true });

        video.addEventListener('pause', () => {
            if (shouldPlay(item) && video.isConnected) {
                setTimeout(() => {
                    if (video.paused && video.isConnected) {
                        video.play().catch(() => {});
                    }
                }, 10);
            }
        });

        video.addEventListener('error', () => {
            setTimeout(() => {
                if (!video.isConnected) return;
                video.load();
                if (shouldPlay(item)) video.play().catch(() => {});
            }, 500);
        });
    }

    // Монтирование трейлера обложки
    function activateTrailer(item) {
        const wrap = item.querySelector(WRAP_SELECTOR);
        const img = wrap?.querySelector('img.thumb');
        const previewUrl = wrap?.getAttribute('data-preview');
        if (!wrap || !img || !previewUrl) return;

        item.dataset.trailerHoverBlocked = '1';

        const existingVideos = wrap.querySelectorAll('video');
        for (let i = 0; i < existingVideos.length; i++) {
            const v = existingVideos[i];
            if (!v.hasAttribute('our-trailer')) v.remove();
        }

        let video = wrap.querySelector('video[our-trailer]');
        if (video) {
            wrap.dataset.hasTrailer = '1';
            img.style.display = 'none';
            video.style.display = 'block';
            if (shouldPlay(item) && video.paused) {
                video.play().catch(() => {});
            }
            return;
        }

        // Перемещение видео из пула
        if (videoPool.has(previewUrl)) {
            video = videoPool.get(previewUrl);
            wrap.dataset.hasTrailer = '1';
            img.style.display = 'none';
            video.style.display = 'block';
            wrap.appendChild(video);

            if (shouldPlay(item) && video.paused) {
                video.play().catch(() => {});
            }
            return;
        }

        // Создание элемента видео
        video = document.createElement('video');
        video.setAttribute('our-trailer', '1');
        video.dataset.previewUrl = previewUrl;
        video.src = previewUrl;
        video.loop = true;
        video.muted = true;
        video.playsInline = true;
        video.autoplay = Boolean(shouldPlay(item));
        video.preload = 'auto';

        videoPool.set(previewUrl, video);
        wrap.dataset.hasTrailer = '1';
        img.style.display = 'none';
        wrap.appendChild(video);

        registerVideo(item, video);

        if (visibilityObserver) {
            visibilityObserver.observe(item);
        }

        if (shouldPlay(item)) {
            video.play().catch(() => {});
        }
    }

    // Обработка карточки видео
    function processItem(item) {
        if (!(item instanceof HTMLElement) || !item.matches(ITEM_SELECTOR)) return;

        const wrap = item.querySelector(WRAP_SELECTOR);
        const previewUrl = wrap?.getAttribute('data-preview');
        if (!wrap || !previewUrl) return;

        if (videoPool.has(previewUrl)) {
            wrap.dataset.hasTrailer = '1';
            const img = wrap.querySelector('img.thumb');
            if (img) img.style.display = 'none';
        }

        activateTrailer(item);
        item.dataset.trailerPreviewUrl = previewUrl;
    }

    // Обход контейнеров элементов
    function processRoot(root) {
        if (!root) return;

        if (root instanceof Element && root.matches(ITEM_SELECTOR)) {
            processItem(root);
        }

        if (root.querySelectorAll) {
            const items = root.querySelectorAll(ITEM_SELECTOR);
            for (let i = 0; i < items.length; i++) {
                processItem(items[i]);
            }
        }
    }

    // Очистка очереди контейнеров
    function flushPendingRoots() {
        const roots = Array.from(pendingRoots);
        pendingRoots.clear();
        for (let i = 0; i < roots.length; i++) {
            processRoot(roots[i]);
        }
    }

    // Планирование кадра обработки
    function scheduleProcess(root = document) {
        pendingRoots.add(root);

        // Синхронная обработка целевых контейнеров
        if (root instanceof Element && (root.id === 'custom_list_videos_most_recent_videos_items' || root.querySelector?.(ITEM_SELECTOR))) {
            flushPendingRoots();
            return;
        }

        if (processRafId) return;
        processRafId = requestAnimationFrame(() => {
            processRafId = null;
            flushPendingRoots();
        });
    }

    // Восстановление зависших видеопотоков
    function fixStuckVideos() {
        if (!started) return;

        const now = Date.now();
        trackedVideos.forEach(video => {
            if (!video.isConnected) return;

            const item = video.closest(ITEM_SELECTOR);
            if (!item || (!BACKGROUND_PLAY && !shouldPlay(item))) return;

            if (video.paused) {
                video.play().catch(() => {
                    video.load();
                    video.play().catch(() => {});
                });
                return;
            }

            if (video.currentTime === video.lastTime) {
                if (now - video.stuckCheckTime > STUCK_THRESHOLD * 1000) {
                    video.currentTime = 0;
                    video.stuckCheckTime = now;
                    video.play().catch(() => {
                        video.load();
                        video.play().catch(() => {});
                    });
                }
            } else {
                video.lastTime = video.currentTime;
                video.stuckCheckTime = now;
            }
        });

        // Очистка неактивных элементов пула
        videoPool.forEach((video, url) => {
            if (!video.isConnected) {
                if (!video._disconnectedAt) {
                    video._disconnectedAt = now;
                } else if (now - video._disconnectedAt > 15000) {
                    videoPool.delete(url);
                    trackedVideos.delete(video);
                    video.pause();
                    video.removeAttribute('src');
                    video.load();
                }
            } else {
                video._disconnectedAt = null;
            }
        });
    }

    // Запуск процедур скрипта
    function start() {
        if (started) return;
        started = true;
        scheduleProcess(document);
        fixInterval = setInterval(fixStuckVideos, FIX_INTERVAL);
    }

    setTimeout(start, DELAY_START);

    // Регистрация пользовательских событий запуска
    ['scroll', 'click', 'keydown'].forEach(eventName => {
        document.addEventListener(eventName, start, { once: true, passive: true });
    });

    // Блокировка нативных событий наведения
    ['mouseenter', 'mouseover', 'mouseleave', 'mouseout'].forEach(eventName => {
        document.addEventListener(eventName, (event) => {
            const target = event.target;
            if (target && target !== document && target.closest?.(`${ITEM_SELECTOR}[data-trailer-hover-blocked="1"]`)) {
                event.stopImmediatePropagation();
            }
        }, true);
    });

    // Отслеживание мутаций DOM
    new MutationObserver((mutations) => {
        if (!started) return;

        for (let i = 0; i < mutations.length; i++) {
            const mutation = mutations[i];
            for (let j = 0; j < mutation.addedNodes.length; j++) {
                const node = mutation.addedNodes[j];
                if (node.nodeType === 1) {
                    scheduleProcess(node);
                }
            }
        }
    }).observe(document.body, { childList: true, subtree: true });

    // Обработка отложенной загрузки
    document.addEventListener('lazyloaded', () => {
        if (started) scheduleProcess(document);
    }, true);
})();