More Keybinds

Adds some extra keystrokes to Firefox.

目前为 2017-05-07 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name More Keybinds
  3. // @namespace MK
  4. // @description Adds some extra keystrokes to Firefox.
  5. // @version 1.2.2
  6. // @include *
  7. // @run-at document-start
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. var SCROLL_AMOUNT = 60;
  12.  
  13. // Not all keys fire a keypress event (Chrome 2010), so we use keydown.
  14. document.addEventListener('keydown', keypressListener, false);
  15.  
  16. function keypressListener(evt) {
  17. var code = evt.keyCode || evt.which;
  18.  
  19. /*
  20. var modifierReport = "";
  21. modifierReport += ( evt.ctrlKey ? "Ctrl " : "" );
  22. modifierReport += ( evt.shiftKey ? "Shift " : "" );
  23. modifierReport += ( evt.altKey ? "Alt " : "" );
  24. GM_log("Caught keypress "+code+" with modifiers: "+modifierReport);
  25. */
  26.  
  27. // Actions
  28.  
  29. // Ctrl+Delete goes Back
  30. if (code == 8 && evt.ctrlKey) {
  31. window.history.back();
  32. }
  33.  
  34. // Ctrl+Enter goes Forward
  35. if (code == 13 && evt.ctrlKey) {
  36. window.history.forward();
  37. }
  38.  
  39. /* These conflict with selecting words in text!
  40.  
  41. // Ctrl+Shift+Left goes Back
  42. if (code == 37 && evt.ctrlKey && evt.shiftKey) {
  43. window.history.back();
  44. }
  45.  
  46. // Ctrl+Shift+Right goes Forward
  47. if (code == 39 && evt.ctrlKey && evt.shiftKey) {
  48. window.history.forward();
  49. }
  50.  
  51. */
  52.  
  53. // Ctrl+Shift+Up goes up in the URL path (removes the tail leaf)
  54. if (code == 38 && evt.ctrlKey && evt.shiftKey) {
  55. var newURL = document.location.href;
  56. if (newURL.slice(-1)=='/') {
  57. newURL = newURL.slice(0,-1);
  58. }
  59. document.location.href = document.location.href.replace(/[#/?][^#/?]*[/]*$/,'');
  60. }
  61.  
  62. // Do not intercept any of the keys below when the user is focused on an input or textarea.
  63. /*
  64. //var focusedElement = document.activeElement; // document.body if no input is focused
  65. var focusedElement = evt.target || event.srcElement;
  66. if (focusedElement) {
  67. var isInput = focusedElement.nodeName === 'INPUT' || focusedElement.nodeName === 'TEXTAREA';
  68. if (isInput) {
  69. return;
  70. }
  71. }
  72. */
  73. // From next_imageprevious_image.user.js:
  74. if (evt.target.tagName && evt.target.tagName.match(/input|select|textarea/i) || evt.target.getAttribute('contenteditable')==="true") {
  75. return;
  76. }
  77.  
  78. if (!evt.ctrlKey && !evt.shiftKey && !evt.metaKey) {
  79. if (document.location.host !== "9gag.com") {
  80. if (code === 'K'.charCodeAt(0)) {
  81. scrollBy(-getScrollAmount());
  82. }
  83.  
  84. if (code === 'J'.charCodeAt(0)) {
  85. scrollBy(+getScrollAmount());
  86. }
  87. }
  88. }
  89.  
  90. }
  91.  
  92. function scrollBy(amount) {
  93. // If jQuery is present, use it to perform a smooth scroll
  94. if (typeof $ !== "undefined" && $.fn && $.fn.animate) {
  95. queue(function(next){
  96. $("html,body").animate({scrollTop: $(document).scrollTop() + amount}, 200, "swing", ifBody(next));
  97. });
  98. } else {
  99. // otherwise perform a jerky scroll
  100. // Chrome:
  101. document.body.scrollTop += amount;
  102. // Firefox:
  103. document.documentElement.scrollTop += amount;
  104. }
  105. }
  106.  
  107. function getScrollAmount() {
  108. return window.innerHeight / 6;
  109. }
  110.  
  111. function ifBody(fn) {
  112. return function(){
  113. // jQuery calls complete once for each element, and we have two elements. Annoying!
  114. if (this === document.body) {
  115. fn();
  116. }
  117. };
  118. }
  119.  
  120. var actions = [];
  121. var running = false;
  122. function queue(action) {
  123. actions.push(action);
  124. if (!running) {
  125. dequeue();
  126. }
  127. }
  128. function dequeue() {
  129. if (actions.length > 0) {
  130. var nextAction = actions.shift();
  131. running = true;
  132. nextAction(dequeue);
  133. } else {
  134. running = false;
  135. }
  136. }