SpeedeKey: Control HTML5 Video Speed with a Tap

Change HTML5 video playback speed right from your keyboard. ("[" to decrease, "]" to increase, Backspace to reset)

  1. // ==UserScript==
  2. // @name SpeedeKey: Control HTML5 Video Speed with a Tap
  3. // @author cemiu
  4. // @license MIT
  5. // @namespace https://cemiu.net/
  6. // @homepage https://gist.github.com/cemiu/db9f64b9d199b75ab218bb7a23b68734
  7. // @homepage https://cemiu.net/
  8. // @version 1
  9. // @description Change HTML5 video playback speed right from your keyboard. ("[" to decrease, "]" to increase, Backspace to reset)
  10. // @match *://*/*
  11. // @grant none
  12. // @run-at document-end
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // Change keybindings for speed modification
  19. const KEY_SPEED_DECREASE = '['; // decrease playback speed by 0.25x
  20. const KEY_SPEED_INCREASE = ']'; // increase playback speed by 0.25x
  21. const KEY_SPEED_RESET = 'Backspace'; // reset backback speed to 1x
  22. const SPEED_INCREMENT = 0.25;
  23.  
  24. let playbackSpeed = 1.0;
  25. let displayTimeout;
  26. let displayVisible = false; // Track if display is visible
  27.  
  28. // Create a display element for current speed
  29. const displayElement = document.createElement('div');
  30. displayElement.style.position = 'fixed';
  31. displayElement.style.top = '10px';
  32. displayElement.style.left = '10px';
  33. displayElement.style.padding = '5px';
  34. displayElement.style.background = 'rgba(128, 128, 128, 0.9)';
  35. displayElement.style.color = 'white';
  36. displayElement.style.zIndex = '9999';
  37. displayElement.style.fontFamily = 'Arial, sans-serif';
  38. displayElement.style.transition = 'opacity 0.2s';
  39. displayElement.style.opacity = '0';
  40. displayElement.style.fontSize = '14px';
  41.  
  42. // Append the display element to the body
  43. document.body.appendChild(displayElement);
  44.  
  45. // Function to update the display with current speed
  46. function updateSpeedDisplay() {
  47. if (displayVisible) {
  48. displayElement.textContent = `Speed: ${playbackSpeed.toFixed(2)}x`;
  49. displayElement.style.opacity = '1';
  50. }
  51.  
  52. clearTimeout(displayTimeout);
  53. displayTimeout = setTimeout(() => {
  54. displayElement.style.opacity = '0';
  55. }, 600);
  56. }
  57.  
  58. // Function to change the playback speed of the video
  59. function changePlaybackSpeed(speed) {
  60. playbackSpeed += speed;
  61. if (playbackSpeed < SPEED_INCREMENT) { // Don't let speed stop to 0x
  62. playbackSpeed = SPEED_INCREMENT;
  63. }
  64. const videos = document.querySelectorAll('video');
  65. if (videos.length > 0) {
  66. videos.forEach(video => {
  67. video.playbackRate = playbackSpeed;
  68. });
  69. displayVisible = true; // Show display if speed changed on a video page
  70. updateSpeedDisplay();
  71. }
  72. }
  73.  
  74. // Event listener for key presses
  75. document.addEventListener('keydown', event => {
  76. if (event.key === KEY_SPEED_DECREASE) {
  77. changePlaybackSpeed(-SPEED_INCREMENT);
  78. } else if (event.key === KEY_SPEED_INCREASE) {
  79. changePlaybackSpeed(SPEED_INCREMENT);
  80. } else if (event.key === KEY_SPEED_RESET) {
  81. playbackSpeed = 1.0;
  82. const videos = document.querySelectorAll('video');
  83. if (videos.length > 0) {
  84. videos.forEach(video => {
  85. video.playbackRate = playbackSpeed;
  86. });
  87. displayVisible = true; // Show display if speed reset on a video page
  88. updateSpeedDisplay();
  89. }
  90. }
  91. });
  92.  
  93. // Initialize the speed display
  94. updateSpeedDisplay();
  95. })();