58

In Firefox, how can I prevent pages from overriding Firefox built-in keyboard shortcuts through Javascript on a per-key basis? Preferably on a per-site basis, too? The most frustrating override is the forward slash ('/') that's linked to "Find in page". Sites like Google search results, Twitter timelines, some wikis, and other pages steal the slash key for their own search boxes, which is completely wrong.

Since my rep lets me ask, edit, and answer questions, but not add comments, this is basically a duplicate of these other two questions that weren't properly answered:

How do a stop a website for overriding my keyboard short cuts

Firefox: don't allow websites to override the / (slash) key

4 Answers4

22

Building on edymtt's answer, I've created a userscript which disables only specific keyboard shortcuts. You can add more shortcuts to disable by adding keycodes to the keycodes array, or restrict which sites to apply it to by replacing the @include tag with one or more patterns.

Install using greasemonkey.

// ==UserScript==
// @name           Disable keyboard shortcuts
// @description    Stop websites from hijacking keyboard shortcuts
//
// @run-at         document-start
// @include        *
// @grant          none
// ==/UserScript==

keycodes = [191] // Keycode for '/', add more keycodes to disable other key captures

document.addEventListener('keydown', function(e) { // alert(e.keyCode); //uncomment to find out the keycode for any given key if (keycodes.indexOf(e.keyCode) != -1) { e.cancelBubble = true; e.stopImmediatePropagation(); } return false; });

msrd0
  • 187
MikeFHay
  • 2,754
21

Since Firefox 58 it is possible to disable keyboard shortcuts override per web site.

"Override Keyboard Shortcuts" and many other permissions are available in "Page Info -> Permissions" (under info icon in URL bar).

Firefox Permissions example for superuser.com

Keyboard override was introduced in Firefox #380637

8

With regard to Google and the Quick Find shortcut, you can install this Greasemonkey script:

http://userscripts-mirror.org/scripts/show/132237

As the description says, it "stops google from focusing the search input on every key press" -- in particular, if you press / with the keyboard focus outside the search box the Quick Find will appear, as it will on other web sites.

I have only installed it without touching the code, but I think it could be easily adapted to work with other sites and/or other shortcuts.

edymtt
  • 541
0

Here is a more general script - you can define any number of keypress events to disable.

https://greasyfork.org/en/scripts/5819-disable-website-keyboard-hooks

// ==UserScript==
// @name           Disable website keyboard hooks
// @description    Stop websites from hijacking keyboard shortcuts.
// @author         Isaac Levy
// @run-at         document-start
// @include        *
// @grant          none
// @version        0.0.1
// @namespace      https://isaacrlevy.com
// ==/UserScript==

var keycodes = [ // Add keycodes as desired, keep sorted.
    37, 38, 39, 40 // Arrow keys.
]

var meta_keycodes = [ // Disable these when meta key is pressed.
    70
];

// Don't change below this line.

var isMac = navigator.platform.toLowerCase().indexOf('mac') >= 0;

// Create a fast lookup.
// This saves work during normal typing. Maybe unnecessary.
var keycode_offset = keycodes[0];
var keycode_arr = Array(keycodes[keycodes.length - 1] - keycode_offset)
for (var i = 0, len = keycodes.length; i < len; i++) {
    keycode_arr[keycodes[i] - keycode_offset] = true;
}

document.addEventListener('keydown', function(e) {
    //console.log(e);
    if ((isMac && e.metaKey) || (!isMac && e.ctrlKey)) {
        if (meta_keycodes.indexOf(e.keyCode) >= 0) {
            e.stopImmediatePropagation();
        }
    } else if (keycode_arr[e.keyCode - keycode_offset]) {
        e.stopImmediatePropagation();
    }
    return false;
});
phuclv
  • 30,396
  • 15
  • 136
  • 260