0

My scenario is that I has a browser open with multiple tabs open and on of the tabs sends a message to the background. Then from the background I want to send a message back to the tab that sent the original message.

I've tried using appAPI.message.toActiveTab but that doesn't always work as the user can change the tab before the background sends the message. Is there a way to achieve this with Crossrider?

1 Answers1

1

You can achieve your goal by passing the tabId as part of the request from the tab to the background code. In the background, broadcast the response to all tabs and send the tabId in the message so that the original tab can identify the message.

The following example demonstrates the principle:

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: ...
    });
  });
});

[Disclosure: I am a Crossrider employee]

Shlomo
  • 3,763
  • 11
  • 16