可以打开位于资源添加项(数据目录)中的.html文件吗? [英] It is possible to open .html file located in add on resource (data directory)?
问题描述
我想要一个类似于结果页面"的附加组件,该附加组件自己的页面将被打开并显示注释和其他结果.理想情况下,我将使用位于附件的data
目录中的.html
文件.
I want to have something like a 'result page' for an add-on, an own page of the add-on that will be open and display annotations and other result things. Ideally I would use an .html
file located in the data
directory of the add-on.
window
模块是否可以打开扩展名自己的文件?
Does the window
module provide the ability to open extension's own files?
推荐答案
通常,您将要打开一个新选项卡,而不是一个窗口.从data
目录打开页面没有问题,只需使用
Usually you will want to open a new tab, not a window. There is no problem opening pages from your data
directory, you simply have to use the URL returned by self.data.url():
var tabs = require("sdk/tabs");
var self = require("sdk/self");
tabs.open({
url: self.data.url("result-page.html"),
inBackground: false, // This can also be: inNewWindow: true
});
但是该页面没有任何特殊特权.特别是,它将无法访问您的加载项数据,也无法与您的加载项交换消息.为此,您需要将内容脚本注入到新打开的选项卡中:
This page won't have any special privileges however. In particular, it won't have any access to your add-on's data and it won't be able to exchange messages with your add-on. For that you need to inject a content script into the newly open tab:
tabs.open({
url: self.data.url("result-page.html"),
inBackground: false,
onReady: function(tab)
{
tab.attach({
contentScriptFile: self.data.url("result-page.js"),
onMessage: function(message)
{
// Message from content script, send a response?
}
});
}
});
请参见 tab.attach ()和与内容脚本进行通信.
这篇关于可以打开位于资源添加项(数据目录)中的.html文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!