使用Mozilla SDK在Firefox扩展中使用面板切换按钮

时间:2014-05-28 17:39:26

标签: javascript firefox firefox-addon firefox-addon-sdk

我正在使用Mozilla SDK开发firefox扩展。情况是: 我想要一个切换/操作按钮来显示/隐藏扩展程序的面板。

我的代码:

// the panel
let panel = require("sdk/panel").Panel({
    // ...
    onHide: handleHide
});

// the button
let button = ToggleButton({
    // ...
    // will be executed, when user clicks the button
    onChange: handleChange
});

// event handlers
function handleChange(state) {
    // state.checked is always true
    if (???) {
        panel.show();
    } 
}

function handleHide() {
    // un-check the button
    button.state('window', {checked: false});
}

问题是,在 handleChange 内部,切换逻辑应该是,我无法判断面板是应该显示还是隐藏。 In the docs使用 state.checked 的示例,但由于此代码在我点击按钮时运行,因此 state.checked 始终为true。最后,切换不会以这种方式工作,因为单击“切换”按钮时面板永远不会被隐藏。

帮助很多,我已经尝试了很多东西.. mothings有效。 提前谢谢!

1 个答案:

答案 0 :(得分:2)

只需使用console.log查看状态内的内容:

{
  "disabled": false,
  "checked": false,
  "label": "the default label",
  "icon": "./icon.png",
  "id": "show-panel"
}

所以我们可以继续这样:

if(state.checked)
{
   myExt.show();
   return false;
} else {
   myExt.hide();
   return false;
}

在旧版本中,您需要返回false