出于某种原因,我不断收到以下错误:拒绝将字符串评估为JavaScript,因为' unsafe-eval'在以下内容安全策略指令中不是允许的脚本源:" script-src' self'铬扩展资源:&#34 ;. scripts.js中:37。我不知道问题是什么。我试图使用window.setinterval和document.getElementById在html页面中显示时间。这是我的文件:
的manifest.json
{
"manifest_version": 2,
"name": "The REAL Motivation",
"description": "This extension extension makes sure you are always motivated",
"version": "1.0",
"chrome_url_overrides": {
"newtab": "home.html"
},
"background":
{
"scripts": ["scripts.js"]
},
"permissions": [ "tabs" ],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
Home.html中
<html>
<head>
<title>I am a chrome extension</title>
<link rel="stylesheet" href="css/style.css" media="screen" type="text/css" />
<script src="scripts.js"></script>
</head>
<body bgcolor="#34495e">
<div id="bigdiv">
<div class="login-card" id="dateform">
<h1>Please Enter Your Birthday</h1><br>
<form>
<input type="date" name="user" id="hi">
<input type="submit" id="age" name="submit" class="login login-submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
Timer.html
<html>
<head>
</head>
<body>
<h1 id="whatever"></h1>
</body>
<script src="scripts.js"></script>
</html>
scripts.js中
function getAge()
{
var age = document.getElementById("hi").value;
if(age.getTime()>0)
{
localStorage.setItem("userage", age);
}
console.log('hi')
}
function showTime()
{
var personTime = new Date(localStorage.getItem("userage"));
// console.log(personTime);
var deathMonth = personTime.getMonth();
var deathDay = personTime.getDay();
var milliTime = personTime.getTime();
// console.log("Milliseconds on birthday:" + milliTime);
var bornYear = personTime.getFullYear();
var deathYear = bornYear + 78;
var deathDate = new Date(deathYear,deathMonth,deathDay);
var deathDateMilli = deathDate.getTime();
// console.log("Milliseconds Death: " + deathDateMilli);
var today = new Date();
var currentTime = today.getTime();
var timeLeft = (deathDateMilli - today.getTime());
console.log("Time Left: " + timeLeft);
// console.log("Death Date: " + deathDate);
// console.log("Death Year: " + deathYear);
// console.log("Death Month:" + deathMonth)
// console.log("Current time: " + currentTime)
window.setInterval(displayTime(timeLeft), 1000);
}
function displayTime(time)
{
var timer = document.getElementById("whatever");
timer.innerHTML = time;
}
window.onload = function () {
if(localStorage.getItem("userage") == null)
{
if(window.location.pathname != "/home.html"){
window.location = "home.html";
}
document.getElementById("age").onclick = getAge;
}
else
{
if(window.location.pathname != "/timer.html"){
window.location = "timer.html";
}
}
}
if(window.location.pathname == "/timer.html")
{
showTime();
}
答案 0 :(得分:4)
我很确定你的意思是:
window.setInterval(function() {displayTime(timeLeft);}, 1000);
传递函数引用。
您的原始调用已调用displayTime
,然后将其返回值(undefined
)传递给window.setInterval
。