-
Notifications
You must be signed in to change notification settings - Fork 1
/
blackout_paths.js
94 lines (72 loc) · 2.52 KB
/
blackout_paths.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
(function(win) {
var container = document.querySelector('#domains_list_container'),
pathsTitle = document.querySelector('#blackout_paths_title');
var addPathContainer = document.querySelector('#add_path_container'),
inputPath = document.querySelector('#input_path'),
buttonAdd = document.querySelector('#button_add_path');
var pathList = document.querySelector('#blackout_paths_list');
var storageManager,
blackoutPaths;
var currentDomain;
chrome.runtime.getBackgroundPage(function(bg) {
storageManager = bg.storageManager;
bg.storageManager.getBlackoutPaths(function(paths) {
blackoutPaths = paths;
generateDomainList(paths);
});
});
function generateDomainList(readPages) {
var ul = node('ul');
container.appendChild(ul);
for (var domain in readPages){
if (readPages.hasOwnProperty(domain)) {
var li = node('li');
ul.appendChild(li);
li.innerText = domain;
}
}
}
function generatePathList(domain, domainPaths) {
pathsTitle.innerText = domain;
pathList.innerHTML = '';
for (var path in domainPaths) {
if (domainPaths.hasOwnProperty(path)) {
addBlackoutPathToList(path);
}
}
}
function addBlackoutPathToList(path) {
if (!currentDomain) return;
var li = node('li');
pathList.appendChild(li);
li.innerText = path;
var span = node('span');
span.innerText = 'x';
li.appendChild(span);
}
container.addEventListener("click", function(e){
if (e.target.tagName != 'LI') return;
var domain = e.target.innerText;
generatePathList(domain, blackoutPaths[domain]);
currentDomain = domain;
});
pathList.addEventListener('click', function(e) {
if (e.target.tagName != 'SPAN') return;
var li = e.target.parentNode;
var path = li.childNodes[0].nodeValue;
storageManager.lightupPath(currentDomain, path);
pathList.removeChild(li);
})
buttonAdd.addEventListener('click', function(e) {
var path = inputPath.value;
if (path.charAt(0) != '/') {
path = '/' + path;
}
storageManager.blackoutPath(currentDomain, path);
addBlackoutPathToList(path);
});
// Helper methods
function node(tagName) {
return document.createElement(tagName);
}
})(window);