2

我正在尝试从 bootstrap.js 文件中调用 changePassword() 方法,但我无法弄清楚如何.. Bootstrap.js 位于项目文件夹的根目录中,而 options.xul 位于 ./chrome/content/

<setting type="control" title="&passwordBtn;">
<button label="&passwordBtn;" oncommand="changePassword();"/>
</setting>

提前致谢!:)

4

1 回答 1

3

首先,更改您的 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);
};
于 2013-04-19T18:09:48.790 回答