2014-10-11 46 views
1

我试图做一个非常简单的Chrome扩展,当你点击一个按钮时会发出警告,但它不起作用。我收到以下错误:这个简单的Chrome扩展有什么问题?

拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src”self'chrome-extension-resource:“。无论是“不安全内联”的关键字,散列(“SHA256 -...”),或者随机数(“随机数-...”)是必需的,以使内联执行。

任何人都可以帮忙吗?这就是我现在所拥有的:

popup.html

<html> 
    <body> 
     <input type = "button" id = "the_button" value = "My button" onclick = "sayHi()"></input> 
    </body> 
    <script> src = "popup.js" </script> 
</html> 

popup.js

function sayHi() { 
    alert("hi") 
} 

manifest.json的

{ 
    "manifest_version": 2, 
    "name": "Test", 
    "description": "Test Extension", 
    "version": "1.0", 

    "icons": { 
    "48": "icon.png" 
    }, 

    "permissions": [ 
    "http://*/*", 
    "https://*/*" 
    ], 

    "content_scripts": [{ 
    "matches": ["http://*/*", "http://*/*"], 
    "js": ["jquery.js", "popup.js"] 
    }], 

    "browser_action": { 
    "default_title": "This is a test", 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
    } 
} 

回答

3

的问题是在这里

<script> src = "popup.js" </script> 

要包含js文件使用

<script src="popup.js"></script> 

这个错误会发生当你试图把内联Javascr ipt在你的文件中。 Chrome扩展程序抱怨这一点。

如果你尝试

<script> alert("hello world"); </script> 

Google Chrome extension documentation

Inline JavaScript will not be executed. This restriction bans both inline blocks and inline event handlers (e.g. <button onclick="...">).

这也意味着你的内联事件处理程序是不会工作,你会得到同样的错误信息,你必须绑定在您的popup.js脚本中动态地代替事件:

document.getElementById("the_button").addEventListener("click", function() 
{ 
    // click code here 
}, false); 
+0

谢谢,但它似乎仍然不会o正在工作。我现在甚至没有出现错误。 – David 2014-10-11 22:12:49

+0

@David内联事件处理程序也不起作用,请参阅更新。您可以获取元素并动态绑定事件。 – BrunoLM 2014-10-11 22:15:47

4
<script> src = "popup.js" </script> 

应该

<script src="popup.js"></script> 

我想......

相关问题