1

My extension popup page gets data from a message from a sand-boxed page & scripts. I need to store this inside the chrome.storage, preferably using the storage.sync method. I'm getting this error: Uncaught TypeError: Cannot read property 'sync' of undefined

I already added the permissions for storage inside my manifest.json.

... "permissions": ["storage"], ... chrome.storage.sync undefined?

Google alsostates that no backgroud script is needed: https://developer.chrome.com/extensions/storage

Quoting: Your extension's content scripts can directly access user data without the need for a background page.

What am I overlooking? Thanks in advance!

function receiveMessage(event) {
        var data = event.data;
        saveToStorage(data);
    };

window.addEventListener("message", receiveMessage, false);

function saveToStorage(data)
{
    if (!data) {
        console.log('Error: No value specified. Nothing saved to storage.');
        return;
    }

    chrome.storage.sync.set({'data': data}, function() {
        console.log('Data saved to storage.');
    });
};
Community
  • 1
  • 1
  • 1
    Did you reload your extension after adding the new permission? – Rob W Feb 19 '15 at 11:14
  • Your code is correct and you should be able to use it. Though with `sync` storage, I would recommend checking `chrome.runtime.lastError` because of the quotas. – Xan Feb 19 '15 at 12:07
  • Also, see if you reloaded the extension **and** the page that contains your content script. – Xan Feb 19 '15 at 12:08
  • Reloaded everything and it works now. Sometimes it's as easy as that, and i'm as stupid as that :) –  Feb 19 '15 at 12:20
  • I hate the same issue, and I did reload everything. – Owen M Jul 16 '16 at 20:46

1 Answers1

0

Be sure to always reload your extension before testing something new. I never reloaded it before because it wasn't necessary, well until now :) Also chrome.runtime.lastError comes in very handy!