Chrome扩展程序:访问存储对象的数组的长度

问题描述 投票:0回答:1

我试图访问存储arrayobjects的长度。当我将array打印到控制台时,它只显示1,当阵列中有一个object时,当我尝试访问undefined的长度时显示array

在我的弹出窗口中,我有一个文本字段,一旦输入文本并按下回车键,它就会将当前窗口中选项卡中的所有信息存储到storage中。信息存储在object中,object被添加到arraystorage中。然后,我根据array的长度在弹出窗口中创建按钮。

编辑:objectArr的类型为numbergroupObject的类型为object。在我将groupObject推到objectArr之前,我可以访问objectArr's length,我仍然可以在推后访问objectArr's length。然而,当我实际更新objectArr时,objectArr变成了number

background.js

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)});
        })
    }
}

popup.js

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);
        }
    })
}

background script console

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

popup script console

objectArr: 1
objectArr.length: undefined
javascript google-chrome-extension
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.