How can I use GreaseMonkey to return the "/" key to the browser?

It seems that many web pages use a key /to search. I would like to disable this, because in 100% of the cases I want to use /to search on a page in FireFox. Is there a way to override this behavior using GreaseMonkey or dotjs?

The best public example of this is https://www.github.com/ as well as https://wiki.jenkins-ci.org/display/JENKINS/Issue+Tracking

+5
source share
2 answers
  • addEventListener() Doc window " ", 99% , . ( , Flash)

  • , keydown, keyup, keypress , keydown ( ) keyup. keypress, . , keypress, <body>, Firefox, () javascript ( ).

    , - .

  • , DOM_VK_SLASH, , - Firefox. (dotjs) , Chrome.

, script :

// ==UserScript==
// @name        _Nuke the forward slash on select pages
// @include     https://github.com/*
// @include     https://wiki.jenkins-ci.org/*
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

//-- "true" tells the listener to use capture mode.
window.addEventListener ('keydown',  blockSlashKey, true);
window.addEventListener ('keyup',    blockSlashKey, true);
/*-- Don't block keypress on window or body, this blocks the default
    page-search, too.
window.addEventListener ('keypress', blockSlashKey, true);
*/

function blockSlashKey (zEvent) {
    var FORWARD_SLASH   = 191;  // For keydown and keyup
    var ASCII_SLASH     = 47;   // For keypress

    if (    zEvent.which === FORWARD_SLASH
        || (zEvent.which === ASCII_SLASH  &&  zEvent.type == "keypress")
    ) {
        zEvent.stopPropagation();
    }
}

. script, , , , Chrome, Firefox. / . , /.

, zEvent.target.nodeName == "BODY" blockSlashKey().

+3

Greasemonkey script Firefox

// ==UserScript==
// @name        Disable slash key on page
// @namespace   test
// @include     https://github.com/*
// @include     https://wiki.jenkins-ci.org/*
// @grant       none
// @version     1
// ==/UserScript==

document.addEventListener('keydown', function(event) {
    if (event.keyCode === event.DOM_VK_SLASH) {
        event.stopPropagation();
    }
}, true);
-1

All Articles