2013-07-04 37 views
0

我正在做一個擴展鉻,我將要使用的一些功能將使用jQuery。我很難在網上找到有效的解決方案。Chrome擴展:清單和遠程谷歌API(jQuery)

core.js:

$(document).ready(function(){ 
init(); 
}); 

function init() 
{ 
    $(":button").click(function(){ 
     console.log("test"); 
     //more code here... 
    }); 
} 

extension.html:

<html> 
<head> 
<title>awesome</title> 
<script src="scripts/core.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script> 
</head> 
<body> 
<button id="getThem">GET!</button> 
</body> 
</html> 

在我的清單我有名稱,版本,manifest_version(2),說明,browser_action-> default_popup(推廣。 HTML),我不確定剩下的是什麼。你能幫我把這些基本要素拼湊起來嗎?

回答

1

由於Content Security policy,Chrome擴展進程中的頁面無法加載外部腳本。如果你是inspect the popup,控制檯會顯示這個錯誤。

最好的解決方案是將jQuery庫與你的擴展綁定 - 這沒有額外的成本,並且使你的彈出加載速度更快。

如果你真的從外部URL,relax the Content Security policy,e.g嵌入腳本..

"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'" 

這隻能爲HTTPS方案來完成。如果你想從一個http資源加載,你運氣不好(實際上有a work-around,但請不要使用它)。附件號碼:

+0

PS。以下是一個截屏視頻,演示如何識別和解決問題:http://stackoverflow.com/questions/17601615/the-chrome-extension-popup-is-not-working-click-events-are-not-handled/17612988 #17612988 –