Jump to content

User:Parkerdr/linkpad.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(function () {

document.getElementById('p-tb').innerHTML +=
 '</div></div><br>'+
 '<div id=my class=portlet>'+
   '<h5>LinkPad</h5>'+
   '<div class=pBody><center><span id="spotadd" onDragEnter="stopit()" onDrop="onend()" onDragOver="stopit()" onDblClick="addMe()" title="to add - drag links here or double click to add the current page">[add]</span>&nbsp;&nbsp;&nbsp;<span onDblClick="cleaner()" onDragEnter="stopit()" onDragOver="stopit()" title="to delete - drag links here, dbl click to clear all">[delete]</span></center><ul id="links">'+
 '</ul></div>';
 var oldlinks = getCookie("linkpadlinks");
 var linktable; 
 if (oldlinks != null) { 
     links.innerHTML = oldlinks;
     var linktable = links.getElementsByTagName('a')
     for (var i=0; i<linktable.length; i++)
      linktable[i].ondragend = deleter;
 }    
 linktable = document.getElementById('bodyContent').getElementsByTagName('a');
 for (var i=0; i<linktable.length; i++)
    linktable[i].onclick = clickIt;
   
})
	function stopit() {
            var t = event.dataTransfer.getData("URL");
            if (t != null) {
               window.event.returnValue = false;
            } else
               window.event.returnValue = true;
	}
        function onend() {
            var t = event.dataTransfer.getData("URL");
            if (t != null) {
               adder(t)
            }
            spotadd.bgcolor = "transparent";
        }
        function addMe() {
            adder(window.location.href);
        }
        function adder(t) {
            var li = document.createElement("LI");
            var aa = document.createElement("A");
            var i = t.indexOf("/wiki/");
            var fr;
            if (i > -1)
               fr = t.substring(i + 6);
            else
               fr = t;
            aa.href = t;
            aa.innerText = fr;
            aa.className = "linkpad";
            aa.ondragend = deleter;
            li.appendChild(aa);
            links.appendChild(li);
            linksRecord();
}
        function deleter() {
           var target = event.srcElement;
           var li = target.parentElement;
           if (li.tagName != "LI")
              li = li.parentElement; 
           li.parentElement.removeChild(li);
           linksRecord();           
        }
        function linksRecord() {
            var val = links.innerHTML;
            var expires = new Date();
            expires.setDate(expires.getDate()+7); 
            setCookie("linkpadlinks", val, expires, "/");
        } 
        function cleaner () {
            var expires = new Date(); 
            setCookie("linkpadlinks", "", expires, "/");
            links.innerHTML = "";
        }
        function debugit() {
            alert(links.innerHTML);
        }
// name - name of the cookie
   // * return string containing value
   // of specified cookie or null if cookie
   // does not exist
   function getCookie(name) {
      var prefix = name + "="
      var cookieStartIndex = document.cookie.indexOf(prefix)
      if (cookieStartIndex == -1)
         return null
      var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
         prefix.length)
      if (cookieEndIndex == -1)
         cookieEndIndex = document.cookie.length
      return unescape(document.cookie.substring(cookieStartIndex +
         prefix.length,
   cookieEndIndex))
   }
 
function setCookie (sName, vValue)
{
    var argv = setCookie.arguments, argc = setCookie.arguments.length;
    var sExpDate = (argc > 2) ? "; expires="+argv[2].toGMTString() : "";
    var sPath = (argc > 3) ? "; path="+argv[3] : "";
    var sDomain = (argc > 4) ? "; domain="+argv[4] : "";
    var sSecure = (argc > 5) && argv[5] ? "; secure" : "";
    document.cookie = sName + "=" + escape(vValue,0) + sExpDate + sPath + sDomain + sSecure + ";";
}

function clickIt()  
{
    var e = window.event.srcElement;

    if ((e.tagName == "A") && (window.event.altKey)) {
       adder(e.href);
       window.event.returnValue = false;
       window.status = "Link added to linkpad";
    } 
}