Browse Source

react to sync storage changes

Andrew Dolgov 10 months ago
parent
commit
d45ecbcea7
3 changed files with 13 additions and 10 deletions
  1. 11 6
      background.js
  2. 1 1
      manifest.json
  3. 1 3
      options.js

+ 11 - 6
background.js

@@ -1,12 +1,17 @@
-chrome.storage.sync.get('user_css', function(val) {
-	console.log('got sync', val);
-
-	if (typeof val.user_css != 'undefined') {
-		localStorage['user_css'] = val.user_css;
+chrome.storage.sync.get('user_css', (ret) => {
+	if (typeof ret.user_css != 'undefined') {
+		localStorage['user_css'] = ret.user_css;
 	}
 });
 
-chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
+chrome.storage.onChanged.addListener((changes, namespace) => {
+   if (namespace == "sync" && typeof changes.user_css != "undefined") {
+		console.log("settings changed", changes);
+		localStorage['user_css'] = changes.user_css.newValue;
+   }
+});
+
+chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
 	if (request.method == "getCSS") {
 		var href = request.href;
 

+ 1 - 1
manifest.json

@@ -8,7 +8,7 @@
 		"128": "icons/icon128.png"
   },
   "name": "Bloat-free user stylesheets",
-  "version": "0.6",
+  "version": "0.7",
   "manifest_version": 2,
   "description": "Injects user stylesheets, without bloat.",
   "permissions": [ "http://*/*", "https://*/*", "storage" ],

+ 1 - 3
options.js

@@ -34,11 +34,9 @@ $(document).ready(function() {
 	$("#options").on('submit', function() {
 		var user_css = code_editor.getValue();
 
-		console.log('got', user_css);
-
 		localStorage['user_css'] = user_css;
 
-		chrome.storage.sync.set({user_css: user_css}, function() {
+		chrome.storage.sync.set({user_css: user_css}, () => {
 			$("#message").html("Data saved");
 		});