penera
penera

Reputation: 1

How do I use data out of chrome.storage.get function?

I'm trying to grab data from chrome extension storage, but I can use them only in this function.

var help = new Array();

 chrome.storage.local.get(null,  function(storage){   
 //get data from extension storage    
 help = storage;
            console.log(storage);
     });

console.log(help);  // empty

Result in console:

content.js:1 content script running  
content.js:11 []  
content.js:8 {/in/%E5%BF%97%E9%B9%8F-%E6%99%8F-013799151/: "link", /in/adam-  
isaacs-690506ab/: "link", /in/alex-campbell-brown-832a09a0/: "link",   
/in/alex-davies-41513a90/: "link", /in/alex-dunne-688a71a8/: "link", …}

Async function has won. I wrote my code again and now function is called hundreds time, i can not do this in dirrefent way
code:

console.log("content script running");
var cards = document.getElementsByClassName("org-alumni-profile-card");
var searchText = "Connect";
function check(exi, cards) {
  chrome.storage.local.get(null, function(storage) {
    for (var key in storage) {
      if (storage[key] == "link" && key == exi) {
        cards.style.opacity = "0.3";
      }
    }
  });
}
for (var i = 0; i < cards.length; i++) {
  var ctd = cards[i].getElementsByClassName(
    "org-alumni-profile-card__link-text"
  );
  var msg = cards[i].getElementsByClassName(
    "org-alumni-profile-card__messaging-button-shrunk"
  );

  if (ctd.length < 1 || msg.length > 0) {
    cards[i].style.display = "none";
  } else {
    var exi = cards[i]
      .getElementsByClassName("org-alumni-profile-card__full-name-link")[0]
      .getAttribute("href");
    check(exi, cards[i]);
  }
}

SOLUTION of my problem I wanted to delete this topic, but I can not, so instead of doing that, I'll put here what I've done finally.

The code above is wrong becouse, it was taking a list of links from website and for each from them script was grabbing a data from a storage... Which was stupid of course. I didn't see a solution which was so easy: Put all your file's code in this function - it grabs data from storage just once. I'm so sorry for messing up this wonderfull forum with topic like this. Hope u'll forgive.

Upvotes: 0

Views: 132

Answers (1)

holmberd
holmberd

Reputation: 2609

help will return undefined because it is referencing a asynchronous function and not the return value of that function. The content from storage looks to be printed on content.js:8, i.e. line 8.

Upvotes: 1

Related Questions