2014-10-11 75 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> 

我想......

相關問題