0
我正在練習Google Chrome擴展開發。我試圖從我popup.js發送消息給content.js文件,我收到以下錯誤:將popup.html發送至內容腳本的錯誤
「未捕獲的ReferenceError:未定義標籤」
這個錯誤出現在我的popup.js文件。
以下是我的代碼文件
manifst.json
{
"manifest_version": 2,
"name": "by-Surfers",
"description": "This extension is for practice...",
"version": "0.0.1",
"browser_action": {
"default_icon": "icon.png",
"default_title": "Click to speak with other surfers..",
"default_popup": "popup.html"
},
"background": {
"scripts": ["event.js"],
"persistent": false
},
"permissions": [
"tabs"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
]
}
Popup.html
<!DOCTYPE html>
<html>
<head>
<script src="jquery-2.1.1.min.js"></script>
<script src="popup.js"></script>
<style>
body {
min-width: 300px;
overflow-x: hidden;
}
</style>
</head>
<body>
It will start now....
<button id='btn'>click me</button>
</body>
</html>
popup.js
$(document).ready(function(){
$('#btn').click(function(){
StartTab();
});
});
function StartTab(){
chrome.tabs.sendMessage(tabs[0].id, {greeting: "OpenDialog"}, function(response) {
// console.log(response.farewell);
});
}
event.js
chrome.browserAction.setBadgeText({text: "CET"});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.type) {
case "dom-loaded":
alert(request.data.myProperty);
break;
}
return true;
});
function OpenContentScript(){
}
content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "OpenDialog"){
RunIt();
}
});
function Runit(){
alert("it has started...");
}
請幫助!
thnx man ...我犯了這樣一個愚蠢的錯誤! – Savaratkar