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://www.github.com/, also https://wiki.jenkins-ci.org/display/JENKINS/Issue+Tracking https://wiki.jenkins-ci.org/display/JENKINS/Issue+Tracking
If you set addEventListener()Doc https://developer.mozilla.org/en-US/docs/DOM/EventTarget.addEventListener 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
,那么阻止该事件可能需要这种技术 https://stackoverflow.com/a/15283597/331508。这是因为keypress
事件,于<body>
,冒泡以触发 Firefox 的页内搜索,但无法从 javascript (重新)触发该搜索(出于安全考虑)。
幸运的是,您的两个样本点不需要任何严厉的措施。
事件常量,例如DOM_VK_SLASH
很棒,但它们仍然几乎仅限于 Firefox。从这个问题的标签(dotjs)来看,尚不清楚您的意思是否也适用于 Chrome。
把它们放在一起,这个完整的脚本就可以工作了:
// ==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.
如果发生这种情况,请使用类似的检查zEvent.target.nodeName == "BODY"
限制blockSlashKey()
的操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)