157c99091aa716c9d3093eb3da6a9ee42e29728a
   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    ul.setAttribute('draggable', 'true');
  52    ul.addEventListener('dragstart', dragStart, false);
  53    ul.addEventListener('dragenter', dragEnter, false);
  54    ul.addEventListener('dragover', dragOver, false);
  55    ul.addEventListener('dragleave', dragLeave, false);
  56    ul.addEventListener('drop', drop, false);
  57    document.getElementById("links").appendChild(ul);
  58
  59    var grip = document.createElement("span");
  60    grip.setAttribute("class", "grip");
  61
  62    var title = document.createElement("div");
  63    title.setAttribute("class", "title");
  64    title.appendChild(grip)
  65    ul.appendChild(title);
  66
  67    var p = document.createElement("p");
  68    p.innerText = mainlist[i];
  69    title.appendChild(p);
  70
  71    title.insertAdjacentHTML("beforeend", "<span class='add' id='add-"+mainlist[i]+"'>+</span>");
  72
  73    var list = lists["lists"][i];
  74    for(var j=0;j<lists["lists"][i].length;j++) {
  75      var li = document.createElement("li");
  76      li.setAttribute("id", mainlist[i]+"-"+j);
  77      var siteurl = list[j][1];
  78      var nme = list[j][0];
  79      var img = document.createElement("img");
  80      img.className = "icon";
  81      img.src = "https://www.google.com/s2/favicons?domain="+siteurl+"";
  82      li.insertAdjacentHTML("beforeend", "<a class='item' href="+siteurl+"><img src="+img.src+" alt="+extractDomain(siteurl,1)+"/> "+nme+"</a>");
  83      li.insertAdjacentHTML("beforeend", rmspan[0] + j + "-" + mainlist[i] + rmspan[1]);
  84      ul.appendChild(li);
  85    }
  86
  87    new Sortable(ul, {
  88      group: "userlists",
  89      animation: 150,
  90      onUpdate: function (evt) {
  91        save();
  92      }
  93    });
  94  }
  95
  96  menu();
  97
  98};
  99
 100function dragStart(e) {
 101  console.log("Drag started for " + this);
 102  console.log(this);
 103  dragSrcEl = this;
 104  e.dataTransfer.effectAllowed = 'move';
 105  e.dataTransfer.setData('text/html', this.innerHTML);
 106}
 107
 108function dragOver(e) {
 109  console.log("Object " + this + " is ready to be placed");
 110  if (e.preventDefault) {
 111    e.preventDefault();
 112  }
 113  e.dataTransfer.dropEffect = 'move';
 114  return false;
 115}
 116
 117function dragEnter(e) {
 118  this.classList.add('over');
 119}
 120
 121function dragLeave(e) {
 122  this.classList.remove('over');
 123}
 124
 125function drop(e) {
 126  if (e.stopPropagation); {
 127    e.stopPropagation();
 128  }
 129  if (dragSrcEl != this) {
 130    dragSrcEl.innerHTML = this.innerHTML;
 131    this.innerHTML = e.dataTransfer.getData('text/html');
 132  }
 133  console.log("Saving positions");
 134  save();
 135  return false;
 136}
 137
 138var dragSrcEl = null;
 139
 140document.onkeyup=function(e){
 141  var e = e || window.event; // for IE to cover IEs window event-object
 142  fields = document.getElementsByClassName("new");
 143  if(e.which == 27 && fields != null) {
 144    fields[fields.length-1].remove();
 145    return false;
 146  }
 147  if(e.which == 13 && fields.length > 0) {
 148    fields[fields.length-1].getElementsByClassName("save")[0].click();
 149    return false;
 150  }
 151}
 152
 153function listen(li) {
 154  li.addEventListener('click', function(event){
 155    var r = event.target.id.split("-");
 156    if (r[0] == "delete") {
 157      var el = document.getElementById(r[2]+"-"+r[1]);
 158      el.outerHTML = "";
 159      delete el;
 160      save();
 161    } else {
 162      var ul = document.getElementById(r[1]);
 163      console.log(ul);
 164      if (document.querySelectorAll("#"+r[1]+" .new").length > 0) {
 165        fields = document.querySelector("#"+r[1]+" .new .name");
 166        fields.focus(); 
 167        return false;
 168      }
 169      var li = document.createElement("li");
 170      li.setAttribute("class", "new");
 171      li.insertAdjacentHTML("beforeend", "<span class='save' tabindex='3' id='input-"+columnToArray(li).length.toString()+"'>&#x2714;</span><input type='text' class='name' value='' placeholder='name' tabindex='1'><br /><input type='url' spellcheck=false class='url' value='' placeholder='url' tabindex='2' id='form-"+columnToArray(li).length.toString()+"'>");
 172      ul.appendChild(li);
 173      var span = document.getElementById("input-"+columnToArray(li).length.toString());
 174      var form = document.getElementById("form-"+columnToArray(li).length.toString());
 175      span.addEventListener('click', function(event){
 176        var li = document.getElementsByClassName("new")[0]
 177        var ul = li.parentNode;
 178        if (li.getElementsByClassName("name")[0].value != "" && li.getElementsByClassName("url")[0].value != "" && li.getElementsByClassName("url")[0].validity.typeMismatch== false) {
 179          var newli = document.createElement("li");
 180          newli.setAttribute("id",r[1]+"-"+columnToArray(ul).length.toString());
 181          var siteurl = addhttp(li.getElementsByClassName("url")[0].value);
 182          var nme = li.getElementsByClassName("name")[0].value;
 183          li.remove()
 184          delete li;
 185
 186          newli.insertAdjacentHTML("beforeend", "<a href="+siteurl+">"+nme+"</a>");
 187          newli.insertAdjacentHTML("beforeend", rmspan[0]+columnToArray(ul).length.toString()+"-"+r[1]+rmspan[1]);
 188          document.getElementById(r[1]).appendChild(newli);
 189          save();
 190          listen(newli);
 191        }
 192        else {
 193          if (li.getElementsByClassName("name")[0].value == "" && li.getElementsByClassName("url")[0].value == "") {
 194            console.log("No data supplied, deleting form");
 195            this.parentNode.remove();
 196          }
 197          else {
 198            console.log("Missing data, press Esc to delete form");
 199          }
 200        }
 201      });
 202      span.onmouseover = function() {
 203        nme = document.getElementsByClassName("name")[0];
 204        url = document.getElementsByClassName("url")[0];
 205        if (nme.value === ''  || url.value === '' || url.validity.typeMismatch == true) {
 206          this.style.background = "#bf616a";
 207        }
 208        else {
 209          this.style.background = "#a3be8c";
 210        }
 211      };
 212      span.onmouseout = function() {
 213        this.style.background = "#848ead";
 214      };
 215      fields = document.querySelector("#"+r[1]+" .new .name");
 216      fields.focus(); 
 217    }
 218  }, false);
 219}
 220
 221function menu() {
 222  var allUserLi = document.querySelectorAll('.remove, .add');
 223
 224  allUserLi.forEach(function(li, p_index){
 225    listen(li);
 226  });
 227}
 228
 229function save(l) {
 230  var set = l || JSON.parse(JSON.stringify(mainlist));
 231  d = []
 232  d = set;
 233  console.log("Saving settings");
 234  for (var i = 0; i < set.length; ++i) {
 235    d[i] = listToArray(document.getElementById(set[i]));
 236  }
 237  chrome.storage.sync.set( {"lists": d} );
 238}