Advertisement
Guest User

Tradingview Custom Shortcuts

a guest
Jan 18th, 2019
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. Result Preview: https://i.imgur.com/084ZEPP.gif
  2.  
  3.  
  4. 1) Install Greasemonkey Extension for your browser
  5.  
  6. 2) In this example I've made Ctrl+Shift+Y Maximize Individual Chart Windows
  7.  
  8. Using Razer Synapse App I have rebound Mouse Scrollwheel Click -> Ctrl+Shift+Y
  9.  
  10. (Double Click in Tradingview Maximizes Pane, but there is no Maximize shortcut for each chart window so middle click makes sense to use)
  11.  
  12. 3) Create Greasemonkey Script ^(Modify/Add Shotcuts at Bottom of Code)
  13.  
  14. // ==UserScript==
  15. // @name Traindview Keyboard Shortcuts
  16. // @namespace http://tampermonkey.net/
  17. // @version 0.1
  18. // @description try to take over the world!
  19. // @author You
  20. // @match https://www.tradingview.com/chart/*
  21. // @grant none
  22. // ==/UserScript==
  23.  
  24. /**
  25. * http://www.openjs.com/scripts/events/keyboard_shortcuts/
  26. * Version : 2.01.B
  27. * By Binny V A
  28. * License : BSD
  29. */
  30. shortcut = {
  31. 'all_shortcuts':{},//All the shortcuts are stored in this array
  32. 'add': function(shortcut_combination,callback,opt) {
  33. //Provide a set of default options
  34. var default_options = {
  35. 'type':'keydown',
  36. 'propagate':false,
  37. 'disable_in_input':false,
  38. 'target':document,
  39. 'keycode':false
  40. }
  41. if(!opt) opt = default_options;
  42. else {
  43. for(var dfo in default_options) {
  44. if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
  45. }
  46. }
  47.  
  48. var ele = opt.target;
  49. if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
  50. var ths = this;
  51. shortcut_combination = shortcut_combination.toLowerCase();
  52.  
  53. //The function to be called at keypress
  54. var func = function(e) {
  55. e = e || window.event;
  56.  
  57. if(opt['disable_in_input']) { //Don't enable shortcut keys in Input, Textarea fields
  58. var element;
  59. if(e.target) element=e.target;
  60. else if(e.srcElement) element=e.srcElement;
  61. if(element.nodeType==3) element=element.parentNode;
  62.  
  63. if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
  64. }
  65.  
  66. //Find Which key is pressed
  67. if (e.keyCode) code = e.keyCode;
  68. else if (e.which) code = e.which;
  69. var character = String.fromCharCode(code).toLowerCase();
  70.  
  71. if(code == 188) character=","; //If the user presses , when the type is onkeydown
  72. if(code == 190) character="."; //If the user presses , when the type is onkeydown
  73.  
  74. var keys = shortcut_combination.split("+");
  75. //Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
  76. var kp = 0;
  77.  
  78. //Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
  79. var shift_nums = {
  80. "`":"~",
  81. "1":"!",
  82. "2":"@",
  83. "3":"#",
  84. "4":"$",
  85. "5":"%",
  86. "6":"^",
  87. "7":"&",
  88. "8":"*",
  89. "9":"(",
  90. "0":")",
  91. "-":"_",
  92. "=":"+",
  93. ";":":",
  94. "'":"\"",
  95. ",":"<",
  96. ".":">",
  97. "/":"?",
  98. "\\":"|"
  99. }
  100. //Special Keys - and their codes
  101. var special_keys = {
  102. 'esc':27,
  103. 'escape':27,
  104. 'tab':9,
  105. 'space':32,
  106. 'return':13,
  107. 'enter':13,
  108. 'backspace':8,
  109.  
  110. 'scrolllock':145,
  111. 'scroll_lock':145,
  112. 'scroll':145,
  113. 'capslock':20,
  114. 'caps_lock':20,
  115. 'caps':20,
  116. 'numlock':144,
  117. 'num_lock':144,
  118. 'num':144,
  119.  
  120. 'pause':19,
  121. 'break':19,
  122.  
  123. 'insert':45,
  124. 'home':36,
  125. 'delete':46,
  126. 'end':35,
  127.  
  128. 'pageup':33,
  129. 'page_up':33,
  130. 'pu':33,
  131.  
  132. 'pagedown':34,
  133. 'page_down':34,
  134. 'pd':34,
  135.  
  136. 'left':37,
  137. 'up':38,
  138. 'right':39,
  139. 'down':40,
  140.  
  141. 'f1':112,
  142. 'f2':113,
  143. 'f3':114,
  144. 'f4':115,
  145. 'f5':116,
  146. 'f6':117,
  147. 'f7':118,
  148. 'f8':119,
  149. 'f9':120,
  150. 'f10':121,
  151. 'f11':122,
  152. 'f12':123
  153. }
  154.  
  155. var modifiers = {
  156. shift: { wanted:false, pressed:false},
  157. ctrl : { wanted:false, pressed:false},
  158. alt : { wanted:false, pressed:false},
  159. meta : { wanted:false, pressed:false} //Meta is Mac specific
  160. };
  161.  
  162. if(e.ctrlKey) modifiers.ctrl.pressed = true;
  163. if(e.shiftKey) modifiers.shift.pressed = true;
  164. if(e.altKey) modifiers.alt.pressed = true;
  165. if(e.metaKey) modifiers.meta.pressed = true;
  166.  
  167. for(var i=0; k=keys[i],i<keys.length; i++) {
  168. //Modifiers
  169. if(k == 'ctrl' || k == 'control') {
  170. kp++;
  171. modifiers.ctrl.wanted = true;
  172.  
  173. } else if(k == 'shift') {
  174. kp++;
  175. modifiers.shift.wanted = true;
  176.  
  177. } else if(k == 'alt') {
  178. kp++;
  179. modifiers.alt.wanted = true;
  180. } else if(k == 'meta') {
  181. kp++;
  182. modifiers.meta.wanted = true;
  183. } else if(k.length > 1) { //If it is a special key
  184. if(special_keys[k] == code) kp++;
  185.  
  186. } else if(opt['keycode']) {
  187. if(opt['keycode'] == code) kp++;
  188.  
  189. } else { //The special keys did not match
  190. if(character == k) kp++;
  191. else {
  192. if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
  193. character = shift_nums[character];
  194. if(character == k) kp++;
  195. }
  196. }
  197. }
  198. }
  199.  
  200. if(kp == keys.length &&
  201. modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
  202. modifiers.shift.pressed == modifiers.shift.wanted &&
  203. modifiers.alt.pressed == modifiers.alt.wanted &&
  204. modifiers.meta.pressed == modifiers.meta.wanted) {
  205. callback(e);
  206.  
  207. if(!opt['propagate']) { //Stop the event
  208. //e.cancelBubble is supported by IE - this will kill the bubbling process.
  209. e.cancelBubble = true;
  210. e.returnValue = false;
  211.  
  212. //e.stopPropagation works in Firefox.
  213. if (e.stopPropagation) {
  214. e.stopPropagation();
  215. e.preventDefault();
  216. }
  217. return false;
  218. }
  219. }
  220. }
  221. this.all_shortcuts[shortcut_combination] = {
  222. 'callback':func,
  223. 'target':ele,
  224. 'event': opt['type']
  225. };
  226. //Attach the function with the event
  227. if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
  228. else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
  229. else ele['on'+opt['type']] = func;
  230. },
  231.  
  232. //Remove the shortcut - just specify the shortcut and I will remove the binding
  233. 'remove':function(shortcut_combination) {
  234. shortcut_combination = shortcut_combination.toLowerCase();
  235. var binding = this.all_shortcuts[shortcut_combination];
  236. delete(this.all_shortcuts[shortcut_combination])
  237. if(!binding) return;
  238. var type = binding['event'];
  239. var ele = binding['target'];
  240. var callback = binding['callback'];
  241.  
  242. if(ele.detachEvent) ele.detachEvent('on'+type, callback);
  243. else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
  244. else ele['on'+type] = false;
  245. }
  246. }
  247.  
  248. shortcut.add("Ctrl+Shift+Y",function() {
  249. $(".chart-container:hover .chart-controls-bar [class^='seriesControlWrapper-']>div:last-of-type>div").click(function (e) {
  250. }).click();
  251. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement