-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
155 lines (138 loc) · 4.45 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
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-45267314-2']);
_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);
})();
var config = new Config();
var sites = new Sites(config);
function updateClearStatsInterval() {
var select = document.getElementById("clear_stats_interval");
var option = select.options[select.selectedIndex];
config.clearStatsInterval = option.value;
// TODO(nav): Set nextTimeToClear in Config
restoreOptions();
}
function updateTimeDisplay() {
var select = document.getElementById("time_display");
var option = select.options[select.selectedIndex];
config.timeDisplayFormat = option.value;
restoreOptions();
}
function addIgnoredSite() {
var newSite = document.getElementById("new_ignored_site").value;
if (newSite.indexOf("http://") != 0 &&
newSite.indexOf("https://") != 0) {
alert("Include http:// or https:// prefix.");
return;
}
chrome.extension.sendRequest(
{action: "addIgnoredSite", site: newSite},
function(response) {
restoreOptions();
});
}
function removeIgnoredSites() {
var select = document.getElementById("ignored_sites");
var ignoredSites = [];
for (var i = 0; i < select.children.length; i++) {
var child = select.children[i];
if (child.selected == false) {
ignoredSites.push(child.value);
}
}
localStorage['ignoredSites'] = JSON.stringify(ignoredSites);
restoreOptions();
}
// Restores options from localStorage, if available.
function restoreOptions() {
var ignoredSites = localStorage['ignoredSites'];
if (!ignoredSites) {
return;
}
ignoredSites = JSON.parse(ignoredSites);
var select = document.getElementById("ignored_sites");
select.options.length = 0;
for (var i in ignoredSites) {
var option = document.createElement("option");
option.text = ignoredSites[i];
option.value = ignoredSites[i];
select.appendChild(option);
}
var clearStatsInterval = config.clearStatsInterval;
select = document.getElementById("clear_stats_interval");
for (var i = 0; i < select.options.length; i++) {
var option = select.options[i];
if (option.value == clearStatsInterval) {
option.selected = true;
break;
}
}
var timeDisplay = config.timeDisplayFormat;
select = document.getElementById("time_display");
for (var i = 0; i < select.options.length; i++) {
var option = select.options[i];
if (option.value == timeDisplay) {
option.selected = true;
break;
}
}
}
function getTimestamp() {
var e = new Date,
t = e.getFullYear(),
o = ("00" + (e.getMonth() + 1)).slice(-2),
n = ("00" + e.getDate()).slice(-2),
r = ("00" + e.getHours()).slice(-2),
s = ("00" + e.getMinutes()).slice(-2),
i = t + "-" + o + "-" + n + "_" + r + "-" + s;
return i
}
//Timer for peiodic uploads
var time=0;
function download() {
var csvContent = "data:text/csv;charset=utf-8,";
var sitesDict = sites.sites;
var pairs = [];
var dict={}
for (var site in sitesDict) {
if (sitesDict.hasOwnProperty(site)) {
pairs.push(site + "," + sitesDict[site]);
dict[site]=sitesDict[site];
}
}
csvContent += pairs.join("\n");
window.open(encodeURI(csvContent));
// for (index = 0; index < pairs.length; index++){
db.collection("tracking_test").add({
// url: pairs[index].split(",")[0],
// time: parseFloat(pairs[index].split(",")[1])
// url:pairs.split(",")[0],
// time:parseFloat(pairs.split(",")[1])
url:dict[0],
time:dict[1],
uploading_time:getTimestamp()
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
// }
}
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("add_ignored").addEventListener(
"click", addIgnoredSite);
document.getElementById("remove_ignored").addEventListener(
"click", removeIgnoredSites);
document.getElementById("clear_stats_interval").addEventListener(
"change", updateClearStatsInterval);
document.getElementById("time_display").addEventListener(
"change", updateTimeDisplay);
document.getElementById("download").addEventListener(
"click", download);
restoreOptions();
});