2013-10-05 22 views
0

我正在創建一個Chrome擴展,我遇到了這個非常惱人的問題。我想從我的內容腳本發送一條消息到後臺併發送回應。chrome.runtime.sendMessage不工作

Port: Could not establish connection. Receiving end does not exist

我目前使用此代碼:

我不斷收到此錯誤。

背景:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ 
    sendResponse("got it!"); 
}); 

內容腳本:

chrome.runtime.sendMessage(<extension id>, "test message", function(response){ 
    console.log(response); 
}); 

我使用的Chrome版本30.0.1599.69米

在內容腳本

回答

0

,在這裏我沒有「擴展ID」,只是消息和回調函數,所以它的默認發送郵件到此延伸,而在谷歌瀏覽器的任何其它分機:

chrome.runtime.sendMessage({greeting: "removeCookie"}, function(response) { 
     console.log(response.farewell); 
    }); 

在後臺腳本中:

chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){ 

     if (message.greeting == "removeCookie"){ 
        //remove cookie 
        //... 

       sendResponse({farewell:"cookie clean"}); 

     } 

});