Bruno Bernardino
Bruno Bernardino

Reputation: 496

QUOTA_BYTES_PER_ITEM error on storage.sync for Google Chrome Extension

tl;dr: Sync error about QUOTA_BYTES_PER_ITEM, but my item is small, need help figuring out what's wrong with it.

So I'm getting this error on a Google Chrome Extension that I don't think it makes much sense, since what I'm trying to save doesn't get bigger than 400 bytes (serialized, which is bigger than the same in a JSON string), and considering QUOTA_BYTES_PER_ITEM is 4096, it's far from that limit.

Either I'm calculating something wrong (most likely), or I'm doing something wrong (not improbable!).

You can see the code of my extension and install it from web store

The part that is important for this problem is the following (CoffeeScript):

# Object: Default settings
defaultSettings =
    type: 'single'
    chars: 14
    howmany: 1
    security:
        lowercase: true
        uppercase: true
        special: true
        punctuation: true
        readable: false

# Object: Current settings
paGenSettings = defaultSettings

# Function: Save Settings
saveSettings = (settings, notify) ->
    if validateSettings settings, true
        # Save settings using the Chrome extension storage API. Try sync, fallback to local
            window.chrome.storage.sync.set { settings: settings }, () ->
                if chrome.runtime.lastError && chrome.runtime.lastError.message && chrome.runtime.lastError.message.indexOf( 'MAX_WRITE_OPERATIONS_PER_HOUR' ) != -1
                    window.chrome.storage.local.set { settings: settings }, () ->
                        paGenSettings = settings

                        if notify
                            showNotification window.chrome.i18n.getMessage( 'settingsSaved' )

                        applySettings()

                        true
                else
                    paGenSettings = settings

                    if notify
                        showNotification window.chrome.i18n.getMessage( 'settingsSaved' )

                    applySettings()

                    true
    true
    else
        false

# Function: Get Settings
getSettings = () ->
    # Get settings using the Chrome extension storage API. Try sync, fallback to local
    window.chrome.storage.sync.get 'settings', (items) ->
        if items.settings
            settings = extend( paGenSettings, items.settings)

            if validateSettings settings, false
                paGenSettings = settings

                applySettings()
            true
        else
            window.chrome.storage.local.get 'settings', (items) ->
                if items.settings
                    settings = extend( paGenSettings, items.settings)

                    if validateSettings settings, false
                        paGenSettings = settings
                applySettings()
            true

If you inspect it, when you save the settings or generate a password (also saves settings), you'll see the following error popping up:

Error during storage.set: QUOTA_BYTES_PER_ITEM quota exceeded.

I've made it fallback to local storage, but I really want it to sync the settings.

So, if this just won't work, I'll change the get/set code to use a different key/value for each setting, instead of an object, but I don't think it makes much sense, as this sync feature is advertised to support objects (and this one is small), so I'm hoping I'm just doing something wrong.

Thank you!

Upvotes: 2

Views: 4560

Answers (1)

Bruno Bernardino
Bruno Bernardino

Reputation: 496

So I sorted it out.

The main issue was an "infinite loop" (n00b) I had because of the onChanged listener for chrome.storage ( https://developer.chrome.com/extensions/storage.html#event-onChanged ), where I'd save the settings every time there was a change detected.

I didn't read the documentation carefully enough and thought this was to "listen" for changes in the settings that came, for example, from a sync (but it listens to any change), and since I can get the settings from sync, I don't even need it.

Removing that listener fixed the problem.

The new version of the extension (1.0.4) is bug-free as far as I'm aware.

Upvotes: 4

Related Questions