我正在尝试创建闹钟/定时器扩展。以前从未使用过扩展并遇到此问题错误消息:
“拒绝执行内联事件处理程序,因为它违反了以下内容安全策略指令:“script-src 'self' https://apis.google.com ”。要么是 'unsafe-inline' 关键字,一个哈希('sha256- ...'),或者需要一个 nonce ('nonce-...') 来启用内联执行。”
它似乎是指我的 background.js 代码中的“onended”事件。
我发现这是一个类似的问题:
如何修复 chrome-extension 内联 JavaScript 调用错误?
由于我是新手,我不确定如何使修复工作。
我尝试将它添加到我的 manifest.json 文件中,但它没有做任何事情:"content_security_policy":"script-src 'self' https://apis.google.com ; object-src 'self'"
然后我阅读了其中的一些内容,但不确定如何实际获取代码的哈希值:https : //www.w3.org/TR/2015/CR-CSP2-20150721/#script-src-hash-usage
上面的链接说:
“您可以通过 openssl 程序在命令行上获取字符串的摘要”
我不确定 openssl 程序在哪里/什么。我将如何获得这个哈希?
manifest.json“content_security_policy”中的文本应该是什么样的:对于这个哈希信息?
chrome.runtime.onMessage.addListener(function(response){
myInterval = setInterval(function(){
var audio = new Audio("Gentle-wake-alarm-clock.mp3");
audio.setAttribute("onended",
"function(){alert('your ' + response + ' second alert has ended')}");
audio.play();
clearInterval(myInterval);
}, response * 1000);
})
我的 manifest.json 文件:
{
"name": "timer",
"version": "1.0",
"description": "Build a timer!",
"manifest_version": 2,
"permissions": ["storage"],
"options_page": "options.html",
"browser_action": {
"default_popup": "timerpopup.html",
"default_icon": "green alarm.png"
},
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_security_policy":"script-src 'self' https://apis.google.com; object-src 'self'"
}
不使用内联事件处理程序而使用它可能会更容易addEventListener
。您也可以使用setTimeout
代替setInterval
来避免调用clearInterval
:
chrome.runtime.onMessage.addListener(function(response) {
myTimeout = setTimeout(function() {
var audio = new Audio("Gentle-wake-alarm-clock.mp3");
audio.addEventListener('ended', function() {
alert('your ' + response + ' second alert has ended');
});
audio.play();
}, response * 1000);
});
如果您不在其他任何地方使用clearInterval
/ clearTimeout
,您也可以删除myTimeout =
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句