Search code examples
javascripthtmlcssgoogle-chrome-extension

How to make my notepad chrome extension not lose the user's note when they close it?


I am making a notepad and I want the chrome extension to keep what the user types in a text area when they close it... How would I go about doing this? I've tried so many times but failed


Solution

  • You can use chrome.storage.local or chrome.storage.sync to store extension data.

    Note that you need to add it to extension manifest inside permissions section:

      "permissions": [
        "storage"
      ],
    

    It is asynchronous API - you can use it with callbacks like this:

    chrome.storage.sync.set({key: value}, function() {
      console.log('Value is set to ' + value);
    });
    
    chrome.storage.sync.get(['key'], function(result) {
      console.log('Value currently is ' + result.key);
    });
    

    I also recommend to check the official documentation: https://developer.chrome.com/docs/extensions/reference/storage/