0

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.

penera
  • 1
  • 3
  • Someone wrote a reply on that, but then deleted. I know this is an asynchronous function. I have no right to use "chrome.browserAction.Listener" in this file. How can i do this in other way? – penera Feb 10 '18 at 15:05
  • I found similar question: [link](https://stackoverflow.com/questions/11688171/after-calling-chrome-tabs-query-the-results-are-not-available) – penera Feb 10 '18 at 16:19

1 Answers1

1

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.

holmberd
  • 2,393
  • 26
  • 30
  • I edited my code i question. This is how rly it looks like. And u are right, In console first written is console.log from out of function. – penera Feb 10 '18 at 15:56
  • `console.log(help)` will not print the value returned from storage because is called outside of the asynchronous flow. This means that `console.log(help)` is called, as you can see from the log output, before it has been assigned the value from the callback. Think of the function that you are passing to chrome.storage.local, that is a callback that says: When the data is retrieved from storage sometime in the future, we don't know when, then call this function with the returned value. While this is happening the code in your program doesn't halt/block, it continues executing the next lines. – holmberd Feb 10 '18 at 16:09
  • Understood. So i'm going to read about async programming, maybe I'll get a knowlage how to rewrite my code. Thanks for ur time! God bless u! – penera Feb 10 '18 at 16:12