0
我的場景是我打開了一個瀏覽器,並打開了多個選項卡,並在選項卡上向後臺發送消息。然後從後臺我想發送一條消息回發送原始郵件的選項卡。如何將消息傳遞迴特定的選項卡?
我試過使用appAPI.message.toActiveTab,但並不總是工作,因爲用戶可以在後臺發送消息之前更改選項卡。 Crossrider有沒有辦法實現這一點?
我的場景是我打開了一個瀏覽器,並打開了多個選項卡,並在選項卡上向後臺發送消息。然後從後臺我想發送一條消息回發送原始郵件的選項卡。如何將消息傳遞迴特定的選項卡?
我試過使用appAPI.message.toActiveTab,但並不總是工作,因爲用戶可以在後臺發送消息之前更改選項卡。 Crossrider有沒有辦法實現這一點?
您可以通過將tabId作爲請求的一部分傳遞給後臺代碼來實現您的目標。在後臺中,向所有選項卡廣播響應並在消息中發送tabId,以便原始選項卡可以識別該消息。
下面的例子說明的原理是:
extension.js:
appAPI.ready(function($) {
// Listener to handle incoming messages
appAPI.message.addListener(function(msg) {
// Check if the message is intended for this tad
if (msg.tabId === appAPI.getTabId()) {
// Your code here
}
});
// Send message to background
appAPI.message.toBackground({
// Pass the tabId with the message
tabId: appAPI.getTabId(),
yourData: ...
});
});
background.js:
appAPI.ready(function($) {
// Listener to handle incoming messages
appAPI.message.addListener(function(msg) {
// Send message to all tabs
appAPI.message.toAllTabs({
// Pass the tabId with the message to identification
tabId: msg.tabId,
yourData: ...
});
});
});
[披露:我是Crossrider員工]