-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewAll.js
62 lines (52 loc) · 1.71 KB
/
viewAll.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
/*
* Loaded with the page provided with the extension to manage
* banned domains.
*/
// Remove any checked domain in table
var removeDomains = function() {
var domainsTable = document.getElementById("domainsTable");
var toRemove = [];
for (var i=0, row; row = domainsTable.rows[i]; i++) {
var checkBox = row.cells[0].getElementsByTagName("input")[0];
if (checkBox && checkBox.checked) {
// Add to list to remove
toRemove.push(i);
}
}
// Remove any checked domains from storage
chrome.storage.sync.get("banned", function(data) {
for (var i=0; i < toRemove.length; i++) {
// This looks weird, but it's because our list shrinks as we remove elements
data.banned.splice(toRemove[i] - i, 1);
}
// Update banned domains
chrome.storage.sync.set({"banned": data.banned}, populateTable(data.banned));
});
};
// Populate our table with banned domains
var populateTable = function(bannedDomains) {
// Clear the table first in case this is a secondary load
var domainsTable = document.getElementById("domainsTable");
domainsTable.innerHTML = '';
// Create all rows
for (var i=0; i < bannedDomains.length; i++) {
var newRow = domainsTable.insertRow(-1);
var cell1 = newRow.insertCell(0);
cell1.innerHTML = "<input type=\"checkbox\">";
var cell2 = newRow.insertCell(1);
cell2.innerHTML = bannedDomains[i];
}
};
// Set button listeners
var setListeners = function() {
// Set event listener for remove button
var removeButton = document.getElementById("removeSelected");
removeButton.addEventListener("click", removeDomains);
};
// Run when DOM is loaded
window.onload = function() {
setListeners();
chrome.storage.sync.get({"banned": []}, function(data) {
populateTable(data.banned);
});
};