firefox扩展按钮监听器

时间:2011-03-03 13:19:24

标签: firefox-addon addeventlistener

在我的扩展程序中,overlay.js包含以下事件:

var sto = 
{
   onLoad: function() {...},
   onMenuItemCommand: function(e) {...},
   onToolbarButtonCommand: function(e) {...},
};

window.addEventListener("load", function () { sto.onLoad(); }, false);

每次在加载的页面中单击按钮时,我都需要触发一个侦听器。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

嗯,我不确定这是否是您想要的,但您可以尝试在整个文档上进行事件委派:

var document_mouseup_lst    = EventListener.createEventListener();
doc.addEventListener("mouseup", document_mouseup_lst, false);
document_mouseup_lst.addEvent("mouseup", function click(e, callback, object){
    var element = e.target;

    if(element.tagName.toLowerCase() === 'button') {
        if (e.which == 1) { // left click
            // do whatever u want
        } else if (e.which == 2) { // middle click
            // do whatever u want
        }
    }

return false;
});

btw为了创建eventlistener(获取createEventlistener方法的EventListener对象)我使用了这个页面Ajaxian >> An alternative way to addEventListener

答案 1 :(得分:0)

我找到了解决方案 - 非常简单。好的,这里是来源:

https://developer.mozilla.org/en/Code_snippets/Interaction_between_privileged_and_non-privileged_pages

和修改后的代码:

var sto = 
{
   onLoad: function() {...},
   onMenuItemCommand: function(e) {...},
   onMouseClick: function(e) {...},
   onToolbarButtonCommand: function(e) {...},
};

window.addEventListener("load", function () { sto.onLoad(); }, false);
document.addEventListener("click", function(e) { sto.onMouseClick(e); }, false, true);