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

时间:2018-09-05 22:56:42

标签: javascript google-chrome-extension

我正在尝试访问存储array的{​​{1}}的长度。当我将objects打印到控制台时,当数组中有一个array时,它仅显示1,而当我尝试访问{时,则显示object {1}}。

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

编辑:storage的类型为arrayobjectArr的类型为number。在将groupObject推送到object之前,我可以访问groupObject,并且在推送后仍可以访问objectArr。但是,当我实际更新objectArr's length时,objectArr's length变成了objectArr

background.js

objectArr

popup.js

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

1 个答案:

答案 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);