forked from abstiles/deluminate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
232 lines (212 loc) · 6.42 KB
/
popup.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var site;
var key1;
var key2;
function setRadio(name, value) {
var radios = document.querySelectorAll('input[name="' + name + '"]');
// Convention: trailing digits on scheme represent subschemes. Strip them
// before looking for the correct radio to enable.
value = value.replace(/\d+$/, '');
for (var i = 0; i < radios.length; i++) {
radios[i].checked = (radios[i].value.lastIndexOf(value, 0) === 0);
radios[i].disabled = !getEnabled();
}
}
function update() {
document.body.className = getEnabled() ? '' : 'disabled';
if (getEnabled()) {
$('title').innerText = 'Deluminate is Enabled';
$('toggle').innerHTML = '<b>Disable</b> ' +
'<span class="kb">(' + key1 + ')</span>';
$('subcontrols').style.display = 'block';
} else {
$('title').innerText = 'Deluminate is Disabled';
$('toggle').innerHTML = '<b>Enable</b> ' +
'<span class="kb">(' + key1 + ')</span>';
$('subcontrols').style.display = 'none';
}
setRadio('keyaction', getKeyAction());
if (site) {
setRadio('scheme', getSiteScheme(site));
$('toggle_contrast').checked = (getSiteModifiers(site).indexOf('low-contrast') > -1);
$('force_textfield').checked = (getSiteModifiers(site).indexOf('force_text') > -1);
$('kill_bgfield').checked = (getSiteModifiers(site).indexOf('kill_background') > -1);
$('make_default').disabled = !(changedFromDefault());
} else {
setRadio('scheme', getDefaultScheme());
$('toggle_contrast').checked = getLowContrast();
$('force_textfield').checked = getForceText();
$('kill_bgfield').checked = getKillBackground();
}
if (getEnabled()) {
document.documentElement.setAttribute(
'hc',
site ? getSiteScheme(site) : getDefaultScheme());
} else {
document.documentElement.setAttribute('hc', 'normal');
}
chrome.extension.getBackgroundPage().updateTabs();
}
function changedFromDefault() {
var siteModList = getSiteModifiers(site);
var defaultModList = getDefaultModifiers();
return (getSiteScheme(site) != getDefaultScheme() ||
siteModList != defaultModList);
}
function onToggle() {
setEnabled(!getEnabled());
update();
}
function onForget() {
resetSiteSchemes();
update();
}
function onRadioChange(name, value) {
switch (name) {
case 'keyaction':
setKeyAction(value);
break;
case 'apply':
setApply(value);
break;
case 'scheme':
if (site) {
setSiteScheme(site, value);
} else {
setDefaultScheme(value);
}
break;
}
update();
}
function onLowContrast(evt) {
if (site) {
if (evt.target.checked) {
addSiteModifier(site, 'low-contrast');
} else {
delSiteModifier(site, 'low-contrast');
}
} else {
setLowContrast(evt.target.checked);
}
update();
}
function onForceText(evt) {
if (site) {
if (evt.target.checked) {
addSiteModifier(site, 'force_text');
} else {
delSiteModifier(site, 'force_text');
}
} else {
setForceText(evt.target.checked);
}
update();
}
function onKillBackground(evt) {
if (site) {
if (evt.target.checked) {
addSiteModifier(site, 'kill_background');
} else {
delSiteModifier(site, 'kill_background');
}
} else {
setForceText(evt.target.checked);
}
update();
}
function onDimLevel(evt) {
dimLevel = "noinvert-dim" + evt.target.value;
$('dim_radio').value = dimLevel;
if (site) {
setSiteScheme(site, dimLevel);
} else {
setDefaultScheme(dimLevel);
}
update();
}
function onMakeDefault() {
setDefaultScheme(getSiteScheme(site));
setDefaultModifiers(getSiteModifiers(site));
update();
}
function addRadioListeners(name) {
var radios = document.querySelectorAll('input[name="' + name + '"]');
for (var i = 0; i < radios.length; i++) {
radios[i].addEventListener('change', function(evt) {
onRadioChange(evt.target.name, evt.target.value);
}, false);
radios[i].addEventListener('click', function(evt) {
onRadioChange(evt.target.name, evt.target.value);
}, false);
}
}
// Open all links in new tabs.
function onLinkClick() {
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
(function () {
var ln = links[i];
var location = ln.href;
ln.onclick = function () {
chrome.tabs.create({active: true, url: location});
};
})();
}
}
function onSettings() {
setSettingsViewed();
chrome.tabs.create({active: true, url: "options.html"});
}
function init() {
addRadioListeners('keyaction');
addRadioListeners('apply');
addRadioListeners('scheme');
$('toggle_contrast').addEventListener('change', onLowContrast, false);
$('force_textfield').addEventListener('change', onForceText, false);
$('kill_bgfield').addEventListener('change', onKillBackground, false);
$('dim_amount').addEventListener('input', onDimLevel, false);
$('toggle').addEventListener('click', onToggle, false);
$('make_default').addEventListener('click', onMakeDefault, false);
$('settings').addEventListener('click', onSettings, false);
if (navigator.appVersion.indexOf('Mac') != -1) {
key1 = '⌘+Shift+F11';
key2 = '⌘+Shift+F12';
} else {
key1 = 'Shift+F11';
key2 = 'Shift+F12';
}
if (!getSettingsViewed()) {
$('settings').className += " new";
}
chrome.windows.getLastFocused({'populate': true}, function(window) {
site = '';
for (var i = 0; i < window.tabs.length; i++) {
var tab = window.tabs[i];
if (tab.active) {
if (isDisallowedUrl(tab.url)) {
$('scheme_title').innerText = 'Default color scheme:';
$('make_default').style.display = 'none';
} else {
site = siteFromUrl(tab.url);
$('scheme_title').innerHTML = 'Color scheme for <b>' + site +
'</b>:<br><span class="kb">(' + key2 + ')</span>';
$('make_default').style.display = 'block';
break;
}
}
}
if (site) {
currentScheme = getSiteScheme(site);
} else {
currentScheme = getDefaultScheme(site);
}
if (currentScheme.lastIndexOf('noinvert-dim', 0) === 0) {
currentDimLevel = currentScheme.replace(/.*(\d+)$/, '$1');
$('dim_amount').value = currentDimLevel;
$('dim_radio').value = 'noinvert-dim' + currentDimLevel;
}
update();
});
}
window.addEventListener('load', init, false);
document.addEventListener('DOMContentLoaded', onLinkClick);