-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
296 lines (274 loc) · 7.81 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
function HistoryEntry(json) {
this.target = json.target;
this.referer = json.referer;
this.timestamp = json.timestamp;
}
function formatDate(timestamp) {
function toDigits(value) {
if (value < 10) {
return "0" + value;
} else {
return String(value);
}
}
var date = new Date(timestamp);
return toDigits(date.getHours()) + ":" +
toDigits(date.getMinutes());
}
HistoryEntry.prototype.display = function (builder) {
var referer = Url.parse(this.referer);
var name = referer.getFileName();
if (name == null) {
name = referer.getDomain();
} else {
name = "(" + referer.getBaseDomain() + ") " + name;
}
if (name.length > 32)
name = name.slice(0, 32) + "...";
builder
.begin("li")
.appendText(formatDate(this.timestamp) + ": ")
.begin("a")
.setAttribute("href", this.referer)
.setAttribute("title", this.referer)
.setAttribute("target", "_blank")
.appendText(name)
.end()
.end();
};
/**
* Information about a single cookie.
*/
function CookieInfo(json) {
this.cookie = json.cookie;
this.history = json.history.map(function (entry) { return new HistoryEntry(entry); });
this.baseNamesSeen = json.baseNamesSeen;
this.baseNamesSeen.sort();
this.baseDomainsSeen = json.baseDomainsSeen;
this.baseDomainsSeen.sort();
this.severity = json.severity;
}
CookieInfo.prototype.getHistoryLength = function () {
return this.history.length;
};
CookieInfo.prototype.getSourceCount = function () {
return this.baseNamesSeen.length;
};
CookieInfo.prototype.getSeverity = function () {
return this.severity;
};
/**
* A collection of alerts belonging to a particular domain base.
*/
function AlertInfo(ignored, baseName, cookies) {
this.ignored = ignored;
this.baseName = baseName;
this.cookies = cookies.map(function (cookie) { return new CookieInfo(cookie); });
this.primaryCookieCache = null;
this.openSettings = null;
this.containerNode = null;
}
/**
* Returns the unique cookie we'll show information about in the popup.
*/
AlertInfo.prototype.getPrimaryCookie = function () {
if (this.primaryCookieCache)
return this.primaryCookieCache;
var maxHistory = -1;
var primaryCookie = null;
this.cookies.forEach(function (cookie) {
var length = cookie.getHistoryLength();
if (length > maxHistory) {
maxHistory = cookie.getHistoryLength();
primaryCookie = cookie;
}
});
return this.primaryCookieCache = primaryCookie;
};
AlertInfo.prototype.getSeverity = function () {
if (this.isIgnored()) {
return displayInPopupSeverity;
} else {
return this.getPrimaryCookie().getSeverity();
}
};
/**
* Is this alert explicitly ignored?
*/
AlertInfo.prototype.isIgnored = function () {
return !!this.ignored.get(this.baseName);
};
/**
* Displays this information in the DOM using the specified root as the parent.
*/
AlertInfo.prototype.display = function (builder) {
var isIgnored = this.isIgnored();
var primary = this.getPrimaryCookie();
var severityColor = getSeverityColor(primary.severity);
var rootNode;
var containerNode;
builder
.delegate(function (_, node) { rootNode = node; })
.begin("div")
.delegate(function (_, node) { containerNode = node; })
.addClass(isIgnored ? "alert ignored" : "alert")
.addEventListener("click", this.onClick.bind(this, rootNode))
.begin("div")
.begin("span")
.addClass("domain")
.appendText(this.baseName)
.end("span")
.begin("span")
.addClass("domainStats")
.appendText(" (sites: ")
.appendText(primary.getSourceCount())
.appendText(", history: ")
.appendText(primary.getHistoryLength())
.appendText(")")
.end("span")
.end()
.begin("div")
.addClass("sources")
.begin("span")
.addClass("label")
.appendText("Sites tracked: ")
.end()
.forEach(primary.baseDomainsSeen, function (source, builder, index) {
builder
.appendText(index == 0 ? "" : ", ")
.begin("span")
.addClass("source")
.appendText(source)
.end();
})
.appendText(".")
.end()
.begin("div")
.addClass("severity")
.setStyle("background", severityColor)
.setStyle("borderLeft", "1px solid " + severityColor.darker(.1))
.end()
.end();
this.containerNode = containerNode;
};
/**
* Controller for the settings box for an alert.
*/
function AlertSettings(root, info) {
this.root = root;
this.info = info;
this.element = null;
}
AlertSettings.prototype.close = function () {
this.root.removeChild(this.element);
};
AlertSettings.prototype.open = function () {
var element;
var checker;
DomBuilder.attach(this.root)
.begin("div")
.addClass("settings")
.delegate(function (_, node) { element = node; })
.begin("input")
.addClass("checker")
.setAttribute("type", "checkbox")
.setAttribute("checked", this.info.isIgnored())
.delegate(function (_, node) { checker = node; })
.end()
.appendText("Ignore all cookies from ")
.begin("b")
.appendText(this.info.baseName)
.end()
.appendText(".")
.end();
this.element = element;
checker.addEventListener("change", this.onChanged.bind(this));
};
/**
* Calles when the check box is clicked.
*/
AlertSettings.prototype.onChanged = function (event) {
var checked = event.srcElement.checked;
this.info.ignored.put(this.info.baseName, checked);
var builder = DomBuilder.attach(this.info.containerNode);
if (checked) {
builder.addClass("ignored");
} else {
builder.removeClass("ignored");
}
};
AlertInfo.prototype.onClick = function (root, event) {
if (this.openSettings) {
this.openSettings.close();
this.openSettings = null;
} else {
this.openSettings = new AlertSettings(root, this);
this.openSettings.open();
}
};
/**
* Information about all active alerts.
*/
function AlertCollection(ignored, json) {
this.baseNames = Map.wrap(json.baseNames).map(function (cookies, baseName) {
return new AlertInfo(ignored, baseName, cookies);
});
this.alertsToDisplay = null;
}
/**
* Are there any alerts to display?
*/
AlertCollection.prototype.isEmpty = function () {
return this.getAlertsToDisplay().length == 0;
}
/**
* Returns a list of the alerts to display, sorted by severity.
*/
AlertCollection.prototype.getAlertsToDisplay = function () {
if (!this.alertsToDisplay) {
var sortedAlerts = [];
this.baseNames.forEach(function (name, alert) {
if (alert.getSeverity() >= displayInPopupSeverity)
sortedAlerts.push(alert);
});
sortedAlerts.sort(function (alertA, alertB) {
return alertB.getSeverity() - alertA.getSeverity();
});
this.alertsToDisplay = sortedAlerts;
}
return this.alertsToDisplay;
};
/**
* Updates the DOM to display information about this set of alerts.
*/
AlertCollection.prototype.display = function (builder) {
builder
.clearChildren()
.forEach(this.getAlertsToDisplay(), function (alert, builder) {
builder
.begin("div")
.delegate(alert.display.bind(alert))
.end();
});
};
/**
* Handles messages from the badge.
*/
function displayAlerts(ignored, message) {
var alerts = new AlertCollection(ignored, message.state);
if (alerts.isEmpty()) {
var empty = document.getElementById("empty");
empty.style.display = null;
} else {
var main = document.getElementById("main");
main.style.display = null;
alerts.display(DomBuilder.attach(main));
}
}
function onLoad() {
var ignored = MapStorage.create("ignored");
var browser = getBrowserController();
browser.sendRequest("getAlerts")
.onFulfilled(displayAlerts.bind(displayAlerts, ignored))
.onFailed(browser.getLogCallback());
}