0

How to send tab data between tabs in chome extension?

I'm trying to use onMessage. Unsuccessfully.

I still don't understand how this message is sent.

Could anyone tell how can I do?

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
          //console.log(response.farewell);
          var bkg = chrome.extension.getBackgroundPage();
            bkg.console.log(response.farewell);
        });
      });
});

Receive on another tab

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
      console.log(sender.tab ?
                  "from a content script:" + sender.tab.url :
                  "from the extension");
      if (request.greeting == "hello")
        sendResponse({farewell: "goodbye"});
    });
marcelo.delta
  • 2,730
  • 5
  • 36
  • 71

1 Answers1

1

Resolved.

content.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        

      console.log(sender.tab ?
                  "from a content script:" + sender.tab.url :
                  "from the extension2");
      if (request.greeting == "hello")
        sendResponse({farewell: "goodbye"});
    });
marcelo.delta
  • 2,730
  • 5
  • 36
  • 71