Sunday, 12 January 2020

How to forbid keyboard shortcut stealing by websites in Firefox


Many websites, especially everything involving rich text editing (this site is guilty as well), steal keyboard shortcuts normally used to control Firefox and make them do something else instead. It is totally infuriating when I press something like Cmd-number, Cmd-L, Cmd-T, or Cmd-K and it doesn't do what I want it to. Can I make it stop?


Actually, it would probably be for the best if I could forbid stealing of all Cmd-* shortcuts. I've never seen them used for anything useful. Is it possible?



Answer



Thanks to Greasemonkey's new @run-at property, this is now possible!


I took inspiration from this script and this script to combine them into a Userscript that sucessfully intercepts the keyboard shortcuts Ctrl+T and Ctrl+S. I tested in in Firefox 17 ESR and Firefox 25.


// ==UserScript==
// @name Disable Ctrl+s and Ctrl+t interceptions
// @description Stop websites from highjacking keyboard shortcuts
//
// @run-at document-start
// @include *
// @grant none
// ==/UserScript==

// Keycode for 's' and 't'. Add more to disable other ctrl+X interceptions
keycodes = [83, 84];

(window.opera ? document.body : document).addEventListener('keydown', function(e) {
// alert(e.keyCode ); //uncomment to find more keyCodes
if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
e.cancelBubble = true;
e.stopImmediatePropagation();
// alert("Gotcha!"); //ucomment to check if it's seeing the combo
}
return false;
}, !window.opera);

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...