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

Get content of some file in Firefox SDK main.js

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

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

myAddon/data/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/data/main.js

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

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

其次,我尝试只发送 template.html 的资源 URL,然后发送到 myAddon/data/main.js 中的 $.get(templateURL),但这没有用,因为 Firefox 可以不允许 resource:// 文件被 $.get() 处理。

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

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/data/main.js

var template = self.options.template;