R34 Auto Max Quality

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

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 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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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         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);
})();