// ==UserScript==
// @name                Printer-friendly linker
// @version             1.2
// @date                2010-06-11
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds a link to the printer-friendly receipt to each order on the orders page
// @include             http://www.etsy.com/your/listings/sold*
// ==/UserScript==

var anchors = document.getElementsByTagName( 'a' ); // get all the links

// go in reverse order, so that new links don't throw us off
for ( var a = anchors.length - 1; a >= 0; a-- ) {
    if ( anchors[ a ].href.match( '/your/orders/' ) ) {
        var anchor = anchors[ a ];
        var order_id = anchor.href.match( /\/your\/orders\/(\d+)/ )[ 1 ];
        var print = anchor.cloneNode( true );
        anchor.parentNode.insertBefore( print, anchor.nextSibling );
        anchor.parentNode.insertBefore( document.createElement( 'br' ), anchor.nextSibling );
        print.innerHTML = 'Print';
        print.href = '/receipt_print.php?order_id=' + order_id;
    }
}

