forked from ccampbell/chromelogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromelogger-options.js
72 lines (56 loc) · 1.97 KB
/
chromelogger-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
70
71
72
// global localStorage
(function() {
'use strict';
var defaults = {
show_upgrade_messages: true,
show_line_numbers: false,
color1: "#888888",
color2: "#0563ad"
};
function getInputs() {
var checkboxes = document.querySelectorAll('input');
return Array.prototype.slice.call(checkboxes);
}
function showMessage(message) {
var info_div = document.getElementById("info");
info_div.innerHTML = message;
info_div.style.display = "block";
setTimeout(function() {
info_div.style.display = "none";
}, 2500);
}
function saveOptions(e) {
e.preventDefault();
getInputs().forEach(function(input) {
localStorage[input.name] = input.type == 'checkbox' ? input.checked : input.value;
});
showMessage('your settings have been saved');
}
function _setInputValue(input, value) {
if (input.type === 'checkbox') {
input.checked = value && value !== 'false';
return;
}
if (input.type === 'color' && !/#\d{6}/.test(value)) {
value = defaults[input.name];
}
input.value = value;
}
function restoreDefaults(e) {
e.preventDefault();
getInputs().forEach(function(input) {
var value = defaults[input.name];
localStorage[input.name] = value;
_setInputValue(input, value);
});
showMessage('settings have been restored to the defaults');
}
function init() {
getInputs().forEach(function(input) {
_setInputValue(input, input.name in localStorage ? localStorage[input.name] : defaults[input.name]);
});
document.getElementById('save').addEventListener('click', saveOptions, false);
document.getElementById('restore').addEventListener('click', restoreDefaults, false);
}
document.addEventListener('DOMContentLoaded', init, false);
}) ();