YouTube Speed Booster х2 on/off

Добавляет кнопку для включения/выключения изменения скорости воспроизведения на YouTube

目前为 2024-10-31 提交的版本。查看 最新版本

// ==UserScript==
// @name        YouTube Speed Booster х2 on/off
// @namespace   Violentmonkey Scripts
// @match       *://www.youtube.com/*
// @grant       none
// @version     1.3
// @author      -
// @description Добавляет кнопку для включения/выключения изменения скорости воспроизведения на YouTube
// @license
// @license MIT
// ==/UserScript==

let speedEnabled = localStorage.getItem('speedEnabled') === 'true';
const speedButton = document.createElement('button');

function setPlaybackSpeed() {
  const video = document.querySelector('video');
  if (video && speedEnabled) {
    video.playbackRate = 2.0; // Устанавливаем желаемую скорость воспроизведения
  }
}

function toggleSpeed() {
  speedEnabled = !speedEnabled;
  localStorage.setItem('speedEnabled', speedEnabled);
  speedButton.textContent = speedEnabled ? 'Скорость x2 ВКЛ' : 'Скорость x2 ВЫКЛ';

  if (speedEnabled) {
    setPlaybackSpeed(); // Устанавливаем скорость воспроизведения, если включено
  }
}

// Создаем кнопку
speedButton.textContent = speedEnabled ? 'Скорость х2 ВКЛ' : 'Скорость х2 ВЫКЛ';
speedButton.title = 'Расширение запускается при загрузке видео. Обновите страницу для получения результата.';
speedButton.style.position = 'fixed';
speedButton.style.top = '12px'; // Настройте позицию по необходимости
speedButton.style.right = '240px'; // Настройте позицию по необходимости
speedButton.style.zIndex = '9999';
speedButton.style.padding = '10px 10px';
speedButton.style.backgroundColor = '#ff0000';
speedButton.style.color = '#ffffff';
speedButton.style.border = 'none';
speedButton.style.borderRadius = '5px';
speedButton.style.cursor = 'pointer';

// Добавляем кнопку в YouTube
document.body.appendChild(speedButton);
speedButton.addEventListener('click', toggleSpeed);

window.addEventListener('load', () => {
  if (speedEnabled) {
    setPlaybackSpeed();
  }
});

document.addEventListener('fullscreenchange', () => {
  if (document.fullscreenElement) {
    speedButton.style.display = 'none'; // Скрываем кнопку в полноэкранном режиме
  } else {
    speedButton.style.display = 'block'; // Показываем кнопку при выходе из полноэкранного режима
  }
});

const observer = new MutationObserver(setPlaybackSpeed);
observer.observe(document.body, { childList: true, subtree: true });