0

So I want to respond to a request with data pulled from storage. Not sure why this code works but anyway any idea how I can remove the alert under the comment so that I will not have an empty pop up and this still work.

background.js:

var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
  if (request.command == "submit-chat") { 
    chrome.tabs.getSelected(null, function(tab) {
      chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
    });
  } else if (request.command == "set-emots"){
    var data = {};
    request.emots.forEach(function(entry) {
      var emot = JSON.parse(entry);
      data[emot.alt] = JSON.stringify(entry);
    });
    chrome.storage.sync.clear();
    chrome.storage.sync.set(data);
  } else if (request.command == "get-emots"){
    //Not sure why the alert is needed it is like it lets the sendResponce out of the function
    alert(chrome.storage.sync.get(null, function(result){
      if (!result) {
        getPage();
      } else {
        Object.keys(result).forEach(function(entry) {
          mData.push(result[entry]);
        });
      }
      sendResponse({emots: mData.join("|≈|")});
    }));
  } 
});

Where it is being called content.js

var emoticons = null;
window.onload = function(){
    jQuery.noConflict();
    chrome.runtime.sendMessage({command: "get-emots"}, function(response) {
        emoticons = response.emots;
        if (emoticons !== null) { 
            var input = document.getElementsByClassName("input")[0].innerHTML = "";
            jQuery(".input").load(chrome.extension.getURL("input.html"), function(){...});
            }
    });
}

UPDATE: So I tried this but still a no go

var getEmots = function(callback){
    chrome.storage.sync.get(null, function(result){
      if (!result) {
        getPage();
      } else {
        Object.keys(result).forEach(function(entry) {
          mData.push(result[entry]);
        });
      }
      typeof callback === 'function' && callback(mData);
    });
};

var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
  if (request.command == "submit-chat") { 
    chrome.tabs.getSelected(null, function(tab) {
      chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
    });
  } else if (request.command == "set-emots"){
    var data = {};
    request.emots.forEach(function(entry) {
      var emot = JSON.parse(entry);
      data[emot.alt] = JSON.stringify(entry);
    });
    chrome.storage.sync.clear();
    chrome.storage.sync.set(data);
  } else if (request.command == "get-emots"){
    getEmots(function(mData){
      console.log("Data: " + mData);
      sendResponse({emots: mData.join("|≈|")});
    });
  } 
});

UPDATE 2: Got it working here is the final for anybody else. Thanks everyone here is the final on Github

This is background.js

function getPage(){
  alert("You are going to be redirected to a page to get all hipchat emoticons. Please refresh that page then come back and refresh this page.");
  var win = window.open('http://hipchat-emoticons.nyh.name', '_blank');
  win.focus();
};

var getEmots = function(callback){
    chrome.storage.sync.get(null, function(result){
      if (!result) {
        getPage();
      } else {
        Object.keys(result).forEach(function(entry) {
          mData.push(result[entry]);
        });
      }
      typeof callback === 'function' && callback(mData);
    });
};

var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
  if (request.command == "submit-chat") { 
    chrome.tabs.getSelected(null, function(tab) {
      chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
    });
  } else if (request.command == "set-emots"){
    var data = {};
    request.emots.forEach(function(entry) {
      var emot = JSON.parse(entry);
      data[emot.alt] = JSON.stringify(entry);
    });
    chrome.storage.sync.clear();
    chrome.storage.sync.set(data);
  } else if (request.command == "get-emots"){
    getEmots(function(mData){ sendResponse({emots: mData.join("|≈|")}); });
    return true;
  } 
});
thealbinosmurf
  • 561
  • 2
  • 7
  • 13
  • 2
    Since Javascript is single-threaded, asynchronous callbacks don't run until the UI gets to the idle loop. Your call to `alert()` forces it to idle while waiting for the user to close the popup, which allows async code to run. – Barmar Feb 01 '14 at 14:38
  • If getPage() is asynchronous, @Barmar is correct. You will need to replace the alert with a promise or some sort of callback. – RustyToms Feb 01 '14 at 14:41
  • I tried using a call back but my attempt did not run the `sendResponse({emots: mData.join("|≈|")});` it did however run the `console.log("Data: " + mData);` – thealbinosmurf Feb 01 '14 at 15:51
  • 2
    See this answer: http://stackoverflow.com/a/20077854/1507998 You need to return `true` after your call to `getEmots`. – rsanchez Feb 01 '14 at 17:11

0 Answers0