YouTube Like/Dislike Shortcut Lite

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

安装此脚本?
作者推荐脚本

您可能也喜欢YouTube Like/Dislike Shortcut

安装此脚本
  1. // ==UserScript==
  2. // @name YouTube Like/Dislike Shortcut Lite
  3. // @name:pt-BR Atalhos Gostei/Não Gostei no YouTube Lite
  4. // @namespace will64gamer
  5. // @author will64gamer
  6. // @description Enables keyboard shortcuts to like/dislike a video on YouTube.
  7. // @description:pt-BR Cria atalhos para os botões gostei/não gostei em um vídeo no YouTube.
  8. // @match *://*.youtube.com/*
  9. // @license MIT
  10. // @version 1.1
  11. // ==/UserScript==
  12. // You can change the codes to whichever keys you want to use for liking, disliking, and opening or writing comments on Shorts.
  13. const codeLike = "NumpadAdd";
  14. const codeDislike = "NumpadSubtract";
  15.  
  16. /* Filling in these quotation marks with the code of a key creates an additional shortcut dedicated to removing your like/dislike,
  17. that makes it so that pressing the regular shortcuts for liking/disliking multiple times has no effect on the state of that button.
  18. If you want to use the regular shortcuts for removing likes/dislikes as well, leave this blank. */
  19. let codeRemove = "";
  20.  
  21. // 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.
  22. const combination = 0;
  23.  
  24. // /\/\/\ Settings /\/\/\
  25. // ------------------------------
  26. // \/\/\/ Code \/\/\/
  27.  
  28. let tag, timeout, like, dislike;
  29.  
  30. if (typeof(codeRemove) === "string") {codeRemove = codeRemove.trim();}
  31.  
  32. const observer = new MutationObserver(findButtons);
  33.  
  34. addEventListener('yt-page-data-updated', reset);
  35.  
  36. function reset() {
  37. isVideo = /^\/watch/.test(location.pathname);
  38. if (isVideo) {
  39. removeEventListener("keydown", press);
  40. like = null; dislike = null;
  41. observer.observe(document.documentElement, {childList: true, subtree: true});
  42. findButtons();
  43. }
  44. }
  45.  
  46. function findButtons() {
  47. if (like && dislike) {
  48. addEventListener("keydown", press);
  49. observer.disconnect();
  50. }
  51.  
  52. like = document?.getElementsByTagName("like-button-view-model")[0]?.firstElementChild?.firstElementChild?.firstElementChild;
  53. dislike = document?.getElementsByTagName("dislike-button-view-model")[0]?.firstElementChild?.firstElementChild?.firstElementChild;
  54. }
  55.  
  56. function press(e) {
  57. if (e.target.getAttribute("contenteditable") === "true") {return;}
  58.  
  59. tag = e.target.tagName.toLowerCase();
  60. if (tag === "input" || tag === "textarea") {return;}
  61.  
  62. switch (combination) {
  63. case 1:
  64. if (!e.ctrlKey) {return;}
  65. break;
  66. case 2:
  67. if (!e.altKey) {return;}
  68. break;
  69. case 3:
  70. if (!e.shiftKey) {return;}
  71. break;
  72. }
  73.  
  74. switch (e.code) {
  75. case codeLike:
  76. if (like) {
  77. if (codeRemove && checkPressed(like)) {break;}
  78. else {like.click();}
  79. }
  80. case codeDislike:
  81. if (dislike) {
  82. if (codeRemove && checkPressed(dislike)) {break;}
  83. else {dislike.click();}
  84. }
  85. break;
  86. case codeRemove:
  87. if (checkPressed(like)) {like.click();}
  88. else if (checkPressed(dislike)) {dislike.click();}
  89. break;
  90. }
  91. }
  92.  
  93. function checkPressed(element) {
  94. return (element.getAttribute("aria-pressed") === "true");
  95. }