Music-Tab Reload

Reload YouTube on Music Category-Tab + custom Shortcut (default "Alt+M")

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

QingJ © 2025

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