javascriptgoogle-chrome-extensiongoogle-chrome-storage

how many keys can I retrieve with one call using chrome.storage.local.get?


I have written a simple chrome extension which is pretty much consisting of a popup, the content page and an html page (called 'report') that the extension itself will populate with data when clicking a specific button on the popup.

Long story short, there's a moment when I need to send 2 differents var from the content script to the reports page. I'm using chrome.storage.local.get for this purpose, but it seems that I'm able to retrieve only one of the 2 keys I'm passing.

Here's the code from the content script:

// first part of script where I define what rankingArray and logsArray are

function saveLogs() {
    chrome.storage.local.set({
        ranking: rankingArray
    }, function() {
        console.log("chrome.storage.local.set on rankingArray");
      });

    chrome.storage.local.set({
        logs: logsArray
    }, function() {
        console.log("chrome.storage.local.set on logsArray");
      });
}

and here's the code from the report.js page (acting, obviously, on report.html):

function getRecords() {
    chrome.storage.local.get(['ranking', 'logs'], function(result){
        var obj = result.ranking;
        var test = result.logs + "";
        console.log('logs:\n' + test);  // this one actually print on the console of report.html
        console.log('ranking:\n' + obj); // this one returns empty ('ranking:   ')
        // more stuff to do with obj and test
    });
    console.log("end of - @getRecords()");
}

so I checked the key names and everything several time but I can't figure out what's preventing this from working.
My doubt now is this: is it correct to use chrome.storage.local.get the way I'm doing? Can it retrieve 2 different set of values at the same time?


Solution

  • Question can be closed - as per my comment above the issue was caused by a mistake in the script logic. Basically I had my script reloading the content page before reaching the instructions were rankingArray was filled - once fixed that everything worked as expected.