R34 Auto Max Quality

Auto-select max quality up to limit / Автовыбор макс. качества

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         R34 Auto Max Quality
// @namespace    r34-auto-max-quality
// @version      1.1
// @description  Auto-select max quality up to limit / Автовыбор макс. качества
// @match        https://rule34video.com/video/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=rule34video.com
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    // USER SETTING / НАСТРОЙКА ПОЛЬЗОВАТЕЛЯ
    // 1 = 360p | 2 = 480p | 3 = 720p | 4 = 1080p | 5 = 4k
    const MAX_QUALITY = 4; // change here / менять здесь

    let locked = false;
    let lastUrl = location.href;

    function apply() {
        if (locked) return;

        const list = document.querySelector('.fp-settings-list');
        if (!list) return;

        const best = [...list.querySelectorAll('a[data-format]')]
            .map(a => ({ a, f: +a.dataset.format }))
            .filter(x => x.f <= MAX_QUALITY)
            .sort((a, b) => b.f - a.f)[0];

        if (best && !best.a.parentElement.classList.contains('is-selected')) {
            best.a.click();
        }
    }

    // manual selection → lock / ручной выбор → блокировка
    document.addEventListener('click', e => {
        const a = e.target.closest('.fp-settings-list a[data-format]');
        if (!a) return;

        const f = +a.dataset.format;
        if (f <= MAX_QUALITY) locked = true;
    });

    // video change → unlock / новое видео → сброс
    const obs = new MutationObserver(() => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            locked = false;
            setTimeout(apply, 300);
        }
    });

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

    setTimeout(apply, 500);
})();