gpt4 book ai didi

javascript - window.setinterval 不适用于我的 chrome 扩展程序

转载 作者:行者123 更新时间:2023-11-30 10:13:07 25 4
gpt4 key购买 nike

出于某种原因,我不断收到以下错误:拒绝将字符串评估为 JavaScript,因为“unsafe-eval”不是以下内容安全策略指令中允许的脚本源:“script-src 'self' chrome -扩展资源:”。 scripts.js:37。我不知道问题出在哪里。我正在尝试使用 window.setinterval 和 document.getElementById 将时间显示到 html 页面中。这是我的文件:

list .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'"


}

主页.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>

定时器.html

<html>
<head>

</head>
<body>
<h1 id="whatever"></h1>
</body>
<script src="scripts.js"></script>

</html>

脚本.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

关于javascript - window.setinterval 不适用于我的 chrome 扩展程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25345184/

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