Rule34Video 2160p y 1080p Filter

Filtra videos 2160p y 1080p en Rule34Video, ocultando los que no coinciden con la calidad seleccionada.

// ==UserScript==
// @name         Rule34Video 2160p y 1080p Filter
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  Filtra videos 2160p y 1080p en Rule34Video, ocultando los que no coinciden con la calidad seleccionada.
// @author       Luis123456xp
// @license      MIT
// @match        https://rule34video.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @run-at       document-end
// @connect      sleazyfork.org
// ==/UserScript==

(function($) {
    'use strict';

    const currentVersion = '2.1';
    const scriptUrl = 'https://sleazyfork.org/es/scripts/496806-rule34video-2160p-y-1080p-filter/code';

    // Función para comparar versiones
    function compareVersions(v1, v2) {
        const v1parts = v1.split('.').map(Number);
        const v2parts = v2.split('.').map(Number);
        for (let i = 0; i < Math.max(v1parts.length, v2parts.length); i++) {
            if ((v1parts[i] || 0) > (v2parts[i] || 0)) return 1;
            if ((v1parts[i] || 0) < (v2parts[i] || 0)) return -1;
        }
        return 0;
    }

    // Verificar si hay una nueva versión disponible
    GM_xmlhttpRequest({
        method: 'GET',
        url: scriptUrl,
        onload: function(response) {
            const match = response.responseText.match(/@version\s+(\d+\.\d+)/);
            if (match) {
                const latestVersion = match[1];
                if (compareVersions(latestVersion, currentVersion) > 0) {
                    $('body').prepend(`
                        <div id="updateNotification" style="position:fixed; top:0; left:0; width:100%; background-color:yellow; z-index:9999; text-align:center; padding:10px;">
                            ${isEnglish ? 'A new version of the script is available. --Rule34Video 2160p y 1080p Filter--' : 'Hay una nueva versión disponible del script. --Rule34Video 2160p y 1080p Filter--'} <a href="${scriptUrl}" target="_blank">${isEnglish ? 'Click here to update' : 'Haz clic aquí para actualizar'}.</a>
                        </div>
                    `);
                }
            }
        }
    });

    const MAX_CONCURRENT_REQUESTS = 10;
    const userLang = navigator.language || navigator.userLanguage;
    const isEnglish = userLang.startsWith('en');

    // Agrega los botones en la esquina superior derecha
    var button2160p = $(`<button id="search2160pButton" style="position:fixed; top:10px; right:10px; z-index:9999;">${isEnglish ? 'Filter 2160p' : 'Filtrar 2160p'}</button>`);
    var button1080p = $(`<button id="search1080pButton" style="position:fixed; top:50px; right:10px; z-index:9999;">${isEnglish ? 'Filter 1080p' : 'Filtrar 1080p'}</button>`);
    $('body').append(button2160p);
    $('body').append(button1080p);

    var currentQuality = '';

    $('#search2160pButton').on('click', function() {
        currentQuality = '2160p';
        console.log(isEnglish ? 'Filtering 2160p videos' : 'Filtrando videos de 2160p');
        filterVideos(currentQuality);
    });

    $('#search1080pButton').on('click', function() {
        currentQuality = '1080p';
        console.log(isEnglish ? 'Filtering 1080p videos' : 'Filtrando videos de 1080p');
        filterVideos(currentQuality);
    });

    async function filterVideos(quality) {
        console.log(`${isEnglish ? 'Starting video filter with quality:' : 'Iniciando filtrado de videos con calidad:'} ${quality}`);
        const videoLinks = $('a[href*="/video/"]').map(function() {
            return $(this).attr('href');
        }).get();
        console.log(`${isEnglish ? 'Total videos found:' : 'Total de videos encontrados:'} ${videoLinks.length}`);
        $('body').append(`<div id="loadingMessage" style="position:fixed; top:50%; left:50%; transform:translate(-50%, -50%); z-index:9999; background-color: yellow; padding: 10px; font-size: 20px;">${isEnglish ? 'Filtering videos...' : 'Filtrando videos...'}</div>`);

        let matchCount = 0;
        let promises = [];

        for (let i = 0; i < videoLinks.length; i++) {
            promises.push(processVideoLink(videoLinks[i], quality));

            if (promises.length >= MAX_CONCURRENT_REQUESTS || i === videoLinks.length - 1) {
                await Promise.all(promises);
                promises = [];
            }
        }

        $('#loadingMessage').text(`${isEnglish ? 'Filtering complete. Videos matching' : 'Filtrado completado. Videos que coinciden con'} ${currentQuality}: ${matchCount}`);
        setTimeout(() => {
            $('#loadingMessage').remove();
        }, 3000);
        console.log(isEnglish ? 'Filtering complete.' : 'Filtrado completado.');

        async function processVideoLink(link, quality) {
            return new Promise(resolve => {
                GM_xmlhttpRequest({
                    method: 'GET',
                    url: link,
                    onload: function(response) {
                        const includesQuality = response.responseText.includes(quality);
                        console.log(`Video ${link} ${isEnglish ? 'contains' : 'contiene'} ${quality}: ${includesQuality}`);
                        if (!includesQuality) {
                            const videoElement = $(document).find(`a[href='${link}']`).closest('.item.thumb');
                            if (videoElement.length > 0) {
                                videoElement.hide();
                                console.log(`${isEnglish ? 'Hiding video:' : 'Ocultando video:'} ${link}`);
                            } else {
                                console.log(`${isEnglish ? 'Container not found for video:' : 'No se encontró el contenedor para el video:'} ${link}`);
                            }
                        } else {
                            matchCount++;
                        }
                        resolve();
                    }
                });
            });
        }
    }
})(window.jQuery);