[英]window.setinterval isn't working with my chrome extension
出于某些原因,我始终收到以下错误:拒绝将字符串评估为JavaScript,因为在以下内容安全策略指令中不允许使用'unsafe-eval'脚本源:“ script-src'self'chrome-extension-资源:”。 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();
}
我很确定你的意思是:
window.setInterval(function() {displayTime(timeLeft);}, 1000);
通过函数引用。
您的原始调用调用displayTime
然后将其返回值( undefined
)传递给window.setInterval
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.