YT Scroll Rewind

Mouse wheel rewind over YouTube video

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         YT Scroll Rewind
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Mouse wheel rewind over YouTube video
// @author       You
// @match        https://www.youtube.com/watch*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // === Config / Настройки ===
    const config = {
        seekTime: 5,        // Seconds to seek per wheel step / Секунд за один шаг колеса
        deadZonePercent: 20 // Ignore edges (%) / Игнорировать края плеера (%)
    };

    // YouTube player container / Контейнер плеера YouTube
    function getPlayer() {
        return document.querySelector('.html5-video-player');
    }

    // HTML5 <video> element / Сам элемент видео
    function getVideo() {
        return document.querySelector('video');
    }

    // Check cursor inside player rect / Курсор внутри плеера
    function isInside(e, rect) {
        return (
            e.clientX >= rect.left && e.clientX <= rect.right &&
            e.clientY >= rect.top  && e.clientY <= rect.bottom
        );
    }

    // Dead zone near edges (avoid conflicts with UI) /
    // Мёртвая зона по краям (чтобы не мешать интерфейсу)
    function isInDeadZone(e, rect) {
        const dz = config.deadZonePercent / 100;
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;

        return (
            x < rect.width * dz ||
            x > rect.width * (1 - dz) ||
            y < rect.height * dz ||
            y > rect.height * (1 - dz)
        );
    }

    // Main wheel handler / Основная логика прокрутки
    function handleWheel(e) {
        const player = getPlayer();
        const video = getVideo();
        if (!player || !video) return;

        const rect = player.getBoundingClientRect();
        if (!isInside(e, rect)) return;
        if (isInDeadZone(e, rect)) return;

        // Block YouTube default scroll behavior /
        // Блокируем стандартное поведение YouTube
        e.preventDefault();
        e.stopImmediatePropagation();

        // Scroll up = back, down = forward /
        // Вверх — назад, вниз — вперёд
        video.currentTime += e.deltaY < 0 ? -config.seekTime : config.seekTime;

        // Clamp to video duration /
        // Ограничение в пределах видео
        video.currentTime = Math.max(0, Math.min(video.duration, video.currentTime));
    }

    // Capture phase + passive:false is REQUIRED /
    // Захват + passive:false ОБЯЗАТЕЛЬНЫ
    document.addEventListener('wheel', handleWheel, {
        passive: false,
        capture: true
    });

    // Optional external access to config /
    // Необязательный доступ к настройкам извне
    window.ytScrollConfig = config;
})();