Chrome有一些名为"Page Actions",我大致试图用Firefox Addon SDK/Jetpack复制该功能。可能比我迄今为止尝试的方法更好,我愿意接受建议。制作面板只适用于某些网址
使用tabs
,我可以侦听标签就绪并激活事件,如果标签的URL匹配,则应启用插件小部件;如果没有,则禁用。我已经到了可以在适当的时候更改图标的地步,但是我想要禁用面板。
策略1:偷取点击事件,只显示面板,如果我们在右页;否则,忽略。问题是,according to the docs,手动显示面板导致它不被锚定,a bug that's not had much progress on it。
策略2:禁用时将contentURL
设置为null
。得到一个关于它不是URL的错误。
策略3:为禁用状态使用不同的HTML文档。在转到其他页面后,将panel.contentURL
设置为另一个URL不起作用?
下面的代码:
const widgets = require("widget");
const Panel = require("panel").Panel;
const tabs = require("tabs");
const data = require("self").data;
const prefs = require("simple-prefs").prefs;
var panel = Panel({
width: 480,
height: 640,
contentURL: data.url("panel.html"),
contentScriptFile: [data.url('jquery.min.js'), data.url('panel.js')],
onMessage: function (msg) { console.log(msg) }
});
var widget = widgets.Widget({
id: "icon",
label: "Export",
contentURL: data.url("icon.png"),
panel: panel
});
function enable() {
widget.contentURL = data.url('icon.png');
panel.contentURL = data.url("panel.html");
}
function disable() {
widget.contentURL = data.url('icon_disabled.png');
panel.contentURL = data.url("panel_disabled.html");
}
function on_change_tab(tab) {
console.log(tab.url);
if (/http:\/\/example.com\/.*/.exec(tab.url)) {
console.log('ENABLE');
enable();
} else {
console.log('DISABLE');
disable();
}
console.log(panel.contentURL);
}
tabs.on('ready', on_change_tab);
tabs.on('activate', on_change_tab);
相关,但应该有固定的问题? How to reload a widget popup panel with firefox addon sdk?