我正在尝试从 bootstrap.js 文件调用 changePassword() 方法,但我不知道如何调用。 Bootstrap.js 位于项目文件夹的根目录,options.xul 位于 ./chrome/content/
<setting type="control" title="&passwordBtn;">
<button label="&passwordBtn;" oncommand="changePassword();"/>
</setting>
提前致谢! :)
最佳答案
首先,更改您的 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);
};
关于firefox - 如何在 options.xul 中使用 oncommand 来调用 bootstrap.js 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16109372/