6

Lots of web pages seem to use the / key for searching. I'd like to disable that because 100% of the time I want to use / to search in the page in FireFox. Is there a way I can override this behavior with GreaseMonkey or dotjs?

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

Frew Schmidt
  • 9,364
  • 16
  • 64
  • 86
  • Which "dotjs" are you using? There's more than one thing by that name. Are you trying to run this on Chrome, too? – Brock Adams May 09 '13 at 08:50

2 Answers2

4
  • If you set addEventListener()Doc on window and use "event capture", you will catch 99% of what the page tries to do. (Not counting plugins like Flash)

  • You can't be sure if the page fires off of keydown, keyup, keypress, or some combination, so intercept keydown (the typical event used) and keyup. But, if the page fires off of keypress, then blocking the event may require this kind of technique. This is because the keypress event, on <body>, bubbles up to trigger Firefox's in-page search, but there is no way to (re)trigger that search from javascript (for security).

    Fortunately, your two sample sites do not require any drastic measures.

  • Event constants, like DOM_VK_SLASH are great, but they are still pretty much Firefox-only. From this question's tags (dotjs), it is not clear if you mean for this to work on Chrome, too.

Putting it all together, this complete script works:

// ==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();
    }
}

Note: This script seems to work well on the two sites you listed, in both Chrome and Firefox. And, it will not stop the typing of / into inputs or textareas. But, there is a tiny chance that it might cause some sites to not fire other events on the / key.

If that happens, then use checks like zEvent.target.nodeName == "BODY" to restrict blockSlashKey()'s operation.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Ok, I'm a firefox user. I assumed that would be clear since / is the search button in FF, but in chrome the search button is CTRL-F. Anyway, sadly this doesn't seem to work for me at all. While it does correctly fire (I checked by adding an alert to the function body) it does not stop / from focussing the site specific search box on github. I'm going to restart FF and see if greasemonkey works better than dotjs, but I kinda doubt it will. (Worked for the wiki though!) – Frew Schmidt May 09 '13 at 13:39
  • Only seems to work in greasemonkey, but hey, it works everywhere! I'll file a bug with the dotjs guy but I am happy for now :) – Frew Schmidt May 09 '13 at 13:42
  • What most people call "dotjs" is a tool for Chrome. That's why I was asking. If you link to the "dotjs" you are using, maybe I can see a problem (no promises). Also, don't put alerts in event listeners in Greasemonkey. They tend to break the handler. Use `console.log()` instead. – Brock Adams May 09 '13 at 13:46
  • Yeah, I know it's usually a chrome addon. Sorry about that. The source is here: https://github.com/rlr/dotjs-addon – Frew Schmidt May 09 '13 at 13:48
  • Also, I found a site it doesn't seem to work on: https://issues.jenkins-ci.org/secure/Dashboard.jspa If you have ideas on how to make it work please let me know or update your post. – Frew Schmidt May 09 '13 at 13:49
  • Unfortunately, [the second jenkins site](https://issues.jenkins-ci.org/secure/Dashboard.jspa) is one of those that uses `keypress`. As stated in the answer, you need to use techniques like in [this answer](http://stackoverflow.com/a/15283597/331508) to surgically alter such keyboard-shortcut grabbers. ... **However**, in this case, if you don't care about any of the KB shortcuts, and you want that page to operate noticeably faster, just use *AdBlock Plus* to block `https://issues.jenkins-ci.org/rest/api/1.0/shortcuts/*/shortcuts.js`. – Brock Adams May 10 '13 at 02:10
  • If you don't want to block all of that page's ham-handed, kb shortcuts and you can't get `checkForBadJavascripts()` to work on that page, open a new GM question and I promise I'll give it a go. – Brock Adams May 10 '13 at 02:12
  • 1
    Thank you! To make it work with Violentmonkey on GitHub, see https://violentmonkey.github.io/2018/11/23/inject-into-context/ – woky Dec 23 '18 at 20:17
  • Thanks, @woky . Try just changing the `@grant` to `none` instead. Theoretically, that should make it work for Violentmonkey too, but I'll let you test it. – Brock Adams Dec 23 '18 at 20:24
-1

This Greasemonkey script work on 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);
muzuiget
  • 1,057
  • 1
  • 10
  • 11
  • Interesting, it doesn't seem to work with dotjs. I wonder if there is some weird loading issue where it doesn't get included early enough... – Frew Schmidt Apr 22 '13 at 13:23
  • Ok so this works on that wiki, but not on github? You have to be logged in to see the search part of github. – Frew Schmidt Apr 22 '13 at 14:24
  • Also work on github, do you notice the @include line? Maybe you should add more to activate the script. – muzuiget Apr 23 '13 at 02:51