【问题标题】:chrome.storage not saving data properlychrome.storage 未正确保存数据
【发布时间】:2014-05-06 12:43:28
【问题描述】:

我有一个选项数组:

    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 

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

【问题讨论】:

  • 您是否尝试将null 作为第一个参数传递给chrome.storage.local.get?这应该可以让您了解存储的全部内容
  • 我把它改成null,结果是Object {archives: true, autogif: true, autowebm: true, linkifier: true, magnify: false...} 所以整个数组都返回了,但是保存似乎并没有真正改变任何东西。
  • 好的,更新,我解决了。我注意到在我的复选框 onclick 函数中,尽管更改了可见复选框的值,但我实际上并没有更改数组的值。我现在已经修好了。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 2019-09-26
    • 2021-10-15
    • 2012-07-26
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多