HTML5 Video Playback Speed Control Keyboard Shortcut

Add keyboard shortcuts to control HTML5 video playback rate. Available keyboard shortcuts are: <CTRL+[> = Decreate playback rate, <CTRL+]> = Increase playback rate, <CTRL+\> = Set playback rate increment/decrement unit, <CTRL+'> = Set playback to specific rate, <CTRL+;> Reset playback rate to default (to 1.0). Available playback speeds are between 0.25 to 2.0 inclusive in 0.25 increments (configurable via script).

当前为 2018-10-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name HTML5 Video Playback Speed Control Keyboard Shortcut
  3. // @namespace HTML5VideoPlaybackSpeedControlKeyboardShortcut
  4. // @description Add keyboard shortcuts to control HTML5 video playback rate. Available keyboard shortcuts are: <CTRL+[> = Decreate playback rate, <CTRL+]> = Increase playback rate, <CTRL+\> = Set playback rate increment/decrement unit, <CTRL+'> = Set playback to specific rate, <CTRL+;> Reset playback rate to default (to 1.0). Available playback speeds are between 0.25 to 2.0 inclusive in 0.25 increments (configurable via script).
  5. // @version 1.2.5
  6. // @license AGPLv3
  7. // @author jcunews
  8. // @include http://*/*
  9. // @include https://*/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.  
  15. /*
  16. Notes:
  17. 1. YouTube's video playback speed menu selection will not be affected.
  18. 2. Web browser playback rates: Firefox = 0.25 to 5.0; Chrome = 0.1 to 16.0.
  19. */
  20.  
  21. //=== CONFIGURATION BEGIN ===
  22.  
  23. //Playback rate increment/decrement unit
  24. var rateUnit = 0.2;
  25.  
  26. //Duration (in milliseconds) to display On Screen Display (OSD) when changing playback rate. Set to zero or less to disable.
  27. var osdTimeout = 3000;
  28.  
  29. //=== CONFIGURATION END ===
  30.  
  31. var eleOSD, osdTimer;
  32. function showOSD(rate) {
  33. if (eleOSD) {
  34. eleOSD.textContent = rate + "X";
  35. } else {
  36. eleOSD = document.createElement("DIV");
  37. eleOSD.style.cssText = "position:fixed;z-index:999999999;right:5px;bottom:5px;margin:0;padding:5px;width:auto;height:auto;font:bold 10pt/normal monospace;background:#444;color:#fff";
  38. eleOSD.textContent = rate + "X";
  39. document.body.appendChild(eleOSD);
  40. }
  41. clearTimeout(osdTimer);
  42. osdTimer = setTimeout(function() {
  43. eleOSD.remove();
  44. eleOSD = null;
  45. }, osdTimeout);
  46. }
  47.  
  48. addEventListener("keydown", function(ev) {
  49. var ele = document.querySelector("VIDEO"), rate, inp;
  50. if (ele && ev.ctrlKey && !ev.shiftKey && !ev.altKey) {
  51. rate = rate = ele.playbackRate;
  52. switch (ev.key) {
  53. case "[":
  54. rate -= rateUnit;
  55. if (rate < 0.1) rate = 0.1;
  56. break;
  57. case "]":
  58. rate += rateUnit;
  59. if (rate > 16) rate = 16;
  60. break;
  61. case "\\":
  62. if ((inp = prompt("Enter playback rate increment/decrement unit.", rate)) === null) return;
  63. if (isNaN(inp = parseFloat(inp.trim())) || (inp <= 0) || (inp >= 16)) {
  64. alert("Number must be greater than zero, and less than 16.");
  65. return;
  66. }
  67. rate = inp;
  68. break;
  69. case "'":
  70. if ((inp = prompt("Enter playback rate.\n(1.0 = Normal)", rate)) === null) return;
  71. if (isNaN(inp = parseFloat(inp.trim())) || (inp < 0.1) || (inp > 16)) {
  72. alert("Number must be between 0.1 to 16 (inclusive).");
  73. return;
  74. }
  75. rate = inp;
  76. break;
  77. case ";":
  78. rate = 1;
  79. break;
  80. default:
  81. return;
  82. }
  83. rate = parseFloat(rate.toFixed(2));
  84. ele.playbackRate = rate;
  85. if (osdTimeout > 0) showOSD(ele.playbackRate);
  86. }
  87. });
  88.  
  89. })();