forked from ericu/jv-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.js
168 lines (153 loc) · 4.04 KB
/
settings.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
_debug("Setting up settings.");
function disableOption(disabled, containerId) {
if (isChrome()) {
elts = document.options_form[containerId];
if (!elts.length) {
elts.disabled = disabled;
}
for (var i = 0; i < elts.length; ++i) {
elts[i].disabled = disabled;
}
} else {
document.getElementById(containerId).disabled = disabled;
}
}
function setAllDisabled(disabled) {
var i;
for (i = 0; i < prefs.length; ++i) {
disableOption(disabled, prefs[i].containerId);
}
}
function setupBoolPref(pref) {
var value = getPrefs().getBoolPref(pref.prefName);
if (isChrome()) {
var true_elt = document.getElementById(pref.trueId);
var false_elt = document.getElementById(pref.falseId);
if (getPrefs().prefSet(pref.prefName)) { // Else leave the defaults.
if (true_elt) {
true_elt.checked = value;
}
if (false_elt) {
false_elt.checked = !value;
}
} else {
getPrefs().setBoolPref(pref.prefName, (true_elt && true_elt.checked));
}
} else {
var group = document.getElementById(pref.containerId);
if (!group) {
var true_elt = document.getElementById(pref.trueId);
var false_elt = document.getElementById(pref.falseId);
if (true_elt) {
true_elt.checked = value;
}
if (false_elt) {
false_elt.checked = !value;
}
if (!true_elt && !false_elt) {
alert("Couldn't find " + pref.containerId);
}
return;
}
var elt;
if (value) {
elt = document.getElementById(pref.trueId);
}
if (!elt || !value) {
elt = document.getElementById(pref.falseId);
}
if (!elt) { // bad pref?
alert("Failed to find " + pref.trueId + " or " + pref.falseId);
elt = document.getElementById(pref.trueId);
}
if (elt) {
group.selectedItem = elt;
}
}
}
function setupStringPref(pref) {
var value = getPrefs().getStringPref(pref.prefName);
var elt = document.getElementById(pref.containerId);
if (getPrefs().prefSet(pref.prefName)) { // Else leave the defaults.
elt.value = value;
} else {
getPrefs().setStringPref(pref.prefName, elt.value);
}
}
function saveBoolPref(pref) {
var elt = document.getElementById(pref.trueId);
getPrefs().setBoolPref(pref.prefName,
isChrome() ? elt.checked : elt.selected);
}
function saveStringPref(pref) {
var elt = document.getElementById(pref.containerId);
getPrefs().setStringPref(pref.prefName, elt.value);
}
function saveEnableFlag() {
var elt = document.getElementById("jv-settings-checkbox");
if (!elt) {
alert("Couldn't find checkbox!");
return;
}
var enabled = getEnabledPref();
if (elt.checked != enabled) {
enabled = elt.checked;
saveEnabledPref(enabled);
}
return enabled;
}
function jv_checkSettings() {
//dumpLocalStorage();
var i;
try {
for (i = 0; i < prefs.length; ++i) {
if (prefs[i].isBool)
setupBoolPref(prefs[i]);
else
setupStringPref(prefs[i]);
}
// set 'enabled' checkbox
var elt = document.getElementById("jv-settings-checkbox");
if (!elt) {
alert("missing elt!");
}
var enabled = getEnabledPref();
if (elt) {
elt.checked = enabled;
}
setAllDisabled(!enabled);
} catch (ex) {
popup(stringifyObj(ex));
}
}
function jv_EnabledToggled() {
var elt = document.getElementById("jv-settings-checkbox");
if (!elt) {
alert("Couldn't find checkbox!");
return;
}
setAllDisabled(!elt.checked);
}
function jv_saveSettings() {
// Radio buttons first
var i;
for (i = 0; i < prefs.length; ++i) {
if (prefs[i].isBool)
saveBoolPref(prefs[i]);
else
saveStringPref(prefs[i]);
}
// Now the global enable flag
var enabled = saveEnableFlag();
// Now tell everybody
notifyAll(enabled);
}
if (!isChrome()) {
function jv_settingsKeypress(evt) {
if (evt.keyCode == evt.DOM_VK_ESCAPE)
window.close();
}
window.addEventListener("keypress", jv_settingsKeypress, true);
window.addEventListener("load", jv_checkSettings, true);
}
_debug("Set up settings.");