// ==UserScript==
// @name                Etsy buyer info
// @version             3.0
// @date                2008-12-08
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds links to buyer's email, convos, and feedback on to the Etsy receipt page for an order
// @include             http://www.etsy.com/receipt.php?order_id=*
// ==/UserScript==
//

var showEmail = GM_getValue( 'showEmail' );
if ( showEmail == null ) showEmail = false;

var tIdx = 21;
if ( window.location.search.match( 'show_panel=true' ) ) {
    tIdx++;
}

var tables = document.getElementsByTagName( 'table' );
var buyer = tables[ tIdx ]; // this by experimentation...
var rows = buyer.getElementsByTagName( 'tr' ); // the rows of the table of buyer info

// Add link to convos after username
var userLink = rows[0].getElementsByTagName( 'a' )[0];
var username = userLink.innerHTML;

// Make feedback rating a hyperlink to their feedback page
var feedback = userLink.href.replace( 'profile', 'feedback_public' );
var feedbackTd = rows[ 1 ].getElementsByTagName( 'td' )[0];
var rating = feedbackTd.innerHTML.substring( 13 );
feedbackTd.innerHTML = 'Etsy rating: <a href="' + feedback + '">' + rating + '</a>';

// Make their email address a hyperlink
var mailtoTd = rows[ 3 ].getElementsByTagName( 'td' )[0];
var emailAddress = mailtoTd.innerHTML;
var emailText = ( showEmail ) ? emailAddress : 'email';
mailtoTd.innerHTML = '<a id="mailtoLink" href="mailto:' + mailtoTd.innerHTML + '">' + emailText + '</a> | <a href="convo_new.php?to_username=' + username + '">send convo</a> | <a href="convo_drop_search.php?drop_user_name=' + username + '&drop_tag=&submit=Go">convos</a>';

GM_registerMenuCommand( 'Toggle visible email address', function () {
    showEmail = ! showEmail;
    GM_setValue( 'showEmail', showEmail );
    var mtl = document.getElementById( 'mailtoLink' );
    if ( mtl != null ) {
        mtl.innerHTML = ( showEmail ) ? emailAddress : 'email';
    }
} );


