Like Shortcut (Toggle Like)

Like YouTube Videos per Shortcut (default "O")

  1. // ==UserScript==
  2. // @name Like Shortcut (Toggle Like)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Like YouTube Videos per Shortcut (default "O")
  6. // @author You
  7. // @match https://www.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?domain=youtube.com
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15.  
  16. // >>>>>>>>>>>> SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  17.  
  18. const LIKE_SHORTCUT = "O" //YT uses L for forward
  19.  
  20. // <<<<<<<<<<<< SETTINGS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  21.  
  22. const shortcut = {
  23. 'all_shortcuts':{},//All the shortcuts are stored in this array
  24. 'add': function(shortcut_combination,callback,opt) {
  25. //Provide a set of default options
  26. var default_options = {
  27. 'type':'keydown',
  28. 'propagate':false,
  29. 'disable_in_input':false,
  30. 'target':document,
  31. 'keycode':false
  32. }
  33. if(!opt) opt = default_options;
  34. else {
  35. for(var dfo in default_options) {
  36. if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
  37. }
  38. }
  39.  
  40. var ele = opt.target;
  41. if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
  42. var ths = this;
  43. shortcut_combination = shortcut_combination.toLowerCase();
  44.  
  45. //The function to be called at keypress
  46. var func = function(e) {
  47. e = e || window.event;
  48.  
  49. if(opt['disable_in_input']) { //Don't enable shortcut keys in Input, Textarea fields
  50. var element;
  51. if(e.target) element=e.target;
  52. else if(e.srcElement) element=e.srcElement;
  53. if(element.nodeType==3) element=element.parentNode;
  54.  
  55. if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
  56. }
  57.  
  58. //Find Which key is pressed
  59. var code;
  60. if (e.keyCode) code = e.keyCode;
  61. else if (e.which) code = e.which;
  62. var character = String.fromCharCode(code).toLowerCase();
  63.  
  64. if(code == 188) character=","; //If the user presses , when the type is onkeydown
  65. if(code == 190) character="."; //If the user presses , when the type is onkeydown
  66.  
  67. var keys = shortcut_combination.split("+");
  68. //Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
  69. var kp = 0;
  70.  
  71. //Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
  72. var shift_nums = {
  73. "`":"~",
  74. "1":"!",
  75. "2":"@",
  76. "3":"#",
  77. "4":"$",
  78. "5":"%",
  79. "6":"^",
  80. "7":"&",
  81. "8":"*",
  82. "9":"(",
  83. "0":")",
  84. "-":"_",
  85. "=":"+",
  86. ";":":",
  87. "'":"\"",
  88. ",":"<",
  89. ".":">",
  90. "/":"?",
  91. "\\":"|"
  92. }
  93. //Special Keys - and their codes
  94. var special_keys = {
  95. 'esc':27,
  96. 'escape':27,
  97. 'tab':9,
  98. 'space':32,
  99. 'return':13,
  100. 'enter':13,
  101. 'backspace':8,
  102.  
  103. 'scrolllock':145,
  104. 'scroll_lock':145,
  105. 'scroll':145,
  106. 'capslock':20,
  107. 'caps_lock':20,
  108. 'caps':20,
  109. 'numlock':144,
  110. 'num_lock':144,
  111. 'num':144,
  112.  
  113. 'pause':19,
  114. 'break':19,
  115.  
  116. 'insert':45,
  117. 'home':36,
  118. 'delete':46,
  119. 'end':35,
  120.  
  121. 'pageup':33,
  122. 'page_up':33,
  123. 'pu':33,
  124.  
  125. 'pagedown':34,
  126. 'page_down':34,
  127. 'pd':34,
  128.  
  129. 'left':37,
  130. 'up':38,
  131. 'right':39,
  132. 'down':40,
  133.  
  134. 'f1':112,
  135. 'f2':113,
  136. 'f3':114,
  137. 'f4':115,
  138. 'f5':116,
  139. 'f6':117,
  140. 'f7':118,
  141. 'f8':119,
  142. 'f9':120,
  143. 'f10':121,
  144. 'f11':122,
  145. 'f12':123
  146. }
  147.  
  148. var modifiers = {
  149. shift: { wanted:false, pressed:false},
  150. ctrl : { wanted:false, pressed:false},
  151. alt : { wanted:false, pressed:false},
  152. meta : { wanted:false, pressed:false} //Meta is Mac specific
  153. };
  154.  
  155. if(e.ctrlKey) modifiers.ctrl.pressed = true;
  156. if(e.shiftKey) modifiers.shift.pressed = true;
  157. if(e.altKey) modifiers.alt.pressed = true;
  158. if(e.metaKey) modifiers.meta.pressed = true;
  159.  
  160. var k;
  161. for(var i=0; k=keys[i],i<keys.length; i++) {
  162. //Modifiers
  163. if(k == 'ctrl' || k == 'control') {
  164. kp++;
  165. modifiers.ctrl.wanted = true;
  166.  
  167. } else if(k == 'shift') {
  168. kp++;
  169. modifiers.shift.wanted = true;
  170.  
  171. } else if(k == 'alt') {
  172. kp++;
  173. modifiers.alt.wanted = true;
  174. } else if(k == 'meta') {
  175. kp++;
  176. modifiers.meta.wanted = true;
  177. } else if(k.length > 1) { //If it is a special key
  178. if(special_keys[k] == code) kp++;
  179.  
  180. } else if(opt['keycode']) {
  181. if(opt['keycode'] == code) kp++;
  182.  
  183. } else { //The special keys did not match
  184. if(character == k) kp++;
  185. else {
  186. if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
  187. character = shift_nums[character];
  188. if(character == k) kp++;
  189. }
  190. }
  191. }
  192. }
  193.  
  194. if(kp == keys.length &&
  195. modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
  196. modifiers.shift.pressed == modifiers.shift.wanted &&
  197. modifiers.alt.pressed == modifiers.alt.wanted &&
  198. modifiers.meta.pressed == modifiers.meta.wanted) {
  199. callback(e);
  200.  
  201. if(!opt['propagate']) { //Stop the event
  202. //e.cancelBubble is supported by IE - this will kill the bubbling process.
  203. e.cancelBubble = true;
  204. e.returnValue = false;
  205.  
  206. //e.stopPropagation works in Firefox.
  207. if (e.stopPropagation) {
  208. e.stopPropagation();
  209. e.preventDefault();
  210. }
  211. return false;
  212. }
  213. }
  214. }
  215. this.all_shortcuts[shortcut_combination] = {
  216. 'callback':func,
  217. 'target':ele,
  218. 'event': opt['type']
  219. };
  220. //Attach the function with the event
  221. if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
  222. else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
  223. else ele['on'+opt['type']] = func;
  224. },
  225.  
  226. //Remove the shortcut - just specify the shortcut and I will remove the binding
  227. 'remove':function(shortcut_combination) {
  228. shortcut_combination = shortcut_combination.toLowerCase();
  229. var binding = this.all_shortcuts[shortcut_combination];
  230. delete(this.all_shortcuts[shortcut_combination])
  231. if(!binding) return;
  232. var type = binding['event'];
  233. var ele = binding['target'];
  234. var callback = binding['callback'];
  235.  
  236. if(ele.detachEvent) ele.detachEvent('on'+type, callback);
  237. else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
  238. else ele['on'+type] = false;
  239. }
  240. }
  241.  
  242.  
  243. shortcut.add(
  244. LIKE_SHORTCUT,
  245. () => {
  246. document.getElementsByTagName("ytd-video-primary-info-renderer")[0].children[0].children[5].children[2].children[0].children[0].children[0].children[0].children[0].children[0].click()
  247. },
  248. {
  249. type: "keydown",
  250. disable_in_input: true
  251. }
  252. );
  253. })();

QingJ © 2025

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