localStorage.getItem returns null on other page

105 Views Asked by At

I am trying to make an extension for Opera. I have an options page with options.js:

function saveOptions() {
    localStorage.setItem("secret", document.getElementById("secret").value);
}

function restoreOptions() {
    document.getElementById("secret").value = localStorage.getItem("secret") || '';
}

alert(localStorage.getItem("secret"));
restoreOptions();
document.querySelector("form").addEventListener("submit", saveOptions);

The alert shows the correct value for secret.

However, in the content script, the same alert line returns null. Why is this the case? How can it work on one page, but not on the other? How can I fix this?

1

There are 1 best solutions below

0
Gabriel Nadaleti On

LocalStorage data won't persist through domains.

You can use chrome.storage as opera extensions are chromium based.

https://developer.chrome.com/docs/extensions/reference/storage/