简体   繁体   中英

Can I save input data from popup.html in localStorage?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM