Copy & Cite Bookmarklet

I don't know why it is such a hard task to create a browser extension for copy & cite. Since none work to my liking, here is a simple bookmarklet. (This posting is mostly so I don't loose the code.)
var selection = "";
var ranges = window.getSelection();
if ( ranges.rangeCount > 0 ) {
   for ( var i = 0; i < ranges.rangeCount; i++ ) {
      if ( i > 0 ) {
          selection += "\n\n";
      }
      selection += ranges.getRangeAt(i);
   }
}
else {   
   var titles = document.getElementsByTagName("title");
   if ( titles.length > 0 ) {
      selection = titles[0].textContent;
   }
}
var subject = "FYI:" + selection.substr(0,50) + (selection.length > 50 ? "..." : "");
var body = selection + "\n\n" +window.location.href;
window.open("mailto:?subject="+encodeURI(subject)+"&body="+encodeURI(body),"_self");

Use the tool at http://mrcoles.com/bookmarklet/ to create your own bookmarklet.

No comments: