Video Playback Rate Controller

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

当前为 2021-04-16 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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