// ==UserScript==
// @name                Printer-friendly linker
// @version             1.3
// @date                2010-06-18
// @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-- ) {
    var anchor = anchors[ a ];
    if ( anchor.href.match( '/your/orders/' ) ) {
        var order_id = anchor.href.match( /\/your\/orders\/(\d+)/ )[ 1 ];
        addLink( anchor, order_id );
    } else if ( anchor.href.match( 'receipt.php' ) ) {
        var order_id = anchor.href.match( /order_id=(\d+)/ )[ 1 ];
        addLink( anchor, order_id );
    }
}

function addLink ( anchor, order_id ) {
    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;
}



