2014-11-05 103 views

回答

0

這是一個RTFM問題。

Messaging documentation(請注意,它提到的擴展,但它適用於應用程序):

除了在擴展發送不同組件之間的消息,您可以使用消息API與其他擴展進行通信。這使您可以公開其他擴展可以利用的公共API。

您需要使用chrome.runtime.sendMessage(使用應用程序ID)發送消息並使用chrome.runtime.onMessageExternal事件接收它們。如果需要,還可以建立長期連接。

// App 1 
var app2id = "abcdefghijklmnoabcdefhijklmnoab2"; 
chrome.runtime.onMessageExternal.addListener(
    // This should fire even if the app is not running, as long as it is 
    // included in the event page (background script) 
    function(request, sender, sendResponse) { 
    if(sender.id == app2id && request.data) { 
     // Use data passed 
     // Pass an answer with sendResponse() if needed 
    } 
    } 
); 

// App 2 
var app1id = "abcdefghijklmnoabcdefhijklmnoab1"; 
chrome.runtime.sendMessage(app1id, {data: /* some data */}, 
    function(response) { 
    if(response) { 
     // Installed and responded 
    } else { 
     // Could not connect; not installed 
     // Maybe inspect chrome.runtime.lastError 
    } 
    } 
); 
+0

感謝您的指導Xan。 – adw 2014-11-17 10:48:22

相關問題