// ==UserScript==
// @name                Quote post
// @version             2.0
// @date                2008-11-14
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds a "quote post" link to each post in a forum, which quotes the text of the post when you click on it.
// @include             http://www.etsy.com/forums_thread.php?*
// ==/UserScript==

/*
This hack adds a quick "quote post" link to each forum post. Clicking the link inserts the post's text
into the box at the bottom, ready for your reply.
*/

// Add an anchor to the "post" box at the bottom of the page, and stash a reference to the box itself.
var form = document.forms[ 1 ];
var postBox;
if ( form != null ) {
    postBox = form.elements.namedItem( 'post' );
    var postAnchor = document.createElement( 'a' );
    postAnchor.name = 'post';
    form.insertBefore( postAnchor, form.firstChild );
    decoratePosts();
}

function decoratePosts () {
    // Build a regexp that we're going to need
    var isForumLink = /post-(\d+)/;

    // Each forum post has an anchor tag, which happens to be in the table
    // cell we want to decorate. So, we go through all the anchors finding
    // ones that are forum post identifiers.
    var anchors = document.getElementsByTagName( 'a' );
    for ( var a = 0; a < anchors.length; a++ ) {
        var anchor = anchors[ a ];
        if ( ! anchor.name ) continue;
        var match = isForumLink.exec( anchor.name );
        if ( ! match ) continue;
        var postId = match[ 1 ];

        // Get the user name from the shop link
        var userName = anchors[ a + 2 ].innerHTML;

        // Jump through a few hoops to find the cell with the post's text in it
        // and extract the text content
        var row = anchors[ a + 2 ].parentNode.parentNode;
        var textCell = row.parentNode.rows[ row.rowIndex + 1 ].cells[ 0 ];
        var text = toText( textCell );
        text = text.replace( /\s+$/, '' );
        text += '\n__________\n\n';

        // Find the "report post" link - we'll add our link after it
        a += 3;
        while ( anchors[ a ].href.indexOf( 'forum_report.php?post_id=' + postId ) == -1 ) {
            a++;
        }

        // Build the HTML
        var fragment = document.createDocumentFragment();
        fragment.appendChild( document.createTextNode( ' - ' ) );
        var quote = document.createElement( 'a' );
        quote.addEventListener( "click", generateQuoteFn( userName, text ), true );
        quote.href = '#post_box';
        quote.innerHTML = 'Quote this post';
        fragment.appendChild( quote );
        anchors[ a ].parentNode.insertBefore( fragment, anchors[ a ].nextSibling );
    }
}

// Generates a closure to stash the text and username for each post

function generateQuoteFn( userName, text ) {
    return function () {
        postBox.value = userName + ' said:\n' + text;
    }
}    

// Quick and dirty recursive "to text" function - extract all the text content, and
// replace <br /> with newlines.

function toText( node ) {
    var text = "";
    if ( node.nodeType == 3 ) {
        text += node.data;
    } else if ( node.nodeType == 1 && node.nodeName == 'BR' ) {
        text += '\n';
    }
    if ( node.firstChild != null ) {
        text += toText( node.firstChild );
    }
    if ( node.nextSibling != null ) {
        text += toText( node.nextSibling );
    }
    return text;
}


