WASD Scroll hotkeys for websites

Use WASD keys to scroll, not just arrow buttons

Asenna tämä skripti?
Author's suggested script

Saatat myös pitää

Asenna tämä skripti
  1. // ==UserScript==
  2. // @name WASD Scroll hotkeys for websites
  3. // @namespace wasd_scroll
  4. // @author Owyn
  5. // @version 1.1
  6. // @description Use WASD keys to scroll, not just arrow buttons
  7. // @supportURL https://github.com/Owyn/WASD_Scroll/issues
  8. // @homepage https://github.com/Owyn/WASD_Scroll
  9. // @run-at document-end
  10. // @grant GM_registerMenuCommand
  11. // @sandbox JavaScript
  12. // @match http://*/*
  13. // @match https://*/*
  14. // @match file:///*/*
  15. // ==/UserScript==
  16.  
  17. "use strict";
  18.  
  19. var scroll_window_percentage = 0.20; // from 0.0 to 1.0 (meaning 0% to 100%) how much WASD scrolls of the screen
  20. var space_scroll_by = 0.80; // how much space it scrolls of the screen - only for when "aggressive mode" is On
  21. var scroll_speed = "smooth"; // "smooth" / "instant" / "auto"
  22.  
  23. window.addEventListener("keydown", onkeydown, true); // before all others - change to false to make it work after all other listeners
  24.  
  25. if (typeof GM_registerMenuCommand !== "undefined")
  26. {
  27. GM_registerMenuCommand("WASD Scroll - Disable (this once)", disable, "w");
  28. GM_registerMenuCommand("WASD Scroll - toggle aggressive mode for this website", wasd_toggle, "a");
  29. }
  30.  
  31. if (typeof KeyEvent === "undefined")
  32. {
  33. var KeyEvent = {
  34. DOM_VK_A: 65,
  35. DOM_VK_D: 68,
  36. DOM_VK_S: 83,
  37. DOM_VK_W: 87,
  38. DOM_VK_SPACE: 32,
  39. };
  40. }
  41.  
  42. var inputs = ['input', 'select', 'button', 'textarea'];
  43.  
  44. function onkeydown (b)
  45. {
  46. let a = (window.event) ? b.keyCode : b.which;
  47. let by = window.innerHeight * scroll_window_percentage
  48. if (b.altKey || b.ctrlKey || b.metaKey)
  49. {
  50. return;
  51. }
  52.  
  53. let activeElement = document.activeElement;
  54. //console.warn(activeElement);
  55. //console.warn(b.target);
  56. if (activeElement && (inputs.indexOf(activeElement.tagName.toLowerCase()) !== -1 || activeElement.contentEditable === "true"))
  57. {
  58. return;
  59. }
  60. let whatWeScroll = window; //document.body; // or window ?
  61. let aggro = localStorage.getItem('WASD_AGGR') ? true : false;
  62. let everything = aggro ? document.querySelectorAll("*") : []; // lets scroll everything, we can't miss that way!
  63. for (let i = 0; i <= everything.length; i++)
  64. {
  65. switch (a)
  66. {
  67. case KeyEvent.DOM_VK_D:
  68. whatWeScroll.scrollBy({
  69. top: 0,
  70. left: by,
  71. behavior: scroll_speed
  72. });
  73. if(aggro) b.stopImmediatePropagation();
  74. break;
  75. case KeyEvent.DOM_VK_A:
  76. whatWeScroll.scrollBy({
  77. top: 0,
  78. left: by * -1,
  79. behavior: scroll_speed
  80. });
  81. if(aggro) b.stopImmediatePropagation();
  82. break;
  83. case KeyEvent.DOM_VK_W:
  84. whatWeScroll.scrollBy({
  85. top: by * -1,
  86. left: 0,
  87. behavior: scroll_speed
  88. });
  89. if(aggro) b.stopImmediatePropagation();
  90. break;
  91. case KeyEvent.DOM_VK_S:
  92. whatWeScroll.scrollBy({
  93. top: by,
  94. left: 0,
  95. behavior: scroll_speed
  96. });
  97. if(aggro) b.stopImmediatePropagation();
  98. break;
  99. case KeyEvent.DOM_VK_SPACE:
  100. if(!aggro) return;
  101. whatWeScroll.scrollBy({
  102. top: window.innerHeight * space_scroll_by * (b.shiftKey ? -1 : 1),
  103. left: 0,
  104. behavior: scroll_speed
  105. });
  106. b.preventDefault();
  107. b.stopImmediatePropagation();
  108. break;
  109. default:
  110. return;
  111. }
  112. whatWeScroll = everything[i];
  113. }
  114. }
  115.  
  116. function disable()
  117. {
  118. window.removeEventListener("keydown", onkeydown, false);
  119. console.warn("WASD Scroll disabled");
  120. }
  121.  
  122. function wasd_toggle()
  123. {
  124. console.warn("WASD aggressive mode toggled");
  125. if(localStorage.getItem('WASD_AGGR'))
  126. {
  127. localStorage.removeItem('WASD_AGGR');
  128. alert("OFF - now WASD scrolling is back to normal");
  129. }
  130. else
  131. {
  132. localStorage.setItem('WASD_AGGR', "1");
  133. alert("ON - now WASD scrolling will try to scroll EVERY element on the page and block origianl WASD-site hotkeys (if present), and own spacebar scroll will be added as well, this is remembered per-site (via cookies LocalStorage)");
  134. }
  135. }