firefox - 如何在 options.xul 中使用 oncommand 来调用 bootstrap.js 中的函数?

标签 firefox button firefox-addon xul add-on

我正在尝试从 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/

相关文章:

javascript - 获取选项卡的消息管理器? (Firefox 附加 SDK)

internet-explorer - 是否可以检测用户何时切换标签?

javascript - 从多个选项卡同步写入 localStorage 时不一致

Javascript,检测文本框是否有焦点?

http - 如何解释 Firefox 和 Safari 对用户提供的包含多个 # 符号的 URI 的不同处理方式?哪个是 'right' ?

javascript - 需要图像按钮方面的帮助

firefox-addon - 如何在 Firefox 插件中以编程方式打开开发者工具的控制台?

javascript - Firefox 是否会阻止来自本地文件的 ajax 请求甚至被发送出去

c# - winform 在悬停时更改按钮的 BackgroundImage 属性

javascript - 谷歌地图按钮替换