17fbdbd8172dda354a7ad3a4e4f01d8da0463b23
   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        ["syncthing", "https://localhost:8384/"],
  11        ["todoist", "http://todoist.com/"],
  12        ["github", "https://github.com/"],
  13        ["gmail", "http://mail.google.com/"],
  14        ["drive", "http://drive.google.com/"]
  15    ],
  16    [
  17        ["unixporn", "http://reddit.com/r/unixporn"],
  18        ["news", "http://reddit.com/r/news"],
  19        ["funny", "http://reddit.com/r/funny"],
  20        ["linux", "http://reddit.com/r/linux"],
  21        ["all", "http://reddit.com/r/all"]
  22    ],
  23    [
  24        ["elementary", "https://elementary.io/"],
  25        ["ubuntu", "https://www.ubuntu.com/"],
  26        ["fedora", "https://getfedora.org/"],
  27        ["mint", "https://linuxmint.com/"],
  28        ["arch", "https://www.archlinux.org/"]
  29    ]
  30],
  31"l1name": "productivity",
  32"l2name": "general",
  33"l3name": "distros"},
  34        userListsCallback);
  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 div = document.createElement("div");
  50                div.setAttribute("class", "favorites-list users-list");
  51                document.getElementById("links").appendChild(div);
  52
  53                var ul = document.createElement("ul");
  54                ul.setAttribute("id", mainlist[i]);
  55                div.appendChild(ul);
  56
  57                var title = document.createElement("div");
  58                title.setAttribute("class", "title");
  59                ul.appendChild(title);
  60
  61                var p = document.createElement("p");
  62                p.innerText = mainlist[i];
  63                title.appendChild(p);
  64
  65                title.insertAdjacentHTML("beforeend", "<span id='add-"+mainlist[i]+"'>+</span>");
  66
  67                var list = lists["lists"][i];
  68                for(var j=0;j<lists["lists"][i].length;j++) {
  69                        var li = document.createElement("li");
  70                        li.setAttribute("id", mainlist[i]+"-"+j);
  71                        var siteurl = list[j][1];
  72                        var name = list[j][0];
  73                        // ronmurphy change start
  74                        var img = document.createElement("img");
  75                                                img.className = "icon";
  76                                                img.src = "http://www.google.com/s2/favicons?domain="+siteurl+"";
  77                        li.insertAdjacentHTML("beforeend", "<a href="+siteurl+"> <img src="+img.src+" alt="+extractDomain(siteurl,1)+"/> "+name+"</a>");
  78                        // ronmurphy end
  79                        //li.insertAdjacentHTML("beforeend", "<a href="+siteurl+">"+name+"</a>");
  80                        li.insertAdjacentHTML("beforeend", "<span id='delete-"+j+"-"+mainlist[i]+"'>-</span>");
  81                        ul.appendChild(li);
  82                }
  83
  84                var sortable = Sortable.create(ul, {
  85                        group: "userlists",
  86                        onUpdate: function (evt) {
  87                                save();
  88                        }
  89                });
  90        }
  91
  92        menu();
  93
  94};
  95
  96function listen(li) {
  97        li.addEventListener('click', function(event){
  98                var r = event.target.id.split("-");
  99                if (r[0] == "delete") {
 100                        var el = document.getElementById(r[2]+"-"+r[1]);
 101                        el.outerHTML = "";
 102                        delete el;
 103                        save();
 104                } else {
 105                        var ul = document.getElementById(r[1]);
 106                        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>");
 107                        var inp = document.getElementById("div-"+listToArray(ul).length.toString());
 108                        var span = document.getElementById("input-"+listToArray(ul).length.toString());
 109                        var form = document.getElementById("form-"+listToArray(ul).length.toString());
 110
 111                        form.addEventListener('keyup', function(event){
 112                                if (event.keyCode == 13) {
 113                                        span.click();
 114                                }
 115                        });
 116
 117                        span.addEventListener('click', function(event){
 118                                inp.outerHTML = "";
 119                                delete inp;
 120
 121                                if (inp.getElementsByClassName("name")[0].value != "" || inp.getElementsByClassName("url")[0].value != "") {
 122                                        var li = document.createElement("li");
 123                                        li.setAttribute("id",r[1]+"-"+listToArray(ul).length.toString());
 124                                        //var splitted = splitHostname(inp.getElementsByClassName("url")[0].value); fix later
 125                                        var siteurl = addhttp(inp.getElementsByClassName("url")[0].value);
 126                                        var name = inp.getElementsByClassName("name")[0].value;
 127
 128                                        li.insertAdjacentHTML("beforeend", "<a href="+siteurl+">"+name+"</a>");
 129                                        li.insertAdjacentHTML("beforeend", "<span id='delete-"+listToArray(ul).length.toString()+"-"+r[1]+"'>-</span>");
 130                                        document.getElementById(r[1]).appendChild(li);
 131                                        save();
 132                                        listen(li);
 133                                }
 134                        });
 135                }
 136        }, false);
 137}
 138
 139function menu() {
 140        var allUserLi = document.querySelectorAll('.users-list > ul > li > span, .users-list > ul > .title > span');
 141
 142        allUserLi.forEach(function(li, p_index){
 143        listen(li);
 144    });
 145}
 146
 147function save(l) {
 148        var set = l || JSON.parse(JSON.stringify(mainlist));
 149        d = []
 150        d = set;
 151        for (var i = 0; i < set.length; ++i) {
 152                e = document.getElementById(set[i]);
 153                d[i] = listToArray(e);
 154        }
 155        chrome.storage.sync.set( {"lists": d} );
 156}