2015-11-05 48 views
0

爲什麼當顯示擴展的彈出窗口時changeContentName函數未被調用?內容腳本在彈出頁面上不起作用

manifest.json的:

{ 
    "manifest_version": 2, 
    "name":"This is my first chrome addon!", 
    "description": "This extension just shows a text!", 
    "version":"1.0", 
    "icons":{ 
     "128":"icon.png", 
     "16":"icon.png" 
     }, 
    "browser_action":{ 
     "default_icon":"icon.png", 
     "default_popup":"popup.html", 
     "default_title":"Click here!" 
    }, 
    "content_scripts":[{ 
      "matches": ["<all_urls>"], 
      "js": ["JavaScript.js"] 
     } 
    ], 
    "permissions":[ 
     "activeTab", 
     "history", 
     "tabs" 
    ] 
} 

popup.html:

<!doctype html> 
<html> 
<head> 
    <title>My Awesome PopUp!</title> 
    <script src="JavaScript.js"/> 
    <style> 
     body{ 
      background-color: green; 
     } 
    </style> 
</head> 
<body> 
    <a id="1">Pasta la Pasta</a> 
</body> 
</html> 

JavaScript.js:

var changeContentName = function(id, value){ 
    document.getElementById(id).innerHTML = value; 
}; 

document.addEventListener('DOMContentLoaded', function(){ 
    changeContentName("1", "Hello"); 
}); 
+0

什麼不行? – TankorSmash

+0

我認爲你的文件應該命名爲「background.js」 – colecmc

回答

0

內容腳本不會autoinserted到擴展的頁面,如彈出頁面。

在這種情況下,我看不到任何內容腳本的需要。剛纔提到的腳本文件中popup.html

<head> 
    <script src="JavaScript.js"></script> 

並從manifest.json的"content_scripts"部分。

+0

謝謝你的回答。我已經在popup.html中引用了.js文件,正如您在我的帖子中看到的那樣。我刪除了清單中的content_scripts部分,但它仍然不起作用(如果我點擊擴展名圖標,我看不到任何文本)。 – binaryBigInt

+0

有什麼想法可以解決問題? – binaryBigInt

+0

@binaryBigInt,你做錯了。答案顯示了正確的語法。 – wOxxOm

相關問題