// ==UserScript==
// @name          CiteDrag
// @version       0.0.3
// @author        Elijah Grey, http://eligrey.com
// @namespace     http://eligrey.com/projects/citedrag/#userscript
// @description   Adds citations to dragged content.
// @license       GPL v3 and X11/MIT license - http://eligrey.com/about/license/
// @archive       http://code.eligrey.com/citedrag/
// @include       *
// ==/UserScript==

  document.addEventListener("dragstart", function(evt) {
    if (typeof evt.dataTransfer != "undefined") {
      var dt = evt.dataTransfer,
      originName = (document.title||location.hostname);

      // ==modifications for UserScript==
      // If website already uses CiteDrag, restore data to original state if given

      if (dt.getData("text/x-original-html"))
        dt.setData("text/html", dt.getData("text/x-original-html"));
      if (dt.getData("text/x-original-plain"))
        dt.setData("text/plain", dt.getData("text/x-original-plain"));
      // ==/modifications for UserScript==

      // store original data in case a website wants it
      if (dt.getData("text/html"))
        dt.setData("text/x-original-html", dt.getData("text/html"));
      if (dt.getData("text/plain"))
        dt.setData("text/x-original-plain", dt.getData("text/plain"));

      // for some crazy reason, WebKit thinks a page shouldn't be allowed to access dt.getData of a drag originating FROM THE SAME PAGE
      // https://bugs.webkit.org/show_bug.cgi?id=23695
      
      
      if (dt.getData("text/uri-list")) { // dragged an image or link
        
        if (dt.getData("text/x-moz-url")) { // mozilla link list format; supports #comments
          var uriCitation = "\n#via " + originName + " ( " + location.href + " )",
          uriList = dt.getData("text/x-moz-url")
            .replace(/\n#.*/g, "") // remove comments
            .split(/\n/).join(uriCitation) + uriCitation; // add citations as comments
          dt.setData("text/x-moz-url", uriList);
          /* example:
          http://foo.example/
          http://bar.example/
          
          becomes:
          http://foo.example/
          #via site ( uri )
          http://bar.example/
          #via site ( uri )
          */
        }
        
        if (dt.getData("text/html"))
          dt.setData("text/html", // link via <a href={uri}>site</a>
            dt.getData("text/html") + ' via <a href="' +location.href + '" title="' + location.hostname + '">' + originName + "</a>"
          );

        if (dt.getData("text/plain"))
          dt.setData("text/plain", // uri via site ( uri )
            dt.getData("text/plain") + " via " + originName+' ( '+location.href+' )'
          );
      } else if (dt.getData("text/plain")) { // dragged html or plain text
      
        if (dt.getData("text/html"))
          dt.setData("text/html", // <blockquote.../> - <a href={uri}>site</a>
            '<blockquote cite="'+location.href+'">' + dt.getData("text/html") + '</blockquote> \u2015 <a title="' +location.host + '" href="' + location.href + '">' + originName + "</a>"
          );

        if (dt.getData("text/plain"))
          dt.setData("text/plain", // "content" - site ( uri )
            "\u201C" + dt.getData("text/plain") + "\u201D\n  \u2015 " + originName + " ( " + location.href + " )"
          );
      }
    }
  }, false);

