Video Playback Rate Controller

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

  1. // ==UserScript==
  2. // @name Video Playback Rate Controller
  3. // @name:cn 视频倍速控制器
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1.0
  6. // @description 【使用前先看介绍/有问题可反馈】视频倍速控制器 (Video Playback Rate Controller): 为视频添加倍速控制元素,通过 `Shift + @` 可以随时将控制器显示或隐藏。
  7. // @author cc
  8. // @include *
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. var openRateControl = false
  15. function addRateControl () {
  16. var video = document.querySelector('video')
  17. var select = document.createElement('select')
  18. select.name = 'play-rate'
  19. for (let rate of [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 8, 16]) {
  20. let option = document.createElement('option')
  21. option.value = rate
  22. option.innerText = option.value + '倍速'
  23. select.appendChild(option)
  24. }
  25. select.onchange = function (event) {
  26. video.playbackRate = event.target.value
  27. }
  28. select.selectedIndex = 2
  29. var p = document.createElement('p')
  30. p.id = 'rate-control'
  31. p.appendChild(select)
  32. if (video.nextElementSibling) {
  33. video.parentElement.insertBefore(p, video.nextElementSibling)
  34. } else {
  35. video.parentElement.appendChild(p)
  36. }
  37. }
  38. function removeRateControl () {
  39. var p = document.getElementById('rate-control')
  40. if (p) p.remove()
  41. }
  42. function waitKey (key) {
  43. document.addEventListener('keydown', function (event) {
  44. if (event.key == key) {
  45. if (!openRateControl) {
  46. openRateControl = true
  47. addRateControl()
  48. } else {
  49. openRateControl = false
  50. removeRateControl()
  51. }
  52. }
  53. })
  54. }
  55. function run () {
  56. window.onload = function () {
  57. var video = document.querySelector('video')
  58. if (video) waitKey('@')
  59. }
  60. }
  61. run()
  62. })();