简体   繁体   中英

Chrome extension: can't get message passing to work between background script and content script

I'm essentially trying to get persistent storage for my chrome extension by using the localStorage object from a background script.

Here's the setup:

I have one background script and one content script. The content script needs to get / set data from the extension's localStorage .

However, i'm stuck getting messages to send to the background script.

Here's the code in the background script:

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

And the content script:

setTimeout(function () {
  chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
}, 5000);

Ideally the background script should output from a content script: [some url] and the content script should output goodbye . Instead i get the following error:

Port: Could not establish connection. Receiving end does not exist. 

Any help / suggestions are appreciated!

Thanks :)

Edit: Manifest

{
  "manifest_version": 2,

  "name": "Helios",
  "description": "Helios chrome extension.",
  "version": "0.0.1",

  "permissions": [
    "https://secure.flickr.com/",
    "storage"
  ],

  "background": {
    "page": "background.html"
  },

  "browser_action": {
    "default_icon": {
      "19": "img/icon-19.png",
      "38": "img/icon-38.png"
    },
    "default_title": "Helios",
    "default_popup": "popup.html"
  },

  "content_scripts":[
    {
      "matches": ["*://*/*"],
      "css": ["css/main.css"],
      "js": [
        "js/vendor/jquery-1.9.1.js",
        "js/vendor/handlebars-1.0.0-rc.4.js",
        "js/vendor/ember-1.0.0-rc.7.js",
        "js/vendor/d3.v3.js",
        "js/vendor/socket.io.min.js",
        "js/templates.js",
        "js/main.js"
      ],
      "run_at": "document_end"
    }
  ],

  "web_accessible_resources": [
    "img/**",
    "fonts/**"
  ]
}

Edit: Chrome version

Google Chrome   29.0.1547.65 (Official Build 220622) 
OS  Mac OS X 
Blink   537.36 (@156661)
JavaScript  V8 3.19.18.19
Flash   11.8.800.170
User Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36

From onMessage documentation.

This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).

So your code should look like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM