chrome.storage.sync.remove数组不起作用 [英] chrome.storage.sync.remove array doesn't work

查看:241
本文介绍了chrome.storage.sync.remove数组不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小的Chrome扩展程序.我想使用chrome.storage,但无法从存储中删除多个项目(数组).单项清除工作.

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.

推荐答案

当您将字符串传递给sync.remove时,Chrome会尝试删除其键与输入字符串匹配的单个项目 .如果需要删除多个项目,请使用键值数组.

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.

此外,您还应该将remove调用移至get回调内部.

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

}; 

这篇关于chrome.storage.sync.remove数组不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆