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