如何使用GreaseMonkey为浏览器返回“/”键? [英] How can I use GreaseMonkey to give the browser back the "/" key?

查看:113
本文介绍了如何使用GreaseMonkey为浏览器返回“/”键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多网页似乎都使用 / 键进行搜索。我想禁用它,因为100%的时间我想用 / 在FireFox的页面中搜索。有没有办法可以用GreaseMonkey或dotjs覆盖这种行为?

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?

最好的公开示例是 https://www.github.com/ https://wiki.jenkins-ci.org/display/JENKINS/Issue+Tracking

推荐答案


  • 如果您设置 窗口上的addEventListener() Doc 并使用event capture,你将抓住99%的页面尝试做的事情。 (不计算像Flash这样的插件)

    • 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)

      您无法确定页面是否触发 keydown keyup keypress ,或某些组合,因此拦截 keydown (使用的典型事件)和 keyup 。但是,如果页面触发 keypress ,则阻止该事件可能需要这一种技术。这是因为< body> 上的 keypress 事件会触发Firefox的页内搜索,但是没有办法(重新)从javascript触发搜索(为了安全)。

      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.

      事件常数,如 DOM_VK_SLASH 很棒,但它们仍然只是Firefox版本。从这个问题的标签(dotjs)来看,目前还不清楚你是否也想在Chrome上工作。

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






      注意:此脚本似乎适用于您列出的两个网站,包括Chrome和Firefox。并且,它不会停止将 / 键入输入或textareas。但是,它很可能会导致某些网站无法在 / 键上触发其他事件。


      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.

      如果发生这种情况,请使用 zEvent.target.nodeName ==BODY等检查来限制 blockSlashKey()的操作。

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

      这篇关于如何使用GreaseMonkey为浏览器返回“/”键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆