386da741d7c81a7059fbc09c7aca81e69172a316
1var mainlist;
2var rmspan = ["<span class='remove' id='delete-", "'>–</span>"]
3document.addEventListener("DOMContentLoaded", function() {
4 chrome.storage.sync.get(
5 {"lists": [
6 [
7 ["Github", "https://github.com"],
8 ["Wikipedia", "https://en.wikipedia.org"],
9 ["Gmail", "https://mail.google.com"]
10 ],
11 [
12 ["Desmos", "https://www.desmos.com/calculator"],
13 ["Wolfram", "https://wolframalpha.com"],
14 ["Hacker News", "https://news.ycombinator.com"]
15 ],
16 [
17 ["Reddit", "https://www.reddit.com"],
18 ["YouTube", "https://youtube.com"],
19 ["Instagram", "https://instagram.com"]
20 ]
21 ],
22"l1name": "General",
23"l2name": "Productivity",
24"l3name": "Social"},
25 userListsCallback);
26});
27
28function columnToArray(list) {
29 var l = [];
30 var elem = list.getElementsByClassName("item");
31 for (var i = 0; i < elem.length; ++i) {
32 l[i] = [elem[i].innerText, elem[i].getAttribute("href")];
33 }
34 return l;
35}
36
37function listToArray(list) {
38 var l = [];
39 var elem = list.getElementsByTagName("li");
40 for (var i = 0; i < elem.length; ++i) {
41 l[i] = [elem[i].getElementsByTagName("a")[0].innerText, elem[i].getElementsByTagName("a")[0].getAttribute("href")];
42 }
43 return l;
44}
45
46var userListsCallback = function(lists) {
47 mainlist = [lists["l1name"],lists["l2name"],lists["l3name"]]
48 for(var i=0;i<lists["lists"].length;i++) {
49 var ul = document.createElement("ul");
50 ul.setAttribute("id", mainlist[i]);
51 document.getElementById("links").appendChild(ul);
52
53 var title = document.createElement("div");
54 title.setAttribute("class", "title");
55 ul.appendChild(title);
56
57 var p = document.createElement("p");
58 p.innerText = mainlist[i];
59 title.appendChild(p);
60
61 title.insertAdjacentHTML("beforeend", "<span class='add' id='add-"+mainlist[i]+"'>+</span>");
62
63 var list = lists["lists"][i];
64 for(var j=0;j<lists["lists"][i].length;j++) {
65 var li = document.createElement("li");
66 li.setAttribute("id", mainlist[i]+"-"+j);
67 var siteurl = list[j][1];
68 var name = list[j][0];
69 var img = document.createElement("img");
70 img.className = "icon";
71 img.src = "http://www.google.com/s2/favicons?domain="+siteurl+"";
72 li.insertAdjacentHTML("beforeend", "<a class='item' href="+siteurl+"><img src="+img.src+" alt="+extractDomain(siteurl,1)+"/> "+name+"</a>");
73 li.insertAdjacentHTML("beforeend", rmspan[0] + j + "-" + mainlist[i] + rmspan[1]);
74 ul.appendChild(li);
75 }
76
77 var sortable = Sortable.create(ul, {
78 group: "userlists",
79 onUpdate: function (evt) {
80 save();
81 }
82 });
83 }
84
85 menu();
86
87};
88
89document.onkeyup=function(e){
90 var e = e || window.event; // for IE to cover IEs window event-object
91 fields = document.getElementsByClassName("new");
92 if(e.which == 27 && fields != null) {
93 fields[fields.length-1].remove();
94 return false;
95 }
96 if(e.which == 27 && fields != null) {
97 fields[fields.length-1].remove();
98 return false;
99 }
100}
101
102function listen(li) {
103 li.addEventListener('click', function(event){
104 var r = event.target.id.split("-");
105 if (r[0] == "delete") {
106 var el = document.getElementById(r[2]+"-"+r[1]);
107 el.outerHTML = "";
108 delete el;
109 save();
110 } else {
111 var ul = document.getElementById(r[1]);
112 var li = document.createElement("li");
113 li.setAttribute("class", "new");
114 li.insertAdjacentHTML("beforeend", "<span class='save' tabindex='3' id='input-"+columnToArray(li).length.toString()+"'>✔</span><input type='text' class='name' value='' placeholder='name' tabindex='1'><br /><input type='text' class='url' value='' placeholder='url' tabindex='2' id='form-"+columnToArray(li).length.toString()+"'>");
115 ul.appendChild(li);
116 var span = document.getElementById("input-"+columnToArray(li).length.toString());
117 var form = document.getElementById("form-"+columnToArray(li).length.toString());
118 var name = document.getElementsByClassName("name")[0].focus();
119 span.addEventListener('click', function(event){
120 var li = document.getElementsByClassName("new")[0]
121 var ul = li.parentNode;
122 if (li.getElementsByClassName("name")[0].value != "" || li.getElementsByClassName("url")[0].value != "") {
123 var newli = document.createElement("li");
124 newli.setAttribute("id",r[1]+"-"+columnToArray(ul).length.toString());
125 var siteurl = addhttp(li.getElementsByClassName("url")[0].value);
126 var name = li.getElementsByClassName("name")[0].value;
127 li.remove()
128 delete li;
129
130 newli.insertAdjacentHTML("beforeend", "<a href="+siteurl+">"+name+"</a>");
131 newli.insertAdjacentHTML("beforeend", rmspan[0]+columnToArray(ul).length.toString()+"-"+r[1]+rmspan[1]);
132 document.getElementById(r[1]).appendChild(newli);
133 save();
134 listen(newli);
135 }
136 });
137 span.addEventListener('keypress', function(e) {
138 if (e.keyCode == 13) {
139 span.click();
140 }
141 });
142 }
143 }, false);
144}
145
146function menu() {
147 var allUserLi = document.querySelectorAll('.remove, .add');
148
149 allUserLi.forEach(function(li, p_index){
150 listen(li);
151 });
152}
153
154function save(l) {
155 var set = l || JSON.parse(JSON.stringify(mainlist));
156 d = []
157 d = set;
158 console.log("Saving settings");
159 for (var i = 0; i < set.length; ++i) {
160 d[i] = listToArray(document.getElementById(set[i]));
161 }
162 chrome.storage.sync.set( {"lists": d} );
163}