Video Playback Rate Controller

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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