我正在尝试访问存储array
的{{1}}的长度。当我将objects
打印到控制台时,当数组中有一个array
时,它仅显示1
,而当我尝试访问{时,则显示object
{1}}。
在我的弹出窗口中,我有一个文本字段,一旦输入了文本并按下了Enter键,它将把当前窗口中选项卡上的所有信息存储到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);
}
})
}
答案 0 :(得分:0)
我认为这是错误的。
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);