chrome.storage没有正确保存数据

时间:2014-05-06 12:43:29

标签: google-chrome google-chrome-extension opera

我有一个选项数组:

    settings: {
        'magnify': false,
        'nobinds': true,
        'nospoiler': false,
        'replies': true,
        'stickies': true,
        'autogif': true,
        'autowebm': true,
        'resamplewebm': false,
        'archives': true,
        'linkifier': true,
        'menu': ''
    },

我保存/获取这样的数据:

    loadSettings: function() {
        chrome.storage.local.get(Board.settings, function(items) {
            console.log(chrome.runtime.lastError);
            console.log(items);

            Board.settings = items;

            for(var key in Board.settings) {
                console.log("loaded [" + key + "] = " + Board.settings[key] + " - \"" + items[key] + "\"");

                if(typeof(Board.settings[key]) == 'boolean') {
                    Board.switchCheckboxAttribute($('#theme-' + key), Board.settings[key]);
                } else if(typeof(Board.settings[key]) == 'string') {
                    $('#theme-' + key).val(Board.settings[key]);
                }
            }
        });
    },

    saveSettings: function() {
        chrome.storage.local.set(Board.settings, function() {
            console.log(chrome.runtime.lastError);

            $('#theme-msg').html("Done").attr("class", "msg-ok").show().delay(2E3).fadeOut(500);

            Board.applySettings();
        });
    },

    switchCheckboxAttribute: function(obj, active) {
        console.log('switchCheckboxAttribute(' + obj.attr('id') + ', ' + active + ')');

        if(active){ 
            obj.addClass('active');
            obj.html("✔");
        } else {
            obj.removeClass('active');
            obj.html('');
        }
    },

控制台输出:

undefined board.js:74
Object {archives: true, autogif: true, autowebm: true, linkifier: true, magnify: false…} board.js:75
loaded [archives] = true - "true" board.js:80
switchCheckboxAttribute(theme-archives, true) board.js:107
loaded [autogif] = true - "true" board.js:80
switchCheckboxAttribute(theme-autogif, true) board.js:107
loaded [autowebm] = true - "true" board.js:80
switchCheckboxAttribute(theme-autowebm, true) board.js:107
loaded [linkifier] = true - "true" board.js:80
switchCheckboxAttribute(theme-linkifier, true) board.js:107
loaded [magnify] = false - "false" board.js:80
switchCheckboxAttribute(theme-magnify, false) board.js:107
loaded [menu] =  - "" board.js:80
loaded [nobinds] = true - "true" board.js:80
switchCheckboxAttribute(theme-nobinds, true) board.js:107
loaded [nospoiler] = false - "false" board.js:80
switchCheckboxAttribute(theme-nospoiler, false) board.js:107
loaded [replies] = true - "true" board.js:80
switchCheckboxAttribute(theme-replies, true) board.js:107
loaded [resamplewebm] = false - "false" board.js:80
switchCheckboxAttribute(theme-resamplewebm, false) board.js:107
loaded [stickies] = true - "true" board.js:80
switchCheckboxAttribute(theme-stickies, true) board.js:107
event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery.min.js:3
switchCheckboxAttribute(theme-resamplewebm, true) board.js:107
undefined 

上次运行时错误两次都未定义,但是当我保存数据并重新加载时,保存的数组保持不变(默认值)。我处理不正确吗?

1 个答案:

答案 0 :(得分:0)

答案:原来我正在更新jQuery点击的可见复选框,但我没有更新设置值,所以它没有改变,并且在保存时没有记录更改。