我需要在 chrome 内容脚本中使用一些相对复杂的 html。它在 .js 文件中看起来很丑陋,所以我决定使用一些模板并选择 Handlebars。我知道,我可以预编译 Handlebars 模板,因此不会违反 CSP。但是我如何注入(inject)预编译模板以在内容脚本中使用它们?有没有比为每个预编译模板手动创建 XMLHttp 请求更优雅的方法?
最佳答案
刚刚想通了。我们只需要 - 预编译 handlebars 模板,然后将它们与 handlebars runtime 一起包含到 manifest.json 中,如下所示:
"content_scripts": [
{
"matches": ["https://targetsite.com/*"],
"js": ["app/content.js", "handlebars.min-latest.js", "precompiled_tmpls.js"],
"css": ["styles.css"]
}
]
然后我可以像这样在 content.js 中使用它:Handlebars.templates.[tempalate_name]()
无需额外代码
关于javascript - 在 chrome 内容脚本中使用 html 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41186038/