我试图访问存储array
的objects
的长度。当我将array
打印到控制台时,它只显示1
,当阵列中有一个object
时,当我尝试访问undefined
的长度时显示array
。
在我的弹出窗口中,我有一个文本字段,一旦输入文本并按下回车键,它就会将当前窗口中选项卡中的所有信息存储到storage
中。信息存储在object
中,object
被添加到array
的storage
中。然后,我根据array
的长度在弹出窗口中创建按钮。
编辑:objectArr
的类型为number
,groupObject
的类型为object
。在我将groupObject
推到objectArr
之前,我可以访问objectArr's length
,我仍然可以在推后访问objectArr's length
。然而,当我实际更新objectArr
时,objectArr
变成了number
。
chrome.runtime.onInstalled.addListener(onInstall);
/* runs on installation of extension */
function onInstall()
{
// objectArr will store the group objects
var objectArr = new Array();
chrome.storage.local.set({"objectArr": objectArr});
// initializes groupCount to 0
chrome.storage.local.set({"groupCount": 0});
}
function storeTabs(storeTabs)
{
chrome.storage.local.get(["groupCount", "objectArr"], function(group)
{
// current count of groups
var groupCount = group.groupCount;
var promptUser = storeTabs;
var groupObject = {};
/* stores all of the tab's information into an object and then puts object into storage */
chrome.tabs.query({currentWindow: true}, function(tabs)
{
/* gets each tab's name and url from an array of tabs and stores them into arrays */
var tabNamesArr = [];
var tabUrlsArr = [];
var tabCount = 0;
for (; tabCount < tabs.length; tabCount++)
{
tabNamesArr[tabCount] = tabs[tabCount].title;
tabUrlsArr[tabCount] = tabs[tabCount].url;
}
/* initialize object content */
var groupName = "groupName" + groupCount;
groupObject[groupName] = promptUser;
var tabNames = "tabNames" + groupCount;
groupObject[tabNames] = tabNamesArr;
var tabUrls = "tabUrls" + groupCount;
groupObject[tabUrls] = tabUrlsArr;
console.log("before push, group.objectArr.length: " + group.objectArr.length);
var objectArr = group.objectArr.push(groupObject);
console.log("after push, group.objectArr.length: " + group.objectArr.length);
console.log("typeof group.objectArr: " + typeof group.objectArr);
console.log("typeof group.objectArr.push(groupObject): " + typeof objectArr);
// updates storage with new objectArr with groupObject
chrome.storage.local.set({"objectArr": objectArr});
/* prints everything in storage */
chrome.storage.local.get(null, function(items)
{
var allKeys = Object.keys(items);
console.log("storage: " + allKeys);
})
/* prints objectArr and objectArr.length */
chrome.storage.local.get("objectArr", function(group)
{
console.log("objectArr: " + group.objectArr);
console.log("objectArr.length: " + group.objectArr.length);
})
// set-up for next group so last group isn't overwritten
chrome.storage.local.set({"groupCount": (groupCount + 1)});
})
}
}
document.addEventListener("DOMContentLoaded", function()
{
var storeTabs = document.getElementById("storeTabs");
storeTabs.addEventListener("keyup", function(enterKey)
{
if (enterKey.keyCode == 13)
{
// get text from text field
var storeTabs = document.getElementById("storeTabs").value;
chrome.extension.getBackgroundPage().storeTabs(storeTabs);
}
})
}
displayButtons();
/* Display buttons for popup */
function displayButtons()
{
chrome.storage.local.get("objectArr", function(group)
{
var objectArr = group.objectArr;
console.log("objectArr: " + group.objectArr);
console.log("objectArr.length: " + objectArr.length);
for (var i = 0; i < objectArr.length; i++)
{
getStorage(i);
}
})
}
before push, group.objectArr.length: 0
after push, group.objectArr.length: 1
typeof group.objectArr: object
typeof group.objectArr.push(groupObject): number
storage: groupCount,objectArr
objectArr: 1
objectArr.length: undefined
objectArr: 1
objectArr.length: undefined
我认为这是错误的。
console.log("before push, group.objectArr.length: " + group.objectArr.length);
// var objectArr = group.objectArr.push(groupObject); assinged 1...
group.objectArr.push(groupObject);
var objectArr = group.objectArr;
console.log("after push, group.objectArr.length: " + group.objectArr.length);
console.log("typeof group.objectArr: " + typeof group.objectArr);
console.log("typeof group.objectArr.push(groupObject): " + typeof objectArr);