简体   繁体   中英

How can I check if a value exists in Google Chrome?

I am working on a Google Chrome extension, and I have a problem with the storage API. I want to check if a key exists, and if it does, delete it.

I have the following code:

"permissions": [
    "activeTab",
    "https://ajax.googleapis.com/",
    "storage"
  ],

Manifest.json:

chrome.storage.sync.get(this.name, function(items) {
  chrome.storage.remove(items);
});

However, I am getting the following error:

TypeError: Cannot read property 'sync' of undefined

That error seems to be relating to not having access to chrome.storage. Have you tried reloading your app after permissions have changed?

Also, I think you need to use the key name to remove the item by (not the result of your get to storage as shown below). You may also want to check the items exist before trying to remove them eg:

var keyName = this.name;
chrome.storage.sync.get(keyName, function(items) {
    if (items[keyName]) {
        chrome.storage.remove(keyName);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM