YouTube Like/Dislike Shortcut

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

目前為 2024-05-01 提交的版本,檢視 最新版本

// ==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            2.2
// ==/UserScript==

// You can change the codes to whichever keys you want to use for liking, disliking, and commenting on Shorts.
const codeLike = "NumpadAdd";
const codeDislike = "NumpadSubtract";
const codeComments = "NumpadMultiply";

// Change this to false if you don't want Shorts to refresh on load.
const shortsRefresh = true;

let isShort = false;
let tag, like, dislike, sLike, sDislike, comments, textBox, initialRefresh;

const observer = new MutationObserver(findButtons);

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

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

function findButtons() {
  if ((like && dislike) || (sLike && sDislike && comments)) {
    addEventListener("keydown", press);
    observer.disconnect();
    let currentURL = location.href;
    if (isShort && shortsRefresh) {
      let shortInterval = setInterval(() => {
        if (location.href !== currentURL) {
          clearInterval(shortInterval);
          location.reload();
          }
        }, 400);
    } else if (isVideo) {initialRefresh = true;}
  }

  if (isShort) {
    if (initialRefresh) {
      location.reload();
      initialRefresh = false;
    }
    sLike = document.getElementById("like-button")?.getElementsByTagName('button')[0];
    sDislike = document.getElementById("dislike-button")?.getElementsByTagName('button')[0];
    comments = document.getElementById("comments-button")?.getElementsByTagName('button')[0];
  } else {
    like = document.getElementsByTagName("like-button-view-model")[0]?.firstElementChild?.firstElementChild?.firstElementChild;
    dislike = document.getElementsByTagName("dislike-button-view-model")[0]?.firstElementChild?.firstElementChild?.firstElementChild;
  }
}

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

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

  switch (e.code) {
    case codeComments:
      if (comments) {
        comments.click();
// Remove this section before break to not focus on the textbox automatically
        let textboxInterval = setInterval(() => {
          if (!textBox) {
            textBox = document.getElementById("simplebox-placeholder");
          } else {
            textBox.focus();
            clearInterval(textboxInterval);
          }
        }, 300);
      }
      break;
    case codeLike:
      if (like) {like.click();}
      else if (sLike) {sLike.click();}
      break;
    case codeDislike:
      if (dislike) {dislike.click();}
      else if (sDislike) {sDislike.click();}
      break;
  }
}

QingJ © 2025

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