Chrome extension sendRequest from async callback not working?

Posted by Eugene on Stack Overflow See other posts from Stack Overflow or by Eugene
Published on 2012-03-18T07:22:26Z Indexed on 2012/03/19 10:03 UTC
Read the original article Hit count: 241

Can't figure out what's wrong. onRequest not triggered on call from async callback method, the same request from content script works. The sample code below.

background.js
=============
...
makeAsyncRequest();
...
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  switch (request.id) {
    case "from_content_script":
      // This works
      console.log("from_content_script");
      sendResponse({}); // clean up
    break;
    case "from_async":
      // Not working!
      console.log("from_async");
      sendResponse({}); // clean up
    break;
  }
});

methods.js
==========
makeAsyncRequest = function() {
  ...
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      ...
      // It works
      console.log("makeAsyncRequest callback");
      chrome.extension.sendRequest({id: "from_async"}, function(response) {  });
    }
  }
  ...
};

UPDATE: manifest configuration file. Don't no what's wrong here.

{
  "name": "TestExt",
  "version": "0.0.1",
  "icons": {
    "48": "img/icon-48-green.gif"
  },
  "description": "write it later",
  "background_page": "background.html",
  "options_page": "options.html",
  "browser_action": {
    "default_title": "TestExt",
    "default_icon": "img/icon-48-green.gif"
  },
  "permissions": [
    "tabs", "http://*/*", "https://*/*", "file://*/*",
    "webNavigation"
  ]
}

© Stack Overflow or respective owner

Related posts about google-chrome-extension