2012-02-11 68 views
4

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?

回答

2

如果您还没有解决您的问题(和其他人有类似的问题):

我也有类似的问题,并通过使用erikvold的ToolbarButton包解决它。一旦你安装了它和它的依赖关系,你的main.js文件中的东西应该可以工作。

var pan = require("panel").Panel({ 
    width: 400, 
    height: 600, 
    contentURL: "http://stackoverflow.com" 
    //maybe some more options here 
}); 
var button = require("toolbarbutton").ToolbarButton({ 
    id: "myButton", 
    label: "My Button", 
    image: data.url("someicon.png"), 
    panel: pan //This binds the panel to this toolbarbutton 
}); 

我希望你能找到一些方法来适应你的项目。

0

toolbarbutton.ToolbarButton({ ID: “MyAddon”, 标签: “我的附加组件”, 的ToolTipText: “我的附加组件工具提示”, 图像:data.url(为 “Logo.png”), 按需:功能(){

  var dictionary_panel = require("panel").Panel({ 
     width:630, 
     height:600, 
     contentURL: data.url("HtmlPage.html"), 
     contentScriptWhen: 'ready', 
     contentScriptFile: [data.url("style.css"),data.url("jquery-1.7.1.js"), 
        data.url("javascriptquezz.js"),data.url("create.js")]   

});

 dictionary_panel.show(); 

} });

相关问题