0

제 시나리오는 여러 개의 탭이 열려있는 브라우저를 열어 놓은 상태에서 탭에 메시지를 보내는 것입니다. 그런 다음 배경에서 원래 메시지를 보낸 탭으로 메시지를 보내려고합니다.메시지를 특정 탭으로 다시 전달하려면 어떻게합니까?

appAPI.message.toActiveTab을 사용해 보았지만 배경이 메시지를 보내기 전에 사용자가 탭을 변경할 수 있기 때문에 항상 작동하지는 않습니다. Crossrider로이를 달성 할 수있는 방법이 있습니까?

답변

1

요청의 일부로 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입니다

다음 예는 원리를 보여줍니다 직원]

관련 문제