// ==UserScript==
// @name        Lectionary Linker
// @namespace   http://www.longhighway.com/monkeyisland
// @description Add links from Father Kevin Michael Laughery's Roman Catholic liturgical calandar pages to Joel P. Anderson's (AKA "Mr. Klingon") Word English, Douay, and Vulgate Bible texts.
// @include     http://www.kevinlaughery.com/lc20*
// ==/UserScript==

(

function() {
  // Return a link to Bible.MrKlingon.org using the specified citation 
  function createLink(citation) {
   var url = "http://mrklingo.freeshell.org/verbumdomini.php?verses=" + citation;
   var link = document.createElement('a');
   link.setAttribute('href', url);
   link.setAttribute('target', '_blank');
   link.appendChild(document.createTextNode(' Readings at Bible.MrKlingon.org'));
   return link;
  }
 
  // Find citations
  var pattern = "/html/body/a/i";
  var citationLines = document.evaluate(pattern, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  if (citationLines.snapshotLength == 0) {
    pattern = "/html/body/p/i";
    citationLines = document.evaluate(pattern, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  }

  // Append link to each citation
  var i = 0;
  while ( (aCitationLine = citationLines.snapshotItem(i) ) != null ) {
    var readingsList = aCitationLine.innerHTML;
    aCitationLine.appendChild(createLink(readingsList)); 
    i++ ;
  }

}

)();
