Youtube like/dislike video and skip ad keyboard shortcuts

Adds keyboard shortcuts [ and ] for liking and disliking videos, and s for skipping pre-video and banner ads.

当前为 2017-10-11 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Youtube like/dislike video and skip ad keyboard shortcuts
// @namespace   nerevar009
// @include     https://www.youtube.com/*
// @description Adds keyboard shortcuts [ and ] for liking and disliking videos, and s for skipping pre-video and banner ads.
// @version     1.0
// @grant       none
// ==/UserScript==


var onvideopage, skipad, skipbannerad, videoinfo, buttons, like, dislike, tag;

function findButtons() {
  if(!/^\/watch/.test(location.pathname)) {
    onvideopage = false;
    return;
  }
  onvideopage = true;

  skipad = document.getElementsByClassName("videoAdUiSkipButton");
  if(skipad.length == 1)
    skipad = skipad[0];
  else skipad = null;

  skipbannerad = document.getElementsByClassName("close-padding");
  if(skipbannerad.length == 1) {
    skipbannerad = skipbannerad[0];
  }
  else skipbannerad = null;
  
  videoinfo = document.getElementsByTagName("ytd-video-primary-info-renderer");
  if(videoinfo.length == 1) {
    buttons = videoinfo[0].getElementsByTagName("button");
    like = buttons[0];
    dislike = buttons[1];
  }
  else {
    like = null;
    dislike = null;
  }
}

findButtons();
var observer = new MutationObserver(findButtons);
observer.observe(document.documentElement, {childList: true, subtree: true});

// add keybindings
addEventListener("keypress", function(e) {
  if(!onvideopage)
    return;

  if(e.target.getAttribute("contenteditable") == "true")
    return;

  tag = e.target.tagName.toLowerCase();
  if(tag == "input" || tag == "textarea")
    return;

  if(e.code == "BracketLeft" && like)
    like.click();
  else if(e.code == "BracketRight" && dislike)
    dislike.click();
  else if(e.code == "KeyS") {
    if(skipad)
      skipad.click();
    else if(skipbannerad)
      skipbannerad.click();
  }
});