How do I prevent pages I visit from overriding selected Firefox shortcut keys?

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


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;
});

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


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.