Callback of tabs.create

I need to ask you how to solve this.

In content script I have this:

  if (mynamespace.inspector.data.defaults.target == "tab")
    {
    if ( !mynamespace.tabOpened )
      browser.runtime.sendMessage(
        { message:"open", 
          subject: "tab-action",
          module: "inspector",
          // sender:{id:tabs[0].id }
        },
        function response(package){
          switch (package.message){
            case "tab-action":
              if (package.subject == "opened")
                {
                switch(package.module){
                  case "inspector":
                  case "extractor":
                    mynamespace.tabOpened = true;
                    mynamespace.tabID = package.sender.id;  
                  break;
                  }
                }
              else
              if (package.subject == "closed")
                {
                switch(package.module){
                  case "inspector":
                  case "extractor":
                    mynamespace.tabOpened = false;
                    mynamespace.tabID = null;  
                  break;
                  }
                }
            break;
            }
        return true;
        }
        ); // sendMessage

 
    if (mynamespace.tabOpened)
        browser.runtime.sendMessage(
        { message:"add", 
          subject: "innerhtml",
          innerhtml: innnerhtml,
          target: { tab: { id: mynamespace.tabID } }
          // sender:{id:tabs[0].id }
        });
    return true;
    }

In background script I have event handler:

chrome.runtime.onMessage.addListener(
function(package, sender, sendResponse){
    switch (package.message){
      case "open":
        if (package.subject == "tab")
          {
          var url = null;
          
          switch (package.module)
            {
            case "inspector":
              if (!url) 
                url = chrome.extension.getURL("pages/inspector.html")
            case "extractor":
              if (!url) 
                url = chrome.extension.getURL("pages/extractor.html")
              chrome.tabs.create(
                {
                  url: url,
                  active:false
                },
                function(tab) {        
                  var package = { 
                      message: "opened",
                      subject: "tab",
                      id: tab.id
                     };
                  chrome.tabs.sendMessage(
                    sender.tab.id,    // tab id
                    package          // message
                    );

                  browser.windows.update(
                    sender.tab.id,
                    {focused: true}
                    // , function(window) {...}
                  )
                  
                  chrome.tabs.onRemoved.addListener(
                      function( tabId, removeInfo )                    
                      {
                      var package = { 
                          message: "closed",
                          subject: "tab",
                          id: tabId
                         };
                      chrome.tabs.sendMessage(
                        senderTab.id,    // tab id
                        package          // message
                        );
                      }
                    );
                }
              );
              return false;
            break;
            }

It should prevent to open new tab multiple times but the callback response is never fired.
In the tabs.create callback I send message, that makes sense, because the response should be sent when the the moment when tab is created not when the request to open the tab is made. What should I change to make it working correctly?

Can you be more specific about which part of the code doesn’t work? Is it that the callback on create isn’t called at all, or is it that the message in that callback handler isn’t being sent?

This problem is solved. But I do not remember what I did to fix it. Maybe the problem was that I created a C.S. file called addListeners.js where is

chrome.runtime.onMessage.addListener(
function(package){

if (package.command)
  {
  switch(package.command){ ... }
  }

else
if (package.message)
{
switch(package.message){ … }
}

and I did not add the case "particular_message"
to the switch. Which must call specific listener/control function … Yeah I remember I had to add “opened” and “closed” cases. Nevetherless I changed the name of the message later to “tab-action” with “opened” and “closed” subject.

I solved the problem when I have stopped to use the response here because it couldn"t work.

The Content Script intercepts the message like this:

mynamespace.extractor.listener = 
function (package){
  if ( package.message )
  switch (package.message){
/* Notice: it ain't possible to send
   "open tab" package request information 
   via tabs.create callback
*/ 
case "tab-action":
  if (package.subject == "opened")
    {
    mynamespace.extractor.tabOpened = true;
    mynamespace.extractor.tabID = package.id;
    }
  else
  if (package.subject == "closed")
    {
    mynamespace.extractor.tabOpened = false;
    mynamespace.extractor.tabID = null;  
    }