YouTube Like/Dislike Shortcut

Enables keyboard shortcuts to like/dislike a video on YouTube.

目前为 2023-12-29 提交的版本。查看 最新版本

// ==UserScript==
// @name               YouTube Like/Dislike Shortcut
// @name:pt-BR         Atalhos Gostei/Não Gostei no YouTube
// @namespace          will64gamer
// @author             will64gamer
// @description        Enables keyboard shortcuts to like/dislike a video on YouTube.
// @description:pt-BR  Cria atalhos para os botões gostei/não gostei em um vídeo no YouTube.
// @include            https://www.youtube.com/*
// @license            MIT
// @version            1.6
// ==/UserScript==

let codeLike = "NumpadAdd";
let codeDislike = "NumpadSubtract";

let videopage, tag, like, dislike, sLike, sDislike, pageFirst;

let observer = new MutationObserver(findButtons);

addEventListener('yt-page-data-updated', reset);

function findButtons() {
  if ((like && dislike) || (sLike && sDislike)) {
    observer.disconnect();
    return;
  }

  if(/^\/shorts/.test(location.pathname)) {
    sLike = document.getElementById("like-button").getElementsByTagName('button')[0];
    sDislike = document.getElementById("dislike-button").getElementsByTagName('button')[0];
  } else {
    like = document.getElementsByTagName("like-button-view-model")[0]?.firstElementChild?.firstElementChild;
    dislike = document.getElementsByTagName("dislike-button-view-model")[0]?.firstElementChild?.firstElementChild;
  }
}

function press(e) {
  if (!videopage) {return;}

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

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

  switch (e.code) {
    case codeLike:
      if (like) {like.click();}
      else if (sLike) {sLike.click();}
      break;
    case codeDislike:
      if (dislike) {dislike.click();}
      else if (sDislike) {sDislike.click();}
      break;
  }
}

function reset(e) {
  if ((/^\/watch/.test(location.pathname)||/^\/shorts/.test(location.pathname))) {
    videopage = true;
    removeEventListener("keydown", press); addEventListener("keydown", press);
    observer.observe(document.documentElement, {childList: true, subtree: true});
    like = null; dislike = null; sLike = null; sDislike = null;
    findButtons;
  } else {
    videopage = false;
  }
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址