Youtube playback rate shortcut

Press [ and ] to slow down/speed up the video, and shift+[ to reset the playback rate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Youtube playback rate shortcut 
// @namespace   Violentmonkey Scripts
// @match       *://youtube.com/*
// @match       *://*.youtube.com/*
// @grant       none
// @run-at      document-idle
// @version     1.0
// @author      qsniyg
// @description Press [ and ] to slow down/speed up the video, and shift+[ to reset the playback rate
// ==/UserScript==

(function() {
  var playback_increment = 0.25;

  var set_playback = function(diff) {
    var video_el = document.querySelector("video.video-stream.html5-main-video");
    if (video_el) {
      if (diff === 0)
        video_el.playbackRate = 1;
      else {
        var new_pr = video_el.playbackRate + diff * playback_increment;
        if (new_pr > 0)
          video_el.playbackRate = new_pr;
      }
    }
  }

  document.addEventListener("keydown", function(e) {
    if (e.target && e.target.tagName === "INPUT" || e.target.tagName === "TEXTAREA")
      return;
    
    if (e.key === "[" && !e.shiftKey) {
      set_playback(-1);
    } else if (e.key === "]") {
      set_playback(1);
    } else if (e.shiftKey && (e.key === "[" || e.key === "{")) {
      set_playback(0);
    }
  });
})();