最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - chrome.storage.sync.remove array doesn't work - Stack Overflow

programmeradmin4浏览0评论

I am making a small Chrome extension. I would like to use chrome.storage but I can't get it to delete multiple items (array) from storage. Single item removal works.

function clearNotes(symbol)
{
    var toRemove = "{";

    chrome.storage.sync.get(function(Items) {
        $.each(Items, function(index, value) {
            toRemove += "'" + index + "',";         
        });
        if (toRemove.charAt(toRemove.length - 1) == ",") {
            toRemove = toRemove.slice(0,- 1);
        }
        toRemove = "}";
        alert(toRemove);
    });

    chrome.storage.sync.remove(toRemove, function(Items) {
        alert("removed");
        chrome.storage.sync.get( function(Items) {
            $.each(Items, function(index, value) {
                alert(index);           
            });
        });
    });
}; 

Nothing seems to break but the last loop that alerts out what is in the storage still shows all the values I am trying to delete.

I am making a small Chrome extension. I would like to use chrome.storage but I can't get it to delete multiple items (array) from storage. Single item removal works.

function clearNotes(symbol)
{
    var toRemove = "{";

    chrome.storage.sync.get(function(Items) {
        $.each(Items, function(index, value) {
            toRemove += "'" + index + "',";         
        });
        if (toRemove.charAt(toRemove.length - 1) == ",") {
            toRemove = toRemove.slice(0,- 1);
        }
        toRemove = "}";
        alert(toRemove);
    });

    chrome.storage.sync.remove(toRemove, function(Items) {
        alert("removed");
        chrome.storage.sync.get( function(Items) {
            $.each(Items, function(index, value) {
                alert(index);           
            });
        });
    });
}; 

Nothing seems to break but the last loop that alerts out what is in the storage still shows all the values I am trying to delete.

Share Improve this question edited Jul 29, 2013 at 18:38 Chris McFarland 6,1695 gold badges45 silver badges64 bronze badges asked Jul 29, 2013 at 14:59 americanslonamericanslon 4,2885 gold badges34 silver badges62 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 14

When you pass in a string to sync.remove, Chrome will attempt to remove the one single item whose key matches the input string. If you need to remove multiple items, use an array of key values.

Also, you should move your remove call to inside your get callback.

function clearNotes(symbol)
{
// CHANGE: array, not a string
var toRemove = [];

chrome.storage.sync.get( function(Items) {
    $.each(Items, function(index, value)
    {
        // CHANGE: add key to array
        toRemove.push(index);         
    });

    alert(toRemove);

    // CHANGE: now inside callback
    chrome.storage.sync.remove(toRemove, function(Items) {
        alert("removed");

        chrome.storage.sync.get( function(Items) {
            $.each(Items, function(index, value)
            {
                alert(index);           
            });
        });
    }); 
});

}; 

Slightly Slimmer and updated solution

chrome.storage.sync.get(null, (data) => {
    const keys = Object.keys(data).filter((x) => x.startsWith('<start-of-key>')); // Can replace `startsWith` with regex or any other string parison
    chrome.storage.sync.remove(keys);
});
发布评论

评论列表(0)

  1. 暂无评论