gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:26:42 26 4
gpt4 key购买 nike

我正在尝试使用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com