获取Firefox SDK main.js中某些文件的内容

时间:2015-04-21 21:08:51

标签: javascript firefox firefox-addon firefox-addon-sdk

所以我正在开发一个Firefox插件,可以在任何网页的DOM中添加一些HTML。

这里的想法是我使用名为template.html的文件作为模板,该文件位于addon文件夹内的data文件夹中。 接下来,我想在变量中使用该template.html文件的内容,以便我可以将它附加到DOM。

myAddon /数据/ template.html:

<div>{{test}}</div>

myAddon / LIB / main.js:

var template = ... // This is where I want to fetch the contents of template.html.

pageMod.PageMod({
    include: "*", // Apply script at any page
    contentScriptFile: [data.url("jquery-1.11.2.min.js"), data.url("main.js")], // Include jQuery and myAddon/data/main.js
    onAttach: function(worker){ // when these files are attached, send the content of the html-file to the script.
        worker.port.emit("sendTemplate", template);
    }
});

myAddon /数据/ main.js

self.port.on("sendTemplate", function(template){
    // var template should be <div>{{test}}</div>
}

我在SDK中找到了“Panel”,但我不想将HTML文件显示为面板。

其次,我尝试只发送template.html的资源网址,然后发送到$.get(templateURL)中的myAddon/data/main.js,但这不起作用,因为Firefox不允许使用resource:// -file为$.get()'d。

如何确保将保存到插件的数据文件夹中的HTML文件的内容作为字符串提取并放入变量?

1 个答案:

答案 0 :(得分:5)

myAddon / LIB / main.js:

var template = require("sdk/self").data.load("template.html"); // This is where I want to fetch the contents of template.html.

pageMod.PageMod({
    include: "*", // Apply script at any page
    contentScriptFile: [data.url("jquery-1.11.2.min.js"), data.url("main.js")], // Include jQuery and myAddon/data/main.js
    contentScriptOptions: {
      template: template
    }
});

myAddon /数据/ main.js

var template = self.options.template;