4

According to MDN,

Background scripts are loaded as soon as the extension is loaded and stay loaded until the extension is disabled or uninstalled.

My background script changes its behavior accordingly user’s options, stored on localStorage. When the user change options (using options_ui), localStorage is updated, but since the background script stay loaded, user options are not honored. If the browser is reloaded, options are honored.

How do I reload the background script after the user change options?

rodorgas
  • 962
  • 2
  • 12
  • 29
  • 3
    It's chrome.runtime.reload() but the proper solution is to send a [message](https://developer.chrome.com/extensions/messaging) to the background script so it can run the relevant code in its onMessage callback. – wOxxOm May 06 '18 at 19:45
  • Thanks @wOxxOm, I can accept this as an answer if you post. – rodorgas May 06 '18 at 19:48

1 Answers1

4

As said could be chrome.runtime.reload(), but is better to send a message to the background script and update.

Something like this:

chrome.runtime.onMessage.addListener( function(request, sender) 
    updateOption(request.option1)
});

in option.js:

 chrome.storage.sync.set({'option1': 'new_value'}); ....
 chrome.tabs.sendMessage(tab, {option1: 'new_value'});

Kind regards

Marcelo Rossi
  • 375
  • 3
  • 6