我有一个使用附加组件 SDK 制作的 Firefox 附加组件。
https://addons.mozilla.org/es/firefox/addon/board-notes/
为了添加工具栏按钮,我使用了一个流行的 SDK toolbarbutton.js 库:
https://github.com/erikvold/toolbarbutton-jplib/blob/master/lib/toolbarbutton.js
我只想在第一次安装时添加图标并且它可以工作,图标出现但重新启动浏览器后图标消失。用户必须使用右键单击按钮打开设置浏览器栏并将图标再次拖动到栏中。然后,如果他重新启动,图标会在其位置正确继续。
我想修复这个问题,因为大多数用户可能不知道他们可以使用设置选项恢复图标。
我已经测试了一些功能来检测图标是否不在他的位置再次移动,但是如果用户隐藏图标,当他重新启动时它会再次出现。这是 Firefox 政策所禁止的。
我会很感激任何帮助,我快疯了。
我使用的代码是这样的:
button = createButton(options);
if (options.loadReason == "install")
{
button.moveTo({
toolbarID: "nav-bar",
insertbefore: "home-button"
});
}
function createButton(options) {
return toolbarbutton.ToolbarButton({
id: "NoteBoard",
label: "Note Board",
tooltiptext: "Note Board",
image: data.url("noteboardMini.png"),
onCommand: function() {
openPopup();
},
onContext: (function () {
var installed = false;
return function (e, menupopup, _menuitem) {
//Install command event listener
if (!installed) {
menupopup.addEventListener("command", function (e) {
var link = e.originalTarget.value;
if (link) open(link.replace(/\?.*/ , ""));
});
installed = true;
}
var temp = (function (arr) {
arr.forEach(function (item, index) {
for (var i = index + 1; i < arr.length; i++) {
if (arr[i] && item.label == arr[i].label) {delete arr[index]}
}
});
return arr.filter(function (item){return item});
})(loggedins);
//remove old items
while (menupopup.firstChild) {
menupopup.removeChild(menupopup.firstChild)
}
function addChild (label, value) {
var item = _menuitem.cloneNode(true);
item.setAttribute("label", label);
item.setAttribute("value", value);
menupopup.appendChild(item);
}
if (temp.length) {
temp.forEach(function (obj) {
addChild(obj.label, obj.link);
});
}
else {
addChild(_("context"), "");
}
}
})()
});
}
最佳答案
您可以使用库 Toolbar Button Complete ,这是我的 toolbarbutton.js 分支。
使用库
您可以像使用原始 toolbarbutton.js 一样使用此库,但它还有更多选项和功能。
在你的 main.js 文件中:
button = createButton(options);
// On install moves button into the toolbar
if (options.loadReason == "install") {
button.moveTo({
toolbarID: "nav-bar",
insertbefore: "home-button",
forceMove: true
});
};
您可以找到图书馆的工作示例 here , 但有点过时了。
安装库
如果您在计算机上使用附加 SDK:
- 下载Toolbar Button Complete来自 github.com .
- 将它添加到您的
packages
目录。 (在您的 SDK 目录下或在您的插件目录下。)
如果您使用的是 Add-on Builder创建您的插件:
单击您的库文件夹旁边的加号按钮:
- 键入
工具栏按钮完成
。 单击“添加库”按钮:
更新库
图书馆托管于 github.com here .
如果您使用的是 Add-on Builder对于您的附加组件,您只需在有可用更新时单击小刷新按钮即可。
关于首次运行后 Firefox 插件工具栏按钮消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17145348/