OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Store settings in the synchronized repository. |
| 6 var storage = chrome.experimental.storage.sync; |
| 7 |
| 8 // Get at the DOM controls used in the sample. |
| 9 var resetButton = document.querySelector('button.reset'); |
| 10 var submitButton = document.querySelector('button.submit'); |
| 11 var textarea = document.querySelector('textarea'); |
| 12 |
| 13 // Load any CSS that may have previously been saved. |
| 14 loadChanges(); |
| 15 |
| 16 submitButton.addEventListener('click', saveChanges); |
| 17 resetButton.addEventListener('click', reset); |
| 18 |
| 19 function saveChanges() { |
| 20 // Get the current CSS snippet from the form. |
| 21 var cssCode = textarea.value; |
| 22 // Check that there's some code there. |
| 23 if (!cssCode) { |
| 24 message('Error: No CSS specified'); |
| 25 return; |
| 26 } |
| 27 // Save it locally (un-synchronized) using the Chrome extension storage API. |
| 28 storage.set({'css': cssCode}, function() { |
| 29 // Notify that we saved. |
| 30 message('Settings saved'); |
| 31 }); |
| 32 } |
| 33 |
| 34 function loadChanges() { |
| 35 storage.get('css', function(items) { |
| 36 if (items.css) { |
| 37 textarea.value = items.css; |
| 38 message('Loaded saved CSS.'); |
| 39 } |
| 40 }); |
| 41 } |
| 42 |
| 43 function reset() { |
| 44 // Remove the saved value from storage |
| 45 storage.remove('css', function(items) { |
| 46 message('Reset stored CSS'); |
| 47 }); |
| 48 // Refresh the text area. |
| 49 textarea.value = ''; |
| 50 } |
| 51 |
| 52 function message(msg) { |
| 53 var message = document.querySelector('.message'); |
| 54 message.innerText = msg; |
| 55 setTimeout(function() { |
| 56 message.innerText = ''; |
| 57 }, 3000); |
| 58 } |
OLD | NEW |