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

Koe*_*oen 4 javascript firefox firefox-addon firefox-addon-sdk

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

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

myAddon /数据/ template.html:

<div>{{test}}</div>
Run Code Online (Sandbox Code Playgroud)

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

myAddon /数据/ main.js

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

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

其次,我试图只发送template.html的资源URL,然后$.get(templateURL)myAddon/data/main.js,但没有工作,因为Firefox不会允许resource://-file是$.get()"d.

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

eri*_*old 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
    }
});
Run Code Online (Sandbox Code Playgroud)

myAddon /数据/ main.js

var template = self.options.template;
Run Code Online (Sandbox Code Playgroud)