Report abuse


			
/* create footnotes for all unique off-site links in a document, 
referring repeated links to the existing footnote.

Footnote container appended to document body after the last element.  

screen.css:

.print_only{
  display: none;
} 

include all links, inc on-site: http://pastie.caboo.se/105431

*/

window.addEvent('domready',function(){
 print_footnotes();
});


function print_footnotes(){

  if($$('body a').length < 1){return;};

  var content_links =  $$('body a'); 
  var wrapper = document.body.getChildren().getLast();
  var print_notes = document.createElement('div');
  var notes_h1 = document.createElement('h1');
  var notes_title = document.createTextNode('offsite links:');
  var items_ol = document.createElement('ol');
  print_notes.className = 'print_only';
  print_notes.id = 'footnotes';
  wrapper.appendChild(print_notes);
    print_notes.appendChild(notes_h1);
      notes_h1.appendChild(notes_title);
    print_notes.appendChild(items_ol);

  var footnote_index = 0;

  content_links.each(function(link){

    var uri = link.getProperty('href');
    var dulynoted = false;
    var offsite = false;

    if (uri.contains('//')){
      offsite = true;
      if (uri.contains(location.hostname)){
        offsite = false;
      };
    };

    if (offsite == true){
      $$('#footnotes li').each(function(footnote){
        if (footnote.innerHTML == uri){
          dulynoted = true;
          exisiting_footnote = footnote.getProperty('class');
          create_footnote(exisiting_footnote, false, false)
        };
      });
   };

    if (offsite == true){
      if (dulynoted == false){
        footnote_index = footnote_index + 1;
        create_footnote(footnote_index, true, uri); 
      };
    };

    function create_footnote(position, create, url){

      var footnote_super = document.createElement('sup');
      var super_content = document.createTextNode('['+ position +']');
      footnote_super.className = 'print_only';
      link.appendChild(footnote_super);
        footnote_super.appendChild(super_content);

      if (create == true){
        var item_li = document.createElement('li');
        item_li.className = position;
        var li_content = document.createTextNode(url);
        items_ol.appendChild(item_li);
         item_li.appendChild(li_content);
      };
    };
  });
};

/*

sample html:




  
    
    
    
  
  

    

This is a test yeah

This is a test yeah yeah

This is a test yeah yeah yeah

This is a test woo

This is a test yeah yeah

This is a test yeah yeah yeah

This is a test woo

generated html/dom:

This is a test yeah

This is a test yeah yeah[1]

This is a test yeah yeah yeah[1]

This is a test woo[2]

This is a test yeah yeah

This is a test yeah yeah yeah[3]

This is a test woo[3]

*/