-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_pages.js
80 lines (65 loc) · 2.19 KB
/
read_pages.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
(function(win) {
var container = document.querySelector('#container');
var storageManager;
chrome.runtime.getBackgroundPage(function(bg) {
storageManager = bg.storageManager;
bg.storageManager.getReadPages(function(pages) {
generateReadPagesList(pages);
});
});
function generateReadPagesList(readPages) {
for (var domain in readPages){
if (readPages.hasOwnProperty(domain)) {
generateDomainPagesList(domain, readPages[domain]);
}
}
var data = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(readPages));
var a = node('a');
a.href = 'data:' + data;
a.download = 'read_pages.json';
a.text = 'download as JSON';
container.appendChild(a);
}
function generateDomainPagesList(domain, domainPages) {
var LINK_TARGET = '_blank';
var h2 = node('h2'),
ha = node('a');
container.appendChild(h2);
h2.appendChild(ha);
ha.href = "http://" + domain;
ha.text = domain;
ha.target = LINK_TARGET
var ul = node('ul');
var hasReadPage = false;
for (var path in domainPages) {
if (domainPages.hasOwnProperty(path)) {
hasReadPage = true;
var li = node('li'),
la = node('a');
ul.appendChild(li);
li.appendChild(la);
la.href = "http://" + domain + path;
la.text = path;
la.target = LINK_TARGET;
}
}
if (hasReadPage) {
container.appendChild(ul);
} else {
var span = node('span');
span.innerText = 'x';
h2.appendChild(span);
}
}
container.addEventListener("click", function(e){
if (e.target.tagName != 'SPAN') return;
var h2 = e.target.parentNode;
var domain = h2.childNodes[0].innerText;
storageManager.removeDomain(domain);
container.removeChild(h2);
});
// Helper methods
function node(tagName) {
return document.createElement(tagName);
}
})(window);