首先,更改您的 options.xul 添加一个id
属性以供以后选择,它不需要oncommand
属性,因此我们可以将其删除。
<setting type="control" title="&passwordBtn;">
<button id="password" label="&passwordBtn;" />
</setting>
然后侦听首选项面板打开并手动将单击事件侦听器添加到按钮。下面是示例 bootstrap.js
const log = function() { dump(Array.slice(arguments).join(' ') + '\n'); }
const {classes: Cc, interfaces: Ci} = Components;
const OBS = Cc['@mozilla.org/observer-service;1']
.getService(Ci.nsIObserverService);
const myAddonId = 'my_addon_id' // same as install.rdf "<em:id>" tag value
let optionObserver = {
observe: function(subject, topic, data) {
if (topic !== 'addon-options-displayed' || data !== myAddonId) {
return;
}
let document = subject.QueryInterface(Ci.nsIDOMDocument);
let button = document.getElementById('password');
button.addEventListener('command', this.changePassword);
},
changePassword: function(event) {
log('password button clicked!');
// do your stuff...
}
}
let install = function(data, reason) {};
let uninstall = function(data, reason) {};
let startup = function(data, reason) {
OBS.addObserver(optionObserver, 'addon-options-displayed', false);
};
let shutdown = function(data, reason) {
OBS.removeObserver(optionObserver, 'addon-options-displayed', false);
};