forked from gintas/crackbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
260 lines (212 loc) · 8.26 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var TOP_DOMAINS_NUM = 6;
var MSG_SAVED_DELAY = 2*1000;
function getTopDomains(historyItems) {
var typedCounts = {};
// Get URL data.
var domains = [];
for (var i = 0; i < historyItems.length; i++) {
var h = historyItems[i];
if (h.url && h.typedCount) {
if (h.url.slice(0, 5) == 'https')
continue; // https URLs are probably not junk
var url = trimWWW(trimProtocol(h.url.trim()));
var domain = trimPath(url);
// Special case for Google Reader.
// TODO: test me
var components = url.split('/');
if (components.length > 1 && components[0].indexOf('google.') != -1 && components[1] == 'reader') {
domain = components[0] + '/' + components[1];
}
if (domains.indexOf(domain) == -1) {
domains.push(domain);
typedCounts[domain] = h.typedCount;
} else {
typedCounts[domain] += h.typedCount;
}
}
}
// Sort by typed count (descending).
domains.sort(function(a, b) { return typedCounts[b] - typedCounts[a]; });
// Take top N.
var topUrls = domains.slice(0, TOP_DOMAINS_NUM);
return topUrls;
} // getTopDomains
function addUrlField(ulId, value) {
var checkbox = document.createElement('input');
checkbox.setAttribute('type', 'checkbox');
checkbox.setAttribute('checked', 'checked');
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('class', 'domain');
input.value = value;
var li = document.createElement('li');
li.setAttribute('class', 'domain');
li.appendChild(checkbox);
li.appendChild(input);
var ul = document.getElementById(ulId);
var button_li = ul.children[ul.children.length - 1];
// Insert field before the "add domain" button.
ul.insertBefore(li, button_li);
}
function clearDomainsFromPage(ulId) {
var ul = document.getElementById(ulId);
for (var i = ul.children.length-1; i >= 0; i--) {
var li = ul.children[i];
if (li.getAttribute('class') == 'domain')
ul.removeChild(li);
}
}
function putDomainsOnPage(ulId, placeholderId, domains) {
// Put on page.
for (var i = 0; i < domains.length; i++) {
addUrlField(ulId, domains[i]);
}
// Remove placeholder, if any.
var placeholder = document.getElementById(placeholderId);
if (placeholder) {
document.getElementById(ulId).removeChild(placeholder);
}
} // putDomainsOnPage
function collectInputs(ulId) {
var ul = document.getElementById(ulId);
var domains = [];
for (var i = 0; i < ul.childNodes.length; i++) {
if (ul.childNodes[i].nodeName == "LI") {
var li = ul.childNodes[i];
var checkbox = li.childNodes[0];
var input = li.childNodes[1];
if (checkbox.checked && input.value.indexOf('.') != -1) {
domains.push(input.value.trim());
}
}
}
return domains;
}
// Collect the most frequently visited domains and prepopulate the blacklist.
function loadTopUrls() {
var weekAgo = new Date().getTime() - 1000*3600*24*7;
chrome.history.search({ text: "", startTime: weekAgo,
maxResults: 1000 },
function(historyItems) {
var topUrls = getTopDomains(historyItems);
setLocal('junkDomains', topUrls);
// Not calling submitConfigChange here to give the chance for the
// user to clean the domain list.
putDomainsOnPage("siteBlacklist", "blacklistPlaceholder", topUrls);
}
);
} // loadTopUrls()
function addJunkDomain() {
addUrlField('siteBlacklist', '');
}
function bindControlHandlers() {
document.getElementById('add_junk_domain_button').onclick = addJunkDomain;
document.getElementById('save_button').onclick = saveSettings;
document.getElementById('crackbookLink').onclick = function() {
chrome.tabs.create({url: 'http://crackbook.info'});
}
}
function showSettings() {
// Threshold & delay
document.getElementById("dimmerThreshold").value = getLocal('dimmerThreshold');
document.getElementById("dimmerDelay").value = getLocal('dimmerDelay').toFixed(2);
document.getElementById("dimmerDelayIncrement").value = getLocal('dimmerDelayIncrement').toFixed(2);
document.getElementById("reset_daily_flag").checked = getLocal('reset_daily_flag');
document.getElementById("base_delay").value = getLocal('base_delay');
document.getElementById("checkActiveTab").checked = getLocal('checkActiveTab');
// Junk domains.
clearDomainsFromPage('siteBlacklist');
if (getLocal('junkDomains').length > 0) {
putDomainsOnPage("siteBlacklist", "blacklistPlaceholder", getLocal("junkDomains"));
} else {
loadTopUrls();
}
// Schedule
document.getElementById("startTime").value = renderTime(getLocal('startTime'));
document.getElementById("endTime").value = renderTime(getLocal('endTime'));
var currentWeekdays = getLocal('weekdays');
for (var i = 0; i < 7; i++) {
document.getElementById("weekday-" + i).checked = currentWeekdays.indexOf("" + i) != -1;
}
// Reporting
if (getLocal('reporting'))
document.getElementById("upload_stats").checked = true;
}
function saveSettings() {
/* Save settings from submitted form. */
var junkDomains = collectInputs("siteBlacklist");
for (var i = 0; i < junkDomains.length; i++) {
junkDomains[i] = trimWWW(trimProtocol(junkDomains[i]));
}
var dimmerThreshold = parseInt(document.getElementById("dimmerThreshold").value, 10);
if (isNaN(dimmerThreshold) || dimmerThreshold < 0) {
dimmerThreshold = getLocal('dimmerThreshold');
}
var dimmerDelay = parseFloat(document.getElementById("dimmerDelay").value);
if (isNaN(dimmerDelay) || dimmerDelay < 0) {
dimmerDelay = getLocal('dimmerDelay');
}
var dimmerDelayIncrement = parseFloat(document.getElementById("dimmerDelayIncrement").value);
if (isNaN(dimmerDelayIncrement) || dimmerDelayIncrement < 0) {
dimmerDelayIncrement = getLocal('dimmerDelayIncrement');
}
var base_delay = parseFloat(document.getElementById("base_delay").value);
if (isNaN(base_delay) || base_delay < 0) {
base_delay = getLocal('base_delay');
}
var reset_daily_flag = !!document.getElementById('reset_daily_flag').checked;
var checkActiveTab = document.getElementById("checkActiveTab").checked;
// TODO: better validation
var startTime = parseTime(document.getElementById("startTime").value);
var endTime = parseTime(document.getElementById("endTime").value);
var weekdays = "";
for (i = 0; i < 7; i++) {
if (document.getElementById("weekday-" + i).checked)
weekdays += i;
}
var reporting = document.getElementById("upload_stats").checked;
// Write settings to storage.
setLocal('reporting', reporting);
setLocal('dimmerThreshold', dimmerThreshold);
setLocal('dimmerDelay', dimmerDelay);
setLocal('dimmerDelayIncrement', dimmerDelayIncrement);
setLocal('reset_daily_flag', reset_daily_flag);
setLocal('base_delay', base_delay);
setLocal('checkActiveTab', checkActiveTab);
setLocal('junkDomains', junkDomains);
setLocal('startTime', startTime);
setLocal('endTime', endTime);
setLocal('weekdays', weekdays);
bgPage().submitConfigChange();
bgPage().updateIcon(null, true);
// Re-render saved settings so that invalid settings can be seen.
showSettings();
// Show status message.
document.getElementById('save_button').style.display = 'none';
var msg = document.getElementById('saved_message');
msg.style.display = 'inline'; // show the message
var opacityValue = 1.0;
var timeoutFn = function() {
opacityValue -= 1 / 20.0;
msg.style.opacity = opacityValue;
if (opacityValue < 0.1) {
window.close();
} else {
setTimeout(timeoutFn, MSG_SAVED_DELAY / 20.0);
}
};
setTimeout(timeoutFn, MSG_SAVED_DELAY / 20.0);
} // saveSettings
window.onload = function() {
bindControlHandlers();
showSettings();
};
// Google Analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6080477-5']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();