Mar*_*.H. 4 javascript firefox greasemonkey
从理论上讲,以下用户脚本(将在Firefox中与Greasemonkey一起使用)应捕获所有 Ctrl+t
所有网站上的所有事件,警告“ Gotcha!”,然后阻止该网站看到该Ctrl+t
事件。
但是,它仅适用于某些网站(Google,Stack Exchange),而不适用于其他网站。Usercade不起作用的一个示例是Codecademy(当代码编辑器具有焦点时),其中Ctr+t
始终切换光标旁边的两个字符。
我禁用了Flash,因此我认为这是可以使用JavaScript解决的问题。我可以在脚本中进行哪些更改,以真正防止事件冒泡到网站脚本中?
// ==UserScript==
// @name Disable Ctrl T interceptions
// @description Stop websites from highjacking keyboard shortcuts
//
// @run-at document-start
// @include *
// @grant none
// ==/UserScript==
// Keycode for 't'. Add more to disable other ctrl+X interceptions
keycodes = [84];
document.addEventListener('keydown', function(e) {
// alert(e.keyCode ); //uncomment to find more keyCodes
if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
e.cancelBubble = true;
e.stopImmediatePropagation();
alert("Gotcha!"); //comment this out for real world usage
}
return false;
});
Run Code Online (Sandbox Code Playgroud)
免责声明:我最初问了一个涉及超级用户这一主题的更广泛的问题。在尝试寻找答案时,我偶然发现了与脚本相关的特定问题。我不是要重复发表文章-我只是认为问题的这一部分可能更适合Stack Overflow,而不是Superuser。
我通过复制发现了另一个userscript两行固定它在这里。
这改变了document.addEventListener
行,更重要的是改变了最后一行。在Firefox上,!window.opera
评估为true
。这作为第三个选项参数传递给该addEventListener
函数,该函数设置useCapture
为true
。这样的结果是,该事件是在较早的“捕获阶段”而不是“冒泡阶段”中触发的,并阻止了网站的另一个eventListener完全“看到”该事件。
这是工作脚本:
// ==UserScript==
// @name Disable Ctrl T interceptions
// @description Stop websites from highjacking keyboard shortcuts
//
// @run-at document-start
// @include *
// @grant none
// ==/UserScript==
// Keycode for 't'. Add more to disable other ctrl+X interceptions
keycodes = [84];
(window.opera ? document.body : document).addEventListener('keydown', function(e) {
// alert(e.keyCode ); //uncomment to find more keyCodes
if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
e.cancelBubble = true;
e.stopImmediatePropagation();
// alert("Gotcha!"); //ucomment to check if it's seeing the combo
}
return false;
}, !window.opera);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1257 次 |
最近记录: |