Vim-like Navigation

Use (h,j,k,l) to scroll around. gg to go to top G to to bottom

  1. // ==UserScript==
  2. // @name Vim-like Navigation
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Use (h,j,k,l) to scroll around. gg to go to top G to to bottom
  6. // @author Max Schulte
  7. // @match http*://*
  8. // @match *://*/*
  9. // @match *
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. // Your code here...
  16. var keyLog = []
  17.  
  18. document.onkeypress = function (e) {
  19. // if user is typing inside of a text box return
  20. if ( e.target.nodeName == 'INPUT' ) return;
  21. // event
  22. e = e || window.event;
  23. // horizontal and vertical
  24. var h = 0;
  25. var v = 0;
  26. // scroll amount
  27. keyLog.push(e.keyCode)
  28. //console.log(keyLog)
  29. var sa = 100;
  30. switch (e.keyCode){
  31. case 104: // h
  32. h -= sa;
  33. keyLog = [];
  34. break;
  35. case 106: // j
  36. v += sa;
  37. keyLog = [];
  38. break;
  39. case 107: // k
  40. v -= sa;
  41. keyLog = [];
  42. break;
  43. case 108: // l
  44. h += sa;
  45. keyLog = [];
  46. break;
  47. case 103: // gg
  48. if (keyLog[keyLog.length-2] != 103) {
  49. break;
  50. }
  51. window.scrollTo(0, 0);
  52. keyLog = [];
  53. return;
  54. case 71: // G
  55. console.log(document.documentElement.scrollHeight)
  56. window.scrollTo(0, document.documentElement.scrollHeight)
  57. keyLog = [];
  58. return;
  59. default:
  60. keyLog = [];
  61. break;
  62. }
  63. window.scrollBy(h, v);
  64. };
  65. })();