Video Playback Rate Controller

【使用前先看介绍/有问题可反馈】视频倍速控制器 (Video Playback Rate Controller): 为视频添加倍速控制元素,通过 `Shift + @` 可以随时将控制器显示或隐藏。

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         Video Playback Rate Controller
// @name:cn      视频倍速控制器
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @description  【使用前先看介绍/有问题可反馈】视频倍速控制器 (Video Playback Rate Controller): 为视频添加倍速控制元素,通过 `Shift + @` 可以随时将控制器显示或隐藏。
// @author       cc
// @include      *
// @grant        none
// ==/UserScript==

(function() {
  'use strict';
  var openRateControl = false
  function addRateControl () {
    var video = document.querySelector('video')
    var select = document.createElement('select')
    select.name = 'play-rate'
    for (let rate of [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 8, 16]) {
      let option = document.createElement('option')
      option.value = rate
      option.innerText = option.value + '倍速'
      select.appendChild(option)
    }
    select.onchange = function (event) {
      video.playbackRate = event.target.value
    }
    select.selectedIndex = 2
    var p = document.createElement('p')
    p.id = 'rate-control'
    p.appendChild(select)
    if (video.nextElementSibling) {
      video.parentElement.insertBefore(p, video.nextElementSibling)
    } else {
      video.parentElement.appendChild(p)
    }
  }
  function removeRateControl () {
    var p = document.getElementById('rate-control')
    if (p) p.remove()
  }
  function waitKey (key) {
    document.addEventListener('keydown', function (event) {
      if (event.key == key) {
        if (!openRateControl) {
          openRateControl = true
          addRateControl()
        } else {
          openRateControl = false
          removeRateControl()
        }
      }
    })
  }
  function run () {
    window.onload = function () {
      var video = document.querySelector('video')
      if (video) waitKey('@')
    }
  }
  run()
})();