1

我似乎无法弄清楚为什么在重新加载页面时它似乎没有保存(即使它似乎正在将对象保存到 storage.

以下是相关代码:

选项.js

function saveSettings(){
 var settings = {
    "blockComments": document.querySelector("input[name='BlockComments']:checked"),
    "visualDisplay": document.getElementById("visualDisplay").options[document.getElementById("visualDisplay").selectedIndex],
    "shortcut": document.querySelectorAll("[data-shortcut-key] option:checked")
 };
 // Store options object
 chrome.storage.sync.set({"data": settings}, function() {
    // Update status to let user know options were saved.
    var status = document.getElementById('status');
    status.textContent = 'Options saved.';
    setTimeout(function() {
        status.textContent = '';
    }, 2000);
 });
}
// This is called on DOMContentReady and is triggered
function restoreSettings(){
  chrome.storage.sync.get("data", function(items) {
    console.log(items.data); // Returns empty object(s)
  });
}

console.log 显示的内容:

在此处输入图像描述

不知道为什么他们是空的。有什么帮助吗?

4

1 回答 1

1

您正在尝试保存 DOM 节点(不是 JSON 可序列化的)。

提取您需要的属性并保存它们,例如

// Returns true or false
document.querySelector("input[name='BlockComments']).checked

代替

// Returns an element or nothing depending on its state
document.querySelector("input[name='BlockComments']:checked")
于 2015-02-27T18:27:41.430 回答