1

I have two Chrome extensions and in one I need to receive a message in a content script. The other extension is sending a message in its background page. I am following this question but it did not work.

I changed .extension to .runtime in the listener and it still does not work. Here's the code:

extension 1, contentscript.js (this is not being fired)

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
  console.log("contentscript");
  if(sender.id !== "iknbmfmkhcilpbkobjafdhaloffobdbe") 
    return;
  if (document.getElementById("status").innerHTML === "1")
    sendResponse({farewell: "goodbye"});
});

extension 2, background.js

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    chrome.tabs.sendMessage(tabs[0].id, {pdf: "You a pdf?"}, function(response) {alert(response.farewell);});  
  });
});
Community
  • 1
  • 1
Brent Moses
  • 243
  • 5
  • 14
  • Usually, the background page is loaded before a tab exists. So, when you put `chrome.tabs.query` in the background page, then the result is that no tabs are matched. What are you trying to achieve with message passing? – Rob W May 28 '14 at 15:38
  • Sorry. It is in a browser action so the tabs would be loaded at that point. I have tested it and it is sending the message out to the currently opened tab. It's not being received though. – Brent Moses May 28 '14 at 15:42
  • Remove `chrome.tabs.query({active: true, currentWindow: true}, function(tabs){` and `});`, and replace `tabs[0].id` with `tab.id`. And remove `if(sender.id !== "iknbmfmkhcilpbkobjafdhaloffobdbe") return;` – Rob W May 28 '14 at 15:44
  • See if [this answer](http://stackoverflow.com/questions/23895377/sending-message-from-a-background-script-to-a-content-script-then-to-a-injected/23895822#23895822) explains your problem. – Xan May 28 '14 at 15:47
  • That is not the issue though I will remove the tab query because it is not needed. The listener in the content script is not being called so the senders id would not be the issue either. – Brent Moses May 28 '14 at 15:47
  • Xan I don't think that is the issue. My content script would be injected before the browser action is clicked. – Brent Moses May 28 '14 at 15:52

1 Answers1

1

You really should put in HUGE BOLD LETTERS that your question is about two separate extensions.

What you're trying to achieve is impossible, as chrome.tabs.sendMessage does not support cross-extension messaging. This effectively means that content scripts can only be messaged by parent extension.

To achieve what you want, you need Extension 1's background page to act like a proxy:

// Extension 1, background
chrome.runtime.onMessageExternal(message, sender, sendResponse){
  if(sender.id != extensionTwoId) return;
  if(message.tabId) {
    chrome.tabs.sendMessage(message.tabId, message, function(response){
      sendResponse(response);
    });
    return true; // Required if sendResponse will be called asynchronously
  } else {
    // It's not a message to be routed to a tab
  }
}

and

// Extension 2, background
chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.runtime.sendMessage(
    extensionOneId,
    {tabId: tab.id, pdf: "You a pdf?"},
    function(response) {alert(response.farewell);}
  );
});
Xan
  • 74,770
  • 16
  • 179
  • 206
  • You [never answered](http://stackoverflow.com/questions/23836198/how-to-access-localstorage-in-extension-background-page) why on Earth you need two separate extensions.. – Xan May 28 '14 at 16:20
  • The first is a native client extension and I was told by one of the senior programmers that you are not able to have browser/page actions along with them. Thanks for the answer. I thought it may have been an issue because of two extensions. I'll try your answer then upvote and check if it works. – Brent Moses May 28 '14 at 17:39