Enables keyboard shortcuts to like/dislike a video on YouTube.
当前为
// ==UserScript==
// @name YouTube Like/Dislike Shortcut Lite
// @name:pt-BR Atalhos Gostei/Não Gostei no YouTube Lite
// @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.
// @match *://*.youtube.com/*
// @license MIT
// @version 1.1
// ==/UserScript==
// You can change the codes to whichever keys you want to use for liking, disliking, and opening or writing comments on Shorts.
const codeLike = "NumpadAdd";
const codeDislike = "NumpadSubtract";
/* Filling in these quotation marks with the code of a key creates an additional shortcut dedicated to removing your like/dislike,
that makes it so that pressing the regular shortcuts for liking/disliking multiple times has no effect on the state of that button.
If you want to use the regular shortcuts for removing likes/dislikes as well, leave this blank. */
let codeRemove = "";
// If you want the shortcut to be triggered only when holding ctrl, alt, or shift, change this value from 0 to 1, 2, or 3, respectively.
const combination = 0;
// /\/\/\ Settings /\/\/\
// ------------------------------
// \/\/\/ Code \/\/\/
let tag, timeout, like, dislike;
if (typeof(codeRemove) === "string") {codeRemove = codeRemove.trim();}
const observer = new MutationObserver(findButtons);
addEventListener('yt-page-data-updated', reset);
function reset() {
isVideo = /^\/watch/.test(location.pathname);
if (isVideo) {
removeEventListener("keydown", press);
like = null; dislike = null;
observer.observe(document.documentElement, {childList: true, subtree: true});
findButtons();
}
}
function findButtons() {
if (like && dislike) {
addEventListener("keydown", press);
observer.disconnect();
}
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 (combination) {
case 1:
if (!e.ctrlKey) {return;}
break;
case 2:
if (!e.altKey) {return;}
break;
case 3:
if (!e.shiftKey) {return;}
break;
}
switch (e.code) {
case codeLike:
if (like) {
if (codeRemove && checkPressed(like)) {break;}
else {like.click();}
}
case codeDislike:
if (dislike) {
if (codeRemove && checkPressed(dislike)) {break;}
else {dislike.click();}
}
break;
case codeRemove:
if (checkPressed(like)) {like.click();}
else if (checkPressed(dislike)) {dislike.click();}
break;
}
}
function checkPressed(element) {
return (element.getAttribute("aria-pressed") === "true");
}