1 // Copyright (c) 2012 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.
5 // Store CSS data in the "local" storage area.
7 // Usually we try to store settings in the "sync" area since a lot of the time
8 // it will be a better user experience for settings to automatically sync
11 // However, "sync" is expensive with a strict quota (both in storage space and
12 // bandwidth) so data that may be as large and updated as frequently as the CSS
13 // may not be suitable.
14 var storage = chrome.storage.local;
16 // Get at the DOM controls used in the sample.
17 var resetButton = document.querySelector('button.reset');
18 var submitButton = document.querySelector('button.submit');
19 var textarea = document.querySelector('textarea');
21 // Load any CSS that may have previously been saved.
24 submitButton.addEventListener('click', saveChanges);
25 resetButton.addEventListener('click', reset);
27 function saveChanges() {
28 // Get the current CSS snippet from the form.
29 var cssCode = textarea.value;
30 // Check that there's some code there.
32 message('Error: No CSS specified');
35 // Save it using the Chrome extension storage API.
36 storage.set({'css': cssCode}, function() {
37 // Notify that we saved.
38 message('Settings saved');
42 function loadChanges() {
43 storage.get('css', function(items) {
44 // To avoid checking items.css we could specify storage.get({css: ''}) to
45 // return a default value of '' if there is no css value yet.
47 textarea.value = items.css;
48 message('Loaded saved CSS.');
54 // Remove the saved value from storage. storage.clear would achieve the same
56 storage.remove('css', function(items) {
57 message('Reset stored CSS');
59 // Refresh the text area.
63 function message(msg) {
64 var message = document.querySelector('.message');
65 message.innerText = msg;
66 setTimeout(function() {
67 message.innerText = '';