Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension: background.js doesn't take the new values from localStorage

Got a big problem, when I update my localStorage with my Options page it's OK, new values are stored but my background.js keeps the values of the first load even if I put something like :

var value = localStorage['item'];

When I refresh the extension it's OK but very annoying...

There is any fix about that?

Regards

like image 375
Syl Avatar asked Mar 23 '12 10:03

Syl


1 Answers

Are you sure? Are you keeping in mind that the script on the background page only gets executed once, when the extension first loads. So you need to tell the background page that the localStorage item has updated and to update your variable. Or your backgrounds code could just always access localStorage instead of using a variable.
As an example, this code will execute a function in the background page that will update the variable value....
background

// the following line will only be executed when the extension first loads
var value = localStorage['setting'];

function updateSetting () {
    value = localStorage['setting'];
}

options

localStorage['setting']=Math.random();
chrome.extension.getBackgroundPage().updateSetting();
like image 186
PAEz Avatar answered Oct 16 '22 03:10

PAEz