YouTube Skip Time Customizer

Customize YouTube skip time by arrow key and block Numpad0~Numpad9 function

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Skip Time Customizer
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Customize YouTube skip time by arrow key and block Numpad0~Numpad9 function
// @author       TrainingDummy1
// @match        https://www.youtube.com/watch*
// @match        https://www.youtube.com/shorts*
// @grant        none

// @license MIT
// ==/UserScript==
(function() {
	'use strict';

	// You can change this constant to customize skip time
	const step = 0.8;
	let video;
  let searchCount = 0;
  let searchIndex = setInterval(function() {
    video = document.querySelector('video[src]');
    if(!video) {
      if(searchCount < 10) {
			  console.error(`비디오 요소를 찾을 수 없습니다. searchCount: ${searchCount}`);
      }
      else {
        console.error(`비디오 요소를 찾을 수 없습니다. 기능이 비활성화 됩니다.`);
        clearInterval(searchIndex);
      }
    }
    else {
      clearInterval(searchIndex);
    }
  }, 500)

	document.addEventListener('keydown', function(event) {
		// 입력이 콘텐츠 편집 영역에서 발생하지 않는지 확인
		if(event.target.getAttribute('contenteditable') !== 'true' && event.target.id !== 'contenteditable-root') {

			// ArrowRight 또는 ArrowLeft 키 눌렀을 때
			if(event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
				event.preventDefault();
				event.stopPropagation();

        if(!video || isNaN(video.duration) || isNaN(video.currentTime)) {
          video = document.querySelector('video[src]');
        }

				const direction = event.key === 'ArrowRight' ? 1 : -1;
				const targetTime = Math.max(0, Math.min(video.duration, video.currentTime + direction * step));

				// 정확하게 이동하기 위한 보정 함수
				let retries = 0;
				const maxRetries = 5;
				const tolerance = 0.15;

				function accurateSeek() {
					video.currentTime = targetTime;

					if(Math.abs(video.currentTime - targetTime) < tolerance || retries >= maxRetries) {
						return;
					}

					retries++;
					setTimeout(accurateSeek, 100);
				}

				accurateSeek();
			}
			// Numpad 동작 차단
			else if(event.code.match(/Numpad\d/)) {
				event.preventDefault();
				event.stopPropagation();
			}
		}
	}, true);

	console.log(`화살표 키로 정확하게 ${step}초씩 이동하는 기능이 활성화되었습니다.`);
})();