Disable single key keyboard shortcuts

Stop websites from hijacking Firefox quick search key "/", and Escape key

  1. // ==UserScript==
  2. // @name Disable single key keyboard shortcuts
  3. // @description Stop websites from hijacking Firefox quick search key "/", and Escape key
  4. //
  5. // @run-at document-start
  6. // @include *
  7. // @grant none
  8. // @inject-into auto
  9. // @version 2019.12.31.1600
  10. // @namespace https://greasyfork.org/users/30
  11. // ==/UserScript==
  12. /* jshint esversion: 6 */
  13.  
  14. //https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
  15. const keyValues = new Set(['/','Escape']); // Check on https://keycode.info/
  16.  
  17. const preventKey = function(e){
  18. if (keyValues.has(e.key)){
  19. e.stopImmediatePropagation();
  20. }
  21. //console.log('Event: type: %s, key: %s, has: %s', e.type, e.key, keyValues.has(e.key));
  22. };
  23.  
  24. // Slash search on https://github.com/
  25. document.addEventListener('keydown', preventKey, {passive: false, capture: true});
  26. // Slash search on https://forum.manjaro.org/
  27. // `capture` for Slash search on https://phabricator.services.mozilla.com/
  28. document.addEventListener('keypress', preventKey, {passive: false, capture: true});
  29. // I need to find test case
  30. //document.addEventListener('keyup', preventKey);