2

I am working on pretty simple extension. My popup.html has some radiobuttons, which represent set of predefined options for user. The problem is that logic of my content-script depends upon those options.

So, my question is: let us assume I would attach a script to 'popup.html' (meeting Chrome CSP) that listens to any change in my radiobuttons selection and as soon as it happens does someting like .localstorage.setItem(key, value)? Would my content-script be able to get this value from local storage at appropriate moment?

52hertz
  • 342
  • 4
  • 16
  • Why couldn't you do that? – Alec von Barnekow Oct 01 '16 at 08:15
  • 1
    @Fralec I am very new to this. That's why I am asking. should I call `chrome.storage.localStorage.setItem()`? – 52hertz Oct 01 '16 at 08:19
  • You appear to be confused as to the difference between [`Window.localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) and [`chrome.storage`](https://developer.chrome.com/extensions/storage) See: [window.localStorage vs chrome.storage.local](http://stackoverflow.com/questions/24279495/window-localstorage-vs-chrome-storage-local). If interested, [this answer](http://stackoverflow.com/a/39236352/3773011) has an example popup & options page which stores data to `chrome.storage.local` and it s tested and working in both Chrome and Firefox. – Makyen Oct 01 '16 at 08:31

1 Answers1

3

Naturally, you can do that. Here two examples:

chrome.storage.local.set({'value': theValue}, function() {
    // Notify that we saved.
    message('Settings saved');
});

chrome.storage.local.get('value', function(obj) {
    //Notify that we get the value.
    message('Value is ' + obj.value);
});

Or you can use the sync storage:

chrome.storage.sync.set({'value': theValue}, function() {
    // Notify that we saved.
    message('Settings saved');
});

chrome.storage.sync.get('value', function(obj) {
    //Notify that we get the value.
    message('Value is ' + obj.value);
});

Here is the documentation.

The difference between chrome.storage and window.localStorage, is that you can use window.localStorage on every recent browser. Also on websites. The stored objects will be stored until the user close the browser.

You are building a Chrome extension, so I would recommend you to use the API that was make for that.

  • Given that the question is specifically about [`Window.localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), you should address the differences between that API and [`chrome.storage`](https://developer.chrome.com/extensions/storage). – Makyen Oct 01 '16 at 08:35
  • Once again. Just to makesure I got it right. I create a popup.html. Attach script for that one in a way ` – 52hertz Oct 01 '16 at 08:48
  • Yep, be just careful: `chrome.storage.LOCAL.set` pr `chrome.storage.SYNC.set` – Alec von Barnekow Oct 01 '16 at 08:50