☰

Rule34Video - Automatic Best Quality Selector

Automatically sets the best available quality (4K, 1080p, 720p, or 480p) on Rule34Video.com.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Rule34Video - Automatic Best Quality Selector
// @namespace    http://tampermonkey.net/
// @version      3.1.2
// @description  Automatically sets the best available quality (4K, 1080p, 720p, or 480p) on Rule34Video.com.
// @author       LoopFix
// @match        https://rule34video.com/video/*
// @icon         https://i.ibb.co/RTQLcyFm/favicon-32x32.png
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // --- SCRIPT CONFIGURATION ---
    const LOCAL_STORAGE_KEY = 'kvsplayer_selected_format';
    const QUALITY_ELEMENT_XPATH = '//*[@id="tab_video_info"]/div[5]/div/a[1]';

    // This is our lookup table. It maps the text found on the page
    // to the value the player understands in localStorage.
    const qualityMap = {
        '2160p': '4k',
        '1080p': '1080p',
        '720p':  '720p',
        '480p':  '480p' // Added 480p support
    };

    let hasCompleted = false;

    console.log('[Auto Quality] Script starting, using multi-quality XPath method.');

    function findElementByXPath(xpath) {
        const result = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        return result.singleNodeValue;
    }

    function setBestAvailableQuality() {
        if (hasCompleted) {
            return;
        }

        const highestQualityLink = findElementByXPath(QUALITY_ELEMENT_XPATH);

        if (!highestQualityLink) {
            console.log(`[Auto Quality] XPath element ('${QUALITY_ELEMENT_XPATH}') not found yet. Waiting...`);
            return;
        }

        console.log('[Auto Quality] Found element via XPath!');
        if (observer) {
            observer.disconnect();
            console.log('[Auto Quality] Observer disconnected to save resources.');
        }
        hasCompleted = true;

        const linkText = highestQualityLink.textContent.trim();
        console.log(`[Auto Quality] Highest quality link text is: "${linkText}"`);

        let qualityWasSet = false;

        for (const qualityText in qualityMap) {
            if (linkText.includes(qualityText)) {
                const formatToSet = qualityMap[qualityText];
                localStorage.setItem(LOCAL_STORAGE_KEY, formatToSet);
                console.log(`%c[Auto Quality] SUCCESS: Found "${qualityText}" and set preference to "${formatToSet}".`, 'color: green; font-weight: bold;');
                qualityWasSet = true;
                break;
            }
        }

        if (!qualityWasSet) {
            console.warn(`[Auto Quality] NOTE: Highest quality found ("${linkText}") is not a targeted quality. No preference was set.`);
        }
    }

    const observer = new MutationObserver(() => {
        setBestAvailableQuality();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    setBestAvailableQuality();

})();