forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
69 lines (61 loc) · 2.07 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Store CSS data in the "local" storage area.
//
// Usually we try to store settings in the "sync" area since a lot of the time
// it will be a better user experience for settings to automatically sync
// between browsers.
//
// However, "sync" is expensive with a strict quota (both in storage space and
// bandwidth) so data that may be as large and updated as frequently as the CSS
// may not be suitable.
var storage = chrome.storage.local;
// Get at the DOM controls used in the sample.
var resetButton = document.querySelector('button.reset');
var submitButton = document.querySelector('button.submit');
var textarea = document.querySelector('textarea');
// Load any CSS that may have previously been saved.
loadChanges();
submitButton.addEventListener('click', saveChanges);
resetButton.addEventListener('click', reset);
function saveChanges() {
// Get the current CSS snippet from the form.
var cssCode = textarea.value;
// Check that there's some code there.
if (!cssCode) {
message('Error: No CSS specified');
return;
}
// Save it using the Chrome extension storage API.
storage.set({'css': cssCode}, function() {
// Notify that we saved.
message('Settings saved');
});
}
function loadChanges() {
storage.get('css', function(items) {
// To avoid checking items.css we could specify storage.get({css: ''}) to
// return a default value of '' if there is no css value yet.
if (items.css) {
textarea.value = items.css;
message('Loaded saved CSS.');
}
});
}
function reset() {
// Remove the saved value from storage. storage.clear would achieve the same
// thing.
storage.remove('css', function(items) {
message('Reset stored CSS');
});
// Refresh the text area.
textarea.value = '';
}
function message(msg) {
var message = document.querySelector('.message');
message.innerText = msg;
setTimeout(function() {
message.innerText = '';
}, 3000);
}