I'm making a chrome extension which saves a list of URLs using the chrome.storage API. I'm storing this list in an array with the key "tabs". Basically, the refreshList()
function calls when the 'browser action' is called. This calls the getList()
function to get the array of URLs. The console.log()
calls from getList()
show that it is working fine (they print out the array object and "is array"), but in refreshList()
the console.log
prints our "undefined" and an error occurs saying Uncaught TypeError: Cannot read property 'length' of undefined
.
function refreshList()
{
var items = getList();
console.log(items);
var list = document.getElementById('pin-list');
for (var i = 0; i < items.length; i++)
{
var entry = document.createElement("li");
entry.appendChild(document.createTextNode(items[i]));
list.appendChild(entry);
}
}
function getList()
{
var itemsOut;
chrome.storage.local.get("tabs",function (items){
itemsOut=items.tabs;
console.log(itemsOut);
if (itemsOut instanceof Array)
{
console.log("is array");
return itemsOut;
}
else if (typeof(itemsOut)=="undefined")
{
console.log("is undefined");
return [];
}
console.log("is something else");
chrome.storage.local.set({"tabs":[itemsOut]},function(){});
return [itemsOut];
});
}
The refreshList()
function should print out an unordered list of the items in chrome.storage onto the browser action popup.
I'm thoroughly confused. I think it has something to do with passing by reference (or, if I understand it correctly, passing by value where the value is a reference) but I'm not sure how to fix it. I hope this was clear enough to understand... Thank you in advance :)