[英]How can I use GreaseMonkey to give the browser back the “/” key?
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.
我想禁用它,因为100%的时间我想使用/在FireFox页面中搜索。 Is there a way I can override this behavior with GreaseMonkey or dotjs?
有没有办法可以用GreaseMonkey或dotjs覆盖这种行为?
The best public example of this is https://www.github.com/ , also https://wiki.jenkins-ci.org/display/JENKINS/Issue+Tracking 最好的公开示例是https://www.github.com/ ,也是https://wiki.jenkins-ci.org/display/JENKINS/Issue+Tracking
If you set addEventListener()
Doc on window
and use "event capture", you will catch 99% of what the page tries to do. 如果在
window
上设置addEventListener()
Doc并使用“事件捕获”,则将捕获页面尝试执行的99%。 (Not counting plugins like Flash) (不包括像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
. 您无法确定该页面是否触发了
keydown
, keyup
, keypress
或某些组合,因此拦截了keydown
(使用的典型事件)和keyup
。 But, if the page fires off of keypress
, then blocking the event may require this kind of technique . 但是,如果页面触发了
keypress
,那么阻止该事件可能需要这种技术 。 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). 这是因为
<body>
上的keypress
事件会触发Firefox的页内搜索,但是没有办法(重新)触发javascript中的搜索(为了安全起见)。
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. 像
DOM_VK_SLASH
这样的事件常量很棒,但它们仍然只有Firefox。 From this question's tags (dotjs), it is not clear if you mean for this to work on Chrome, too. 从这个问题的标签(dotjs),你不清楚你是否也想在Chrome上工作。
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. 注意:此脚本似乎适用于您列出的两个网站,包括Chrome和Firefox。 And, it will not stop the typing of / into inputs or textareas.
并且,它不会停止输入/输入或textareas。 But, there is a tiny chance that it might cause some sites to not fire other events on the / key.
但是,它很可能会导致某些网站无法触发/ key上的其他事件。
If that happens, then use checks like zEvent.target.nodeName == "BODY"
to restrict blockSlashKey()
's operation. 如果发生这种情况,那么使用
zEvent.target.nodeName == "BODY"
类的检查来限制blockSlashKey()
的操作。
This Greasemonkey script work on Firefox 这个Greasemonkey脚本适用于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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.