如何在编辑时禁用GreaseMonkey中的热键?

时间:2022-06-03 23:15:40

I'm using Ctrl+Left / Ctrl+Right in a GreaseMonkey script as a hotkey to turn back / forward pages. It seems to works fine, but I want to disable this behavior if I'm in a text edit area. I'm trying to use document.activeElement to get the page active element and test if it's an editable area, but it always returns "undefined".

我在GreaseMonkey脚本中使用Ctrl + Left / Ctrl + Right作为热键来转回/转发页面。它似乎工作正常,但如果我在文本编辑区域,我想禁用此行为。我正在尝试使用document.activeElement来获取页面活动元素并测试它是否是可编辑区域,但它始终返回“undefined”。

2 个解决方案

#1


2  

document.activeElement works for me in FF3 but the following also works

document.activeElement在FF3中适用于我,但以下也适用

(function() {

var myActiveElement;
document.onkeypress = function(event) {
    if ((myActiveElement || document.activeElement || {}).tagName != 'INPUT')
        // do your magic
};
if (!document.activeElement) {
    var elements = document.getElementsByTagName('input');
    for(var i=0; i<elements.length; i++) {
        elements[i].addEventListener('focus',function() {
            myActiveElement = this;
        },false);
        elements[i].addEventListener('blur',function() {
            myActiveElement = null;
        },false);
    }
}

})();

#2


0  

element.activeElement is part of HTML5 spec but is not supported by most browsers. It was first introduced by IE.

element.activeElement是HTML5规范的一部分,但大多数浏览器都不支持。它首先由IE推出。

#1


2  

document.activeElement works for me in FF3 but the following also works

document.activeElement在FF3中适用于我,但以下也适用

(function() {

var myActiveElement;
document.onkeypress = function(event) {
    if ((myActiveElement || document.activeElement || {}).tagName != 'INPUT')
        // do your magic
};
if (!document.activeElement) {
    var elements = document.getElementsByTagName('input');
    for(var i=0; i<elements.length; i++) {
        elements[i].addEventListener('focus',function() {
            myActiveElement = this;
        },false);
        elements[i].addEventListener('blur',function() {
            myActiveElement = null;
        },false);
    }
}

})();

#2


0  

element.activeElement is part of HTML5 spec but is not supported by most browsers. It was first introduced by IE.

element.activeElement是HTML5规范的一部分,但大多数浏览器都不支持。它首先由IE推出。