简体   繁体   中英

Chrome Extension Message Passing with localStorage, value is undefined

I am trying to use Message Passing to pass some data from localStorage to my contentscript. Everything seems right but data is not being passed. I have set up console writes to see if the Message passing is being touched and it is not. This is my first time using JavaScript so the syntax could be wrong.

Here are all the files i'm working with in the extenstion:

My Code On Gist

It looks like you may be using sendRequest as if it is synchronous.

function genericOnClick(info, tab) {
  var sText = info.selectionText;
  var daddr = sText.split(' ').join('+');

  chrome.extension.sendRequest(
    {method: "getLocalStorage", key: "saddr"}, 
    function(response) {
      var saddr = response.data;
      var homeTxt = "http://maps.google.com/maps?" + 
          "saddr=" + saddr.split(' ').join('+') + "&daddr=";
      var url = homeTxt + daddr;
      chrome.tabs.create({url:url});
      console.log("item " + info.menuItemId + " was clicked");
      console.log("info: " + JSON.stringify(info));
      console.log("tab: " + JSON.stringify(tab));
    }
 );
}

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