2012-07-03 54 views
0

因此,我正在做這個擴展需要與另一個擴展進行通信,並且在得到大量矛盾信息之後,我還沒有得到任何結果,下面是目前使用的代碼:如何將郵件從擴展程序發送到另一個?

發送在擴展的HTML的一部分(options.html)

console.log("sending message..."); 
chrome.extension.sendRequest(receivingExtensionID, {txt: "sometext"}, function (res){ 
     console.log("response received"); 
     console.log("res: " + res); 
     try{ 
     console.log("res.txt: " + res.txt); 
     }catch(e){ 
     console.log("ERROR: cannot print res.txt because res is undefined."); 
     } 
}); 

console.log("message sent.") 

在其他擴展的background.html接收部分

chrome.extension.onRequestExternal.addListener(function(request, sender, sendResponse) { 
    sendResponse({txt: "someothertext"}); 
}); 

我得到以下錯誤:

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

我不知道除了細節,要說還有什麼說這個,當你在錯誤點擊調試器:

chromeHidden.Port.dispatchOnDisconnect 

我試圖用的sendMessage和onMessageExternal和相同的代碼我得到同樣的結果

請幫

+0

您是否在第一個擴展之前加載了第二個擴展? –

+0

是的,我不能相信這個!我以相反的順序加載它們,並且它立即生效!謝謝你! – DeadlyBacon

+0

我應該發佈一個答案,以便你可以接受它,或者你想刪除(看似微不足道的)問題嗎? –

回答

0

第一個擴展(發送消息)必須第二擴充(其接收後裝載信息)。這是顯而易見的:當它們以相反的順序加載時,發送者在創建接收者之前發送消息。

相關問題