admin管理员组文章数量:1323714
I want to inject some HTML into some websites with a Chrome extension, and it would be much nicer to use a template system like Mustache.js to do it. I can't figure out how to access the content of the template file, though. An example of what I'm trying to do:
content_script.js
var image = chrome.extension.getURL('logo.jpg');
var tb = Mustache.to_html(
chrome.extension.getURL('template.html'),
{
"imageURL": image,
"num": 5
}
);
$('body').prepend(tb);
template.html
<div id="topbar">
<img src="{{imageURL}}"></img>
{{num}}
</div>
The image shows up just fine, as you'd expect.
And therefore loading template.html
just returns the following string:
chrome-extension://badmldhplgbmcbkolbmkhbamjaanjejj/template.html
How do I get the content of my template file as a string?
Thanks to Boris Smus for the solution
content_script.js
var req = new XMLHttpRequest();
req.open("GET", chrome.extension.getURL('template.html'), true);
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
var image = chrome.extension.getURL('logo.jpg');
console.log('Rendering Mustache.js template...');
var tb = Mustache.to_html(
req.responseText,
{
"imageURL": image,
"num": 5
}
);
$('body').prepend(tb);
}
};
req.send(null);
I want to inject some HTML into some websites with a Chrome extension, and it would be much nicer to use a template system like Mustache.js to do it. I can't figure out how to access the content of the template file, though. An example of what I'm trying to do:
content_script.js
var image = chrome.extension.getURL('logo.jpg');
var tb = Mustache.to_html(
chrome.extension.getURL('template.html'),
{
"imageURL": image,
"num": 5
}
);
$('body').prepend(tb);
template.html
<div id="topbar">
<img src="{{imageURL}}"></img>
{{num}}
</div>
The image shows up just fine, as you'd expect.
And therefore loading template.html
just returns the following string:
chrome-extension://badmldhplgbmcbkolbmkhbamjaanjejj/template.html
How do I get the content of my template file as a string?
Thanks to Boris Smus for the solution
content_script.js
var req = new XMLHttpRequest();
req.open("GET", chrome.extension.getURL('template.html'), true);
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
var image = chrome.extension.getURL('logo.jpg');
console.log('Rendering Mustache.js template...');
var tb = Mustache.to_html(
req.responseText,
{
"imageURL": image,
"num": 5
}
);
$('body').prepend(tb);
}
};
req.send(null);
Share
Improve this question
edited Nov 3, 2011 at 21:52
daharon
asked Nov 3, 2011 at 21:26
daharondaharon
2,0102 gold badges18 silver badges20 bronze badges
1 Answer
Reset to default 9chrome.extension.getURL(file)
returns the absolute URL of the requested file, not its contents. You should do an XHR on the template to get its contents instead.
Alternatively, store the contents of your template in your HTML itself using something like <script id="templ" type="text/x-template">...</script>
, and then reference the contents of the template via document.getElementById('templ')
.
本文标签: javascriptRender Mustachejs templates in a Chrome extensionStack Overflow
版权声明:本文标题:javascript - Render Mustache.js templates in a Chrome extension - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742125095a2421902.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论