// ==UserScript==
// @name                Forum user links
// @version             1.2
// @date                2008-11-13
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         For each post in a fourm, adds links to the poster's profle, convos you've had with them, and a link to start a new convo
// @include             http://www.etsy.com/forums_thread.php?*
// ==/UserScript==

/*
This script adds some extra links to each forum post, to speed up replying off-forum
*/

// Build a couple of regexps that we're going to need
var isForumLink = /post-(\d+)/;
var extractUserId = /user_id=(\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 ID and name from the shop link
    var match = extractUserId.exec( anchors[ a + 2 ].href );
    var userId = match[ 1 ];
    var userName = anchors[ a + 2 ].innerHTML;
    // Build the HTML
    html = document.createElement( 'div' );
    html.style.marginTop = "5px";
    html.innerHTML += '<a style="color: #0192B5 !important" href="/profile.php?user_id=' + userId + '">' + userName + '\'s profile</a> | ';
    html.innerHTML  += '<a style="color: #0192B5 !important" href="/add_favorite_shop.php?user_id=' + userId + '">add to favorites</a> | ';
    html.innerHTML  += '<a style="color: #0192B5 !important" href="/convo_new.php?to_username=' + userName + '">send convo</a> | ';
    html.innerHTML  += '<a style="color: #0192B5 !important" href="/convo_drop_search.php?drop_user_name=' + userName + '&drop_tag=&submit=Go">list convos</a></div>';
    // Add it to the parent cell and move on
    a += 3;
    while ( anchors[ a ].href.indexOf( 'forum_report.php?post_id=' + postId ) == -1 ) {
        a++;
    }

    anchors[ a ].parentNode.appendChild( html );
}    


