How to use Templates with Backbone.js for Chrome Extension in Manifest Version 2 How to use Templates with Backbone.js for Chrome Extension in Manifest Version 2 google-chrome google-chrome

How to use Templates with Backbone.js for Chrome Extension in Manifest Version 2


Both Underscore and Handlebars templates convert strings to JavaScript functions; for example, Underscore does it like this:

source = "var __t,__p='',__j=Array.prototype.join," +  "print=function(){__p+=__j.call(arguments,'')};\n" +  source + "return __p;\n";var render = new Function(settings.variable || 'obj', '_', source);

so it builds some JavaScript and uses new Function to build a function; Handlebars probably does something similar. Apparently Chrome doesn't want you doing things like that in an extension.

You could precompile the template and then embed the function in your extension as a simple bit of JavaScript. For Underscore templates, you could look at the source property:

The source property is available on the compiled template function for easy precompilation.

<script>  JST.project = <%= _.template(jstText).source %>;</script>

So you can t = _.template(your_template) while packaging your extension and put the t.source text in your extension as a JavaScript function.

You can do similar things with Handlebars (see handlebars_assets for example).

Both of them complicate your build and packaging process but if Chrome doesn't want you building functions in an extension then there's not much you can do about it.