admin管理员组文章数量:1328015
The following Userscript (to be used in Firefox with Greasemonkey) should, in theory, capture all Ctrl+t
events on all websites, alert "Gotcha!", and then prevent the website from seeing that Ctrl+t
event.
However, it only works on some sites (Google, Stack Exchange), but not on others. One example where the Userscript does not work is Codecademy (when the code editor has focus), where Ctr+t
always toggles the two characters next to the cursor.
I disabled Flash, so I think this is a problem that can be solved with JavaScript. What can I change in my script so that it really prevents events to bubble through to the website scripts?
// ==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 ); //unment to find more keyCodes
if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
e.cancelBubble = true;
e.stopImmediatePropagation();
alert("Gotcha!"); //ment this out for real world usage
}
return false;
});
Disclaimer: I originally asked a broader question covering this topic on superuser. While trying to find an answer, I stumbled on this specific, scripting related problem. I did not mean to double-post - I simply think that this part of the question might better fit to Stack Overflow than to Superuser.
The following Userscript (to be used in Firefox with Greasemonkey) should, in theory, capture all Ctrl+t
events on all websites, alert "Gotcha!", and then prevent the website from seeing that Ctrl+t
event.
However, it only works on some sites (Google, Stack Exchange), but not on others. One example where the Userscript does not work is Codecademy (when the code editor has focus), where Ctr+t
always toggles the two characters next to the cursor.
I disabled Flash, so I think this is a problem that can be solved with JavaScript. What can I change in my script so that it really prevents events to bubble through to the website scripts?
// ==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 ); //unment to find more keyCodes
if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
e.cancelBubble = true;
e.stopImmediatePropagation();
alert("Gotcha!"); //ment this out for real world usage
}
return false;
});
Disclaimer: I originally asked a broader question covering this topic on superuser. While trying to find an answer, I stumbled on this specific, scripting related problem. I did not mean to double-post - I simply think that this part of the question might better fit to Stack Overflow than to Superuser.
Share Improve this question edited Mar 20, 2017 at 10:18 CommunityBot 11 silver badge asked Nov 5, 2013 at 9:17 Martin J.H.Martin J.H. 2,2053 gold badges24 silver badges40 bronze badges1 Answer
Reset to default 10I fixed it by copying two lines from another userscript found here.
This changed the document.addEventListener
line and, more importantly, the last line. On Firefox, !window.opera
evaluates to true
. This is passed as the third option argument to the addEventListener
function, which sets useCapture
to true
. This has the effect that the event is triggered in the earlier "capturing phase", not in the "bubbling phase", and prevents that the other eventListener of the website "sees" the event at all.
Here is the working script:
// ==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 ); //unment to find more keyCodes
if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
e.cancelBubble = true;
e.stopImmediatePropagation();
// alert("Gotcha!"); //ument to check if it's seeing the bo
}
return false;
}, !window.opera);
本文标签: javascriptGreasemonkey script to intercept key pressesStack Overflow
版权声明:本文标题:javascript - Greasemonkey script to intercept key presses - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742244687a2439054.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论