简体   繁体   English

如何在Manifest版本2中使用Backbone.js模板进行Chrome扩展

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

im trying to use templates for my backbone views. 我试图使用模板作为我的骨干视图。 I tried it with underscore.template to get it running. 我用underscore.template尝试了它以使其运行。 The problem is that since the manifest_version 2 of chrome extensions there are some security restrictions. 问题是,由于chrome扩展的manifest_version 2存在一些安全限制。 I think the reason is because inline blocks are not allowed anymore. 我认为原因是因为不再允许内联块。 In this little example i load a template and try to render it. 在这个小例子中,我加载一个模板并尝试渲染它。 I then get the error: 然后我得到错误:

Uncaught Error: Code generation from strings disallowed for this context. 未捕获的错误:对于此上下文不允许从字符串生成代码。

I tried it also with Handlebars.js and a template right into my html-file. 我也尝试使用Handlebars.js和我的html文件中的模板。 It works in a normal browser-window. 它适用于普通的浏览器窗口。 But it does not as a chrome extension. 但它不作为chrome扩展。

So how can i use templates with backbone.js in a chrome extension with manifest_version 2? 那么我如何在带有manifest_version 2的chrome扩展中使用带有backbone.js的模板?

With underscore (does not work): 使用下划线(不起作用):

define [
  'jquery'
  'backbone'
  'lib/facade'
  'text!templates/loginTemplate.js'
],
($, Backbone, facade, LoginTemplate) ->
  'use strict'
  class LoginView extends Backbone.View
    tagName: 'div'
    events: {

    }

    initialize: (options) ->
      @el = options.el

    render: ->
      console.log 'LoginView: render()'
      $(@el).html(_.template(LoginTemplate, {}))

with handlebars (does not work): 带把手(不起作用):

template in index.html: index.html中的模板:

<!-- templates -->
  <script id="loginTemplate" type="text/x-handlebars-template">
    <form class="form-horizontal">
      <fieldset>
        <legend>Login</legend>
        <div class="control-group">
          <label class="control-label" for="email">Email:</label>
          <div class="controls">
            <input type="text" class="input-xlarge" id="email" name="email">
          </div>
        </div>
        <div class="control-group">
          <label class="control-label" for="password">Passwort:</label>
          <div class="controls">
            <input type="password" class="input-xlarge" id="password" name="password">
          </div>
        </div>
        <div class="form-actions">
          <button type="submit" class="btn btn-primary">Login</button>
        </div>
      </fieldset>
    </form>
  </script>

and in my view: 在我看来:

define [
  'jquery'
  'backbone'
  'lib/facade'
],
($, Backbone, facade) ->
  'use strict'
  class LoginView extends Backbone.View
    tagName: 'div'    
    events: {

    }

    initialize: (options) ->
      @el = options.el

    render: ->
      console.log 'LoginView: render()', $("#loginTemplate")
      $(@el).html(Handlebars.compile($("#loginTemplate").html()))

Both Underscore and Handlebars templates convert strings to JavaScript functions; Underscore和Handlebars模板都将字符串转换为JavaScript函数; for example, Underscore does it like this : 例如, Underscore就是这样的

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; 所以它构建了一些JavaScript并使用new Function来构建一个函数; Handlebars probably does something similar. 把手可能会做类似的事情。 Apparently Chrome doesn't want you doing things like that in an extension. 显然,Chrome不希望您在扩展程序中执行此类操作。

You could precompile the template and then embed the function in your extension as a simple bit of JavaScript. 您可以预编译模板,然后将该函数作为一个简单的JavaScript嵌入扩展中。 For Underscore templates, you could look at the source property : 对于Underscore模板,您可以查看source属性

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. 因此,您可以在打包扩展时t = _.template(your_template)并将t.source文本作为JavaScript函数放在扩展中。

You can do similar things with Handlebars (see handlebars_assets for example). 您可以使用Handlebars执行类似的操作(例如,请参阅handlebars_assets )。

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. 它们都会使您的构建和打包过程变得复杂,但如果Chrome不希望您在扩展中构建功能,那么您无法做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM