1

如何检查我的谷歌浏览器扩展中的存储是否为空?我尝试了很多可能性,但对我没有任何效果。

4

1 回答 1

2

这相当简单。

要获取当前使用的存储字节数,您可以使用 chrome.storage API。

如果您将扩展详细信息存储在名为“设置”的对象中,则可以通过以下方式检索正在使用的字节数。

function logBytes(bytes) {
    console.log(bytes);
}

// gets the number of bytes used in sync storage area
chrome.storage.sync.getBytesInUse(['settings'], logBytes);

// gets the number of bytes used in the local storage area
chrome.storage.local.getBytesInUse(['settings'], logBytes]);

getBytesInUse 参数采用字符串数组或单个字符串,每个字符串代表存储您希望为其计算字节数的数据的键。

如果您的扩展没有使用任何空间(空),您将使用零字节。

更多文档可以在Chrome Storage API中找到

扩展 wOxxOm 的评论,您可以通过执行以下操作来获取当前存储在存储中的对象:

function logBytes(bytes) {
    console.log(bytes);
}

function getSyncBytes(settings) {
    var keys = Object.keys(settings);
    chrome.storage.sync.getBytesInUse(keys, logBytes);
}

function getLocalBytes(settings) {
    var keys = Object.keys(settings);
    chrome.storage.local.getBytesInUse(keys, logBytes);
}

chrome.storage.sync.get(null, getSyncBytes);
chrome.storage.local.get(null, getLocalBytes);
于 2017-04-06T09:48:50.170 回答