从JavaScript中的chrome.storage.local中删除以字符串开头的变量 [英] Remove variables starting with a string from chrome.storage.local in JavaScript

查看:503
本文介绍了从JavaScript中的chrome.storage.local中删除以字符串开头的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在chrome扩展程序中,我将一些变量存储在chrome.storage.local中,如下所示:chrome.storage.local.set({["name" + counter]: ""}, function() {});(非常感谢Xan关于ES6计算属性名称的回答

In a chrome extension, I store some variables in chrome.storage.local like this: chrome.storage.local.set({["name" + counter]: ""}, function() {}); (many thanks to Xan's answer regarding ES6 computed property names here), where counter is basically an incremented number.

当我不再需要它们时,如何使用chrome.storage.local.remove删除以前存储的所有以"name"开头的变量?

How can I use chrome.storage.local.remove to remove all the variables starting with "name" that were previously stored, when I don't need them anymore?

注意:我使用这种类型的存储("name0","name1",...),因为我无法将它们存储为数组并在chrome.storage.local中即时对其进行更新(我必须首先获取阵列,对其进行更新,然后将其存储回去,这不适用于大量数据.另外,在执行新扩展时,我不知道最大的counter值,因此无法在for循环中使用它.

Note: I use this type of storage ("name0", "name1", ...) as I can't store them as an array and update it on the fly in chrome.storage.local (I have to first get the array, update it, then store it back, which is unsuitable for large amounts of data). Also, in the case of a new extension execution, I don't know the maximum counter value, so I'm not able to use it within a for loop.

推荐答案

由于事情超出了我的能力范围,因此我尝试对此发表评论.但是,因为这样可以提供正确的格式,所以.

I was trying to put this on a comment since things are outside my capability. But since this gives proper formatting, so.

我查看了 chrome.storage ,如果您通过了null第一个参数,您将获得全部值.因此,通过提供回调,它变成

I looked at chrome.storage that if you pass null to first parameter, you'll get the entire values. So by provide a callback to it, it becomes

chrome.storage.local.get(null, function (items) {
    for (var key in items) {
        if (key.startsWith('name')) { // or key.includes or whatever
            chrome.storage.local.remove(key)
        }
    }
})

由于存储的项目可能足够大,因此将密钥存储在另一个密钥名称中也许会起作用. 包装函数示例

Since items stored might big enough, storing keys inside another key name would work perhaps. Example of wrapper functions

function set(key, value) {
    var collectionOfKeys = chrome.storage.local.get('collection_of_keys') || []

    collection_of_keys.push(key)

    chrome.storage.local.set('collection_of_keys', collection_of_keys)
    chrome.storage.local.set('name' + key, value)
}

要删除

chrome.storage.local.remove(chrome.storage.local.get('collection_of_keys'))

好吧,如果键的大小仍然很大,就足够愚蠢了.

Well, dumb enough if the size of keys is still big.

如果您将其保持顺序,也许可以跟踪它的长度.

In case you keep it in sequence, maybe track the length of it will do.

function set(key, value) {
    var collectionOfKeyLength = chrome.storage.local.get('collection_of_key_length') || 0

    chrome.storage.local.set('collection_of_key_length', collectionOfKeyLength + 1)
    chrome.storage.local.set('name' + key, value)
}

function remove () {
    for (var i = 0; i < chrome.storage.local.get('collection_of_key_length'); i++) {
        chrome.storage.local.remove('name' + i)
    }
}

这篇关于从JavaScript中的chrome.storage.local中删除以字符串开头的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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