2014-03-03 46 views
1

Firefox插件中是否有某些东西可以通过它我们可以注册一个回调,当通过单击左侧的x按钮关闭插件时,该回调会被调用?Firefox插件栏关闭事件

我需要的是,当用户使用x按钮关闭插件栏时,应该通知我在该栏上加载的扩展程序。现在发生的是,即使用户关闭了插件栏,它也没有关闭;相反,它只是隐藏。

如果我们可以通过回调得知用户点击了x按钮,那么我可以在扩展中听到这个。

回答

0

是的,先生绝对是:MutationObserver。

将其粘贴到浏览器环境中的暂存器文件中,然后当插件栏关闭并打开时,您将看到一条消息。

// select the target node 
var win = Services.wm.getMostRecentWindow('navigator:browser'); 
var target = win.document.querySelector('#addon-bar'); 

// create an observer instance 
var observer = new win.MutationObserver(function(mutations) { 
    mutations.forEach(function(mutation) { 
     if (mutation.attributeName == 'collapsed') { 
      Services.prompt.alert(null,'title','addon bar toggled it WAS = ' + mutation.oldValue); 
     } 
    });  
}); 

// configuration of the observer: 
var config = { attributes:true, attributeOldValue:true }; 

// pass in the target node, as well as the observer options 
observer.observe(target, config); 

// later, you can stop observing 
//observer.disconnect(); 
+0

这是相当矫枉过正。此外,突变事件的价格标签非常陡峭 - 如果它们在文档中的某处使用,则该文档中所有更改的性能都会显着下降。 –

+0

MutationEvents中不提倡 - [MDN:MutationEvents](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Mutation_events?redirectlocale=en-US&redirectslug=Web%2FGuide%2FAPI%2FDOM%2FEvents %2FMutation_events) MutationObserver被取代它,所以它没有性能问题。 [MDN:MutationObserver(https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) 佑学生becoems老师的老师一个答复可能不会再得到你哈哈:P – Noitidart

+0

啊,我不知何故忽略了你提出了突变观察者而不是突变事件。是的,当时的性能考虑并不起作用。尽管如此,还是有点矫枉过正...... –

0

最简单的方法是将command处理程序附加到相关按钮上。如果你的代码在浏览器窗口中运行,这将做到:

var closeButton = document.getElementById("addonbar-closebutton"); 
closeButton.addEventListener("command", function(event) { 
    // Add-on bar is being closed, do something 
}, false); 

注意,该代码被绑定到停止很快如从Firefox被删除的附加酒吧工作。