2012-05-02 93 views
0

我只是想在當前選項卡的URL發送到我的分機:ContentScript.js和Chrome擴展程序之間的通信

以下是我的manifest.json

{ 
"name": "DocUrlExtention", 
"version": "1.0", 
"manifest_version": 2, 
"description": "The first extension that I made.", 
"browser_action": { 
"default_icon": "icon.png", 
"default_popup": "popup.html" 
}, 
"content_scripts": [ 
{ 
    "matches": ["http://*/*"], 
    "js": ["contentscript.js"] 
} 
]} 

以下是我contentscript.js

chrome.extension.sendRequest({url: window.location.href}, function(response) { 
    console.log(response.farewell); 
}); 

以下是我popup.html

<!doctype html> 
<html> 
<head> 
<title>Getting Started Extension's Popup</title> 

<script> 
    chrome.extension.onRequest.addListener(
     function(request, sender, sendResponse) { 
     console.log(sender.tab ? 
        "from a content script:" + sender.tab.url : 
        "from the extension"); 
     }); 
</script> 

<!-- JavaScript and HTML must be in separate files for security. --> 
<!--<script src="popup.js"></script>--> 
</head> 
<body> 
<div id="mydiv">Doc Id:</div> 
</body> 
</html> 

我在控制檯中看不到任何東西。我是Chrome擴展程序的新手。

+0

人們發現我的相關答案很有用,它提供了一些有關Chrome擴展的信息,我學到了花費太多時間瞭解它們。 http://stackoverflow.com/questions/11920473/chrome-extension-background-page-html-not-working/19487091#19487091 –

回答

1

您的清單文件包含"manifest_version": 2,,它啓用Content Security Policy。默認情況下,Inline JavaScript will not be executed。並且,有no way to relax the CSP such that inline JavaScript is allowed

你有兩個選擇:

  1. 刪除"manifest_version": 2(其中禁用默認CSP)。
  2. 將嵌入式JavaScript移動到外部文件。

第二種方法建議,並還建議在你的代碼...

 
... 
<!-- JavaScript and HTML must be in separate files for security. --> 
<!--<script src="popup.js"></script>--> 
</head> 
... 

PS。用於彈出窗口的開發工具可通過右鍵單擊瀏覽器操作圖標上的打開,然後選擇最後一個選項"Inspect popup"

相關問題