Opera Vim-like Navigation

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

目前为 2019-04-16 提交的版本,查看 最新版本

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