chrome.storage.sync.remove
方法用于从 Chrome 扩展的同步存储区域中删除特定的键值对。您可以通过传递要删除的键名或键名数组来调用此方法。
以下是使用 chrome.storage.sync.remove
的一些示例:
删除单个键
如果您想删除名为 "myKey"
的特定键,您可以这样做:
chrome.storage.sync.remove('myKey', function() {
console.log('键 "myKey" 已被删除');
});
删除多个键
如果您想一次删除多个键,可以传递一个包含多个键名的数组:
chrome.storage.sync.remove(['key1', 'key2'], function() {
console.log('键 "key1" 和 "key2" 已被删除');
});
使用回调处理错误
您还可以在回调函数中检查 chrome.runtime.lastError
来查看是否有任何错误:
chrome.storage.sync.remove('myKey', function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else {
console.log('键 "myKey" 已被删除');
}
});
请确保您的扩展具有 "storage"
权限,以便使用 chrome.storage.sync
API。这意味着您需要在扩展的 manifest.json
文件中包括以下内容:
{
"permissions": [
"storage"
]
}
通过使用 chrome.storage.sync.remove
,您可以精确控制要从扩展的同步存储区域中删除哪些数据,而不必清除所有内容。