Now I use chrome.extension.sendMessage in content script and chrome.extension.onMessage.addListener in background script. But the problem appears when I need to sync to extension's local storage in real time (several checkboxes with options in extension popup).
Popup with checkbox (to control content-script.js options) -> checkbox state stored in extension's local storage.
Content script need to know changes made by user in popup window -> send request to Background page to access extension's local storage keys.
Background page -> send response to Content script -> callback function replicate all keys from extension's local storage to web local storage.
Content script read replicated keys and turn on/off it's options.
But this process is not real-time and I need it to be reactive.
Content-script.js:
chrome.extension.sendMessage({ name: "cache" }, function(response) { var status = response.url; if (status == 'enabled') { localStorage['cache'] = 'enabled'; } if (status == 'disabled') { localStorage['cache'] = 'disabled'; } } ); Backround.js:
chrome.extension.onMessage.addListener( function(request, sender, sendResponse) { console.log("request recieved is " + request); if (request.name == "cache") { sendResponse({ url: JSON.parse(localStorage['cache']).status }); } else {} } ); Thanks for your help!
localStorage['cache']is a string, but in your background.js you seem to be doing aJSON.parse()on it. Is that the error you are referring to?chrome.extension.sendMessageand friends are deprecated in favour ofchrome.runtime.sendMessage