我正在尝试创建一个bootstrapped firefox扩展来获取当前标签页面和标题。问题是我想检查标签网址的网址中是否有“about:”。我建议的解决方案是检查require(“sdk / tabs”)。activeTab.url“使用浏览器native string.substr()javascript函数。是否可以在widget或ToolbarButton onClick方法上使用浏览器本机javasript函数?
exports.main = function(options) {
var base64 = require("sdk/base64");
var panel = require("sdk/panel").Panel({
width: 700,
height: 470,
onHide: function ()
{
panel.contentURL = "about:blank";
}
});
var tbb = require("toolbarbutton").ToolbarButton({
id: "extension-tbb-id",
label: "IFShare+",
image: "https://www.fasdfasd.es/factory/templates/templateforidkreader/favicon.ico",
panel: panel,
onClick: function()
{
windowPanel = require("sdk/tabs").activeTab;
title = windowPanel.title;
url = windowPanel.url;
// Is any posibility to do something like that ????
contentScript: "if("+url+".substring(0,5)=='about:'){"
{
url='';
title='';
}
contentScript: "}"
this.panel.contentURL = "https://www.fasdfasdf.es/factory/index2.php?option=com_idkreader&view=shareplus&task=window&Itemid=0&url="+base64.encode(url, "utf-8")+'&title='+base64.encode(title, "utf-8")+'&ref=ext';
},
});
// move the toolbar button to the navigation bar
if ("install" == options.loadReason) {
tbb.moveTo({toolbarID: "nav-bar"});
}
}
答案 0 :(得分:0)
我并不完全清楚你要完成的细节,但我认为你很接近。
要回答问题,请不要在内容脚本中访问变量。您可以做的最好的事情是使用the page-mod documentation中提到的内容脚本消息。
但是,要完成您想要的任务,您不需要这样做。您可以在onClick函数本身中执行您想要的操作,如此
function onClick()
{
windowPanel = require("sdk/tabs").activeTab;
title = windowPanel.title;
url = windowPanel.url;
if(url.substring(0,5)=='about:'){
{
url='';
title='';
} else {
//You can insert any content script stuff you want here
}
this.panel.contentURL = "https://www.fasdfasdf.es/factory/index2.php?option=com_idkreader&view=shareplus&task=window&Itemid=0&url="+base64.encode(url, "utf-8")+'&title='+base64.encode(title, "utf-8")+'&ref=ext';
}
如果你稍微改进一下你的问题,我会很乐意改进我的答案。