jquery - Chrome 扩展程序 : Uncaught Error: Code generation from strings disallowed for this context

标签 jquery google-chrome google-chrome-extension template-engine

我正在尝试使用micro template engine在 chrome 扩展中出现以下错误:未捕获错误:此上下文不允许从字符串生成代码 在解析模板时。你能帮我解决这个问题吗?

Manifest.json

manifest.json:
{
  "name": "YYYY",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.ico",
    "default_popup": "popup.html"
  }
}

popup.html:

<!doctype html>
<html ng-csp ng-app>
  <head>
    <title>Getting Started Extension's Popup</title>
    <style>
      body {
        min-width:357px;
        overflow-x:hidden;
      }
    </style>

    <!-- JavaScript and HTML must be in separate files for security. -->
    <script src="jquery.min.js"></script>
    <script src="popup.js"></script>


  </head>
  <body>
      <ul>
            <li></li>
      </ul>

        <script id="userlisttemplate" type="text/html">
           <% for(var i=0; i < items.length; i++) { var item = items[i]; %>               

                <li> 
                <%= item.UserName%>

                </li>                                                     

           <% } %>
        </script>
  </body>
</html>

popup.js:

// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function () {
    var cache = {};

    this.tmpl = function tmpl(str, data) {
        // Figure out if we're getting a template, or if we need to
        // load the template - and be sure to cache the result.
        var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :

        // Generate a reusable function that will serve as a template
        // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +

        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +

        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");

        // Provide some basic currying to the user
        return data ? fn(data) : fn;
    };
})();


$.ajax({
    url: myurl,
    type: "GET",
    contentType: "application/json",
    success: function (response) {
        debugger;
        console.log(response);
        var data = response.data;
        var s = tmpl($('#userlisttemplate').html(), { items: data });
        $('body').append($(s));
    },
    error: function (jqXHR, textStatus, errorThrown) {
        $("#result").text(textStatus);
    }
});

最佳答案

此模板库无法在常规扩展程序页面中使用,因为它使用带有字符串的 new Function(),而 Chrome 的新内容安全政策现在不允许使用 list 版本创建的扩展程序2.see here

关于jquery - Chrome 扩展程序 : Uncaught Error: Code generation from strings disallowed for this context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11968234/

相关文章:

jQuery 克隆 jQueryUI slider 奇怪的行为

javascript - 如何选择DOM中除一个元素之外的所有元素?

google-chrome - 如何使用 clients2.google.com 下载 CRX?

google-chrome-extension - 仅在 "content_script"匹配时激活的浏览器操作

javascript - 更改所有td颜色

javascript - 使用 jquery/javascript 使用正则表达式解析文本

javascript - 在用户脚本中覆盖 Chrome 键盘快捷键

javascript - 修复 Firefox 在 setInterval 上抛出错误。

jquery - safari/chrome/opera 可以在上传过程中发出 ajax 请求吗?

iframe - Chrome扩展程序可以访问iframe吗?