Search code examples
javascripthtmlgoogle-chrome-extension

use variable in 2 different js file (chrome extension)


I'm trying to build an autofill extension but I don't know ho to use user input in my js fie. I tried with:

localStorage.setItem("full_name_E", full_name_E);

this is in the html script (popup for etension). And in the autofill js file I wrote:

localStorage.getItem('full_name_E');

but if I type the first command in the chrome console and refresh the page, full_name_E value appear in the box. I tried with import/export but it doesn't work. Any help?


Solution

  • You should use the Chrome storage API for this extension. It will allow you to let your users sync their autofill data to any browser they use.

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

    Add this to your manifest:

    "permissions": [
        "storage"
      ],
    

    You can find more information here: https://developer.chrome.com/docs/extensions/reference/storage/