1

I cant seem to work out why it doesn't seem to be saving when reloading the page (even though it appears to be saving the object to storage.

Here's the relevant code:

options.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) }); } 

What the console.log displays:

enter image description here

Not sure why they're empty. Any help?

1 Answer 1

1

You are trying to save DOM nodes (which are not JSON-serializable).

Extract the properties you need and save them instead, e.g.

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

instead of

// Returns an element or nothing depending on its state document.querySelector("input[name='BlockComments']:checked") 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I noticed I could get properties but the reason I tried to get the object was because I wanted to modify it (e.g. on options page reload, set the radio button to be be whichever one was saved by the user as well as getting the value of it). Any way to do this or do I just do property by property?
Just go with sets of properties you need. Kind of like here: developer.chrome.com/extensions/optionsV2#step-2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.