YouTube Speed Booster х2 on/off

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

目前為 2024-10-31 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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 });