0

I'm trying to send a message to my extension's background page to get data from chrome.storage and return it to the content-script that asked for that data. I have to access chrome.storage through the background page because of compatibilty issues with Firefox.

The problem is that I'm using 2 async functions, chrome.storage and chrome.runtime.sendMessage, which means that undefined is always returned.

content-script:

chrome.runtime.sendMessage({
    getStorage: ["sync", "prefs"]
}, function (data) {
    var prefs = data.prefs;
    //returns error since data is undefined
});

background:

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

          var type = request.getStorage[0];
          var name = request.getStorage[1];

          if (type == "sync") {
              chrome.storage.sync.get(name, function (data) {
                  sendResponse(data);
              });
          } else {
                  chrome.storage.local.get(name, function (data) {
                  sendResponse(data);
              });
          }
       }
});
Daniel Herr
  • 19,083
  • 6
  • 44
  • 61

0 Answers0