66

I'm trying to use chrome storage in an extension, via a content_script, but I keep failing on

Uncaught TypeError: Cannot read property 'sync' of undefined 

This is my code:

testChromeStorage();

function testChromeStorage() {  
    console.log("Saving");
    chrome.storage.sync.set({'value': theValue}, function() {
        message('Settings saved');
    });
    chrome.storage.sync.get("value", function (retVal) {
            console.log("Got it? " + retVal.value);
    });
}
Yossale
  • 14,165
  • 22
  • 82
  • 109

5 Answers5

137

You have to add the "storage" permission in your manifest.json file, i.e.:

...
  "permissions": [
    "storage"
  ],
...

For more information, see: https://developer.chrome.com/extensions/storage

sfarbota
  • 2,619
  • 1
  • 22
  • 30
  • 2
    Thanks, this is the actual correct answer I would believe .. No need for messaging – Obmerk Kronen Sep 08 '14 at 11:24
  • 11
    google ALWAYS does that...makes a lot useless help pages...why would'nt they menthion about storage permission here? :https://developer.chrome.com/extensions/options – Prosto Trader Dec 29 '14 at 12:58
  • True, it would be helpful on that page. I just sent them the suggestion as feedback by clicking on the "Report a bug" link on the bottom of the page. – sfarbota Dec 29 '14 at 23:13
  • I will note though, that although the `options` extension and the `storage` extension are often used together, they can actually be used independently from one another. And this requirement is mentioned in the link I posted in the answer. So I wouldn't be surprised if they decide not to add it to the page for the `options` extension. – sfarbota Dec 29 '14 at 23:16
  • How to specify "unlimitedStorage" permission instead of just "storage"? – Chetan Mar 10 '16 at 13:21
  • @Chetan Just substitute `"unlimitedStorage"` in place of the `"storage"` permission. But there are a couple limitations with this (taken from https://developer.chrome.com/extensions/declare_permissions): "This permission applies only to Web SQL Database and application cache... Also, it doesn't currently work with wildcard subdomains such as `http://*.example.com`." – sfarbota Mar 10 '16 at 13:49
  • I already tried with substitution with "unlimitedStorage" and then i get error "cannot read property 'sync' of undefined ". i am using chrome.storage.sync to set values. The moment i switch back to storage , then i don't get the error – Chetan Mar 10 '16 at 14:04
  • That's because you are still using `chrome.storage`, which is no longer in your permissions (and therefore undefined). As I mention in my previous comment, if you want to use `unlimitedStorage`, you will have to use either a Web SQL Database (which is deprecated by the way) or application cache. – sfarbota Mar 10 '16 at 14:31
  • @ProstoTrader 7 years later and it's still not there. – PeterG Sep 28 '21 at 18:22
15

RELOAD THE EXTENSION

I had the "permissions" key added in my manifest file but still I struggled to get this fixed.

After adding the permission:-

"permissions": [
    "storage"
 ]

Goto your extension using: chrome://extensions/ & click the Reload button:-

enter image description here

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
3

If someone was facing this issue on Firefox, please note, that it is not supported yet:

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage#Chrome_incompatibilities

For my purposes it was sufficient to replace chrome.storage.sync by chrome.storage.local.

Regarding the Firefox implementation state it might be worth to look also here from time to time:

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Browser_support_for_JavaScript_APIs#storage

kabeleced
  • 609
  • 6
  • 9
1

RESTART THE DEV SERVER

Despite adding the permission and reloading the extension, I sync was still undefined.

Turned out I needed to restart the dev server for the new manifest to picked up. Which makes sense since this file was not being watched. Doh!

Kildareflare
  • 4,590
  • 5
  • 51
  • 65
-1

See https://developer.chrome.com/extensions/content_scripts.html:

However, content scripts have some limitations. They cannot:

Use chrome. APIs (except for parts of chrome.extension)*

(emphasis added)

sowbug
  • 4,644
  • 22
  • 29
  • 2
    However, that page hasn't been updated for a long time. `chrome.runtime` is also available and `chrome.storage` will be available when the "storage" permission is declared. – 方 觉 Aug 02 '13 at 05:58