获取Firefox SDK main.js中某些文件的内容 [英] Get content of some file in Firefox SDK main.js

查看:134
本文介绍了获取Firefox SDK main.js中某些文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在开发一个Firefox插件,它为所有网页的DOM添加了一些HTML。



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


$ b myAddon / data / template.html:

  < DIV> {{测试}}< / DIV> 



myAddon / lib / main.js:

  var template = ... //这是我要获取template.html内容的地方。 
$ b $ pageMod.PageMod({
include:*,//在任何页面应用脚本
contentScriptFile:[data.url(jquery-1.11.2.min。 js),data.url(main.js)],//包括jQuery和myAddon / data / main.js
onAttach:function(worker){//当这些文件被附加时,发送内容
worker.port.emit(sendTemplate,template);
}
});

myAddon / data / main.js $ b

  self.port.on(sendTemplate,function(template){
// var template应该是< div> {{test} }< / div>
}

,但我不想将HTML文件显示为面板。其次,我试图发送template.html的资源URL,然后 $。get(templateURL) myAddon / data / main.js 中,但是这并不起作用,因为Firefox没有允许 resource:// -file为 $。get()'d。



如何确保保存到插件数据文件夹中的HTML文件的内容作为字符串提取并放入变量?
< myAddon / lib / main.js:

  var模板= require(sdk / self)。data.load(template.html); //这是我想要获取的地方nt的template.html。 
$ b $ pageMod.PageMod({
include:*,//在任何页面应用脚本
contentScriptFile:[data.url(jquery-1.11.2.min。 js),data.url(main.js)],//包括jQuery和myAddon / data / main.js
contentScriptOptions:{
template:template
}
});



myAddon / data / main.js



  var template = self.options.template; 


So I'm developing a Firefox addon that adds a bit of HTML to the DOM of any webpage.

The idea here is that I'm using a file called template.html as a template, that is located in the data folder inside the addon folder. Next, I would like to use the contents of that template.html file inside a variable, so that I can append it to the 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>
}

I've found "Panel" in the SDK, but I do not want to show the HTML-file as a panel.

Secondly, I've tried to just send the resource URL of template.html and then to $.get(templateURL) in myAddon/data/main.js, but that did not work because Firefox does not allow a resource://-file to be $.get()'d.

How can I make sure that the content of an HTML-file that is saved into the addon's data folder is fetched as a string and put into a variable?

解决方案

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;

这篇关于获取Firefox SDK main.js中某些文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆