0

I'm working on a pretty simple browser extension, but can't get the message passing to work: I can send messages, but the response is never delivered!

My code: Its mostly just a copy from the browserActions tutorial, contentscripts tutorial (manifest) and message-passing api definition.

manifest.json:

{
    "manifest_version":2,
  "name": "FUN SCRIPT",
  "version": "1",
  "description": "THIS IS SOME FUN SCRIPT",
  "browser_action": {
    "name": "Fun",
    "icons": ["icon.png"],
    "default_icon": "icon.png",
     "default_popup": "popup.html"
  },
  "content_scripts": [ {
    "js": [ "jquery.js", "background.js" ],
    "matches": [ "http://*/*", "https://*/*"]
  }],
}

background.js

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"});
  });

popup.js

document.addEventListener('DOMContentLoaded', function () {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console.log(response.farewell);
      });
    });
});

popup.html

<!doctype html>
<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <style>
      body {
        min-width: 357px;
        overflow-x: hidden;
      }

      img {
        margin: 5px;
        border: 2px solid black;
        vertical-align: middle;
        width: 75px;
        height: 75px;
      }
    </style>
    <script src="popup.js"></script>
  </head>
  <body>
  </body>
</html>
Nils Ziehn
  • 4,118
  • 6
  • 26
  • 40

1 Answers1

0

Since the message from your popup to the content script is sent whenever the popup is loaded, be sure to reload the page to ensure the content script is loaded. Otherwise, you could end up with the following error:

Could not establish connection. Receiving end does not exist.

That said, I tried what you provided, and it worked as is. Since your output is in popup.js, the response is in the console output of the popup. You can view it by right-clicking on your browser action icon, and selecting 'Inspect popup'.

Métoule
  • 13,062
  • 2
  • 56
  • 84
  • I don't think this is true, since the message is delivered to the content script ( and i get the console.log from the content script) but the response is not working. Also: the action is triggered when I click on the browserAction button. This is after the contentscript is loaded! – Nils Ziehn Oct 14 '13 at 13:39
  • OK, I tried further, and it does work with what you provided. I loaded the extension, inspected the popup and saw the `Receiving end does not exist` error. I reloaded a page to load the content script, then re-inspected the popup, and saw the expected console output. – Métoule Oct 14 '13 at 14:59
  • Very interesting.. I though it should work, too. But I dont see the console output from the response.. – Nils Ziehn Oct 14 '13 at 15:50
  • 1
    Maybe you're not looking in the right place? Since your output is in `popup.js`, the response is in the console output of the popup. You can view it by right-clicking on your browser action icon, and selecting 'Inspect popup'. – Métoule Oct 14 '13 at 17:56
  • Hey, that was the problem -,- Do you want to write this as an answer so I can vote for it? – Nils Ziehn Oct 15 '13 at 12:29
  • I edited my answer, with the feedback you provided, to ensure it really matched your issue. – Métoule Oct 15 '13 at 12:40