Mefiquote

By Plutor Last update Mar 12, 2008 — Installed 1,337 times.
// ==UserScript==
// @name           Mefiquote
// @namespace      http://plutor.org/
// @description    Adds "quote" links to Metafilter comments.
// @include        http://metafilter.com/*
// @include        http://*.metafilter.com/*
// ==/UserScript==
//
// DONE 2008-03-12
// * Don't strip the domain off the URl
// * Play nicely with mark contact contributions

var BUTTONTEXT = 'quote';
var QUOTEFORMAT = '<a href="%l">%n</a>: "<i>%q</i>"';

/* ======================================================================== */

function mq_quotethis(evt) {
    var commenttextarea = document.getElementById('comment');
    if (!commenttextarea) return;

    var quotelink = evt.target;
    var metadata = quotelink.parentNode;
    var comment = metadata.parentNode;

    // Get all of the data to fill in placeholders
    var quotebits = new Object;
    quotebits['%'] = '%';

    quotebits.q = new String(comment.innerHTML);
    quotebits.q = quotebits.q.replace(/<br>/ig, '');
    // Remove the trailing metadata
    quotebits.q = quotebits.q.slice(0,
                        quotebits.q.lastIndexOf('<span class="smallcopy">posted by'));
    // Remove the player from music
    quotebits.q = quotebits.q.slice(0,
                        quotebits.q.lastIndexOf('<object '));
    // Remove the more inside junk
    quotebits.q = quotebits.q.replace(/<\/?div[^>]*>/g, '');
    quotebits.q = quotebits.q.replace(/^[ \t\n]*/, '');
    quotebits.q = quotebits.q.replace(/[ \t\n]*$/, '');

    // Default to top of the thread, just in case
    quotebits.l = '#';

    // The rest of the data
    var metabits = metadata.childNodes;
    for (var i=0; i<metabits.length; ++i) {
        if (metabits[i].tagName == 'A' || metabits[i].tagName == 'a') {
            var url = metabits[i].href;
            var path = url.replace(/https?:\/\/([^\/]*\.)?metafilter.com/, '');

            if (path.match(/^\/user\/(\d+)/)) {
                quotebits.i = RegExp.$1;
                quotebits.n = metabits[i].innerHTML;
		quotebits.n = quotebits.n.replace(/<[^>]*>/, '');
                quotebits.p = url;
            } else if (path.match(/#\d+$/)) {
                quotebits.l = url;
            }
        }
    }

    // Replace all of the placeholders
    var quoteregex = new RegExp('%(.)', 'g');
    var quotehtml = new String();
    var lastIndex = 0;
    while ( quoteregex.exec(QUOTEFORMAT) ) {
        var thisIndex = quoteregex.lastIndex;
        quotehtml = quotehtml.concat( QUOTEFORMAT.substr(lastIndex, thisIndex-lastIndex-2) );
        var val = quotebits[QUOTEFORMAT.substr(thisIndex-1, 1)];
        if (val != undefined) {
            quotehtml = quotehtml.concat( quotebits[QUOTEFORMAT.substr(thisIndex-1, 1)] );
        } else {
            quotehtml = quotehtml.concat( '%' + QUOTEFORMAT.substr(thisIndex-1, 1) );
        }

        lastIndex = thisIndex;
    }
    quotehtml = quotehtml.concat( QUOTEFORMAT.substr(lastIndex) );

    // GM_log( quotehtml );
    commenttextarea.value = commenttextarea.value + quotehtml + "\n\n";
}

/* ======================================================================== */

function mq_load_preferences() {
    BUTTONTEXT = GM_getValue('mefiquote_buttontext', BUTTONTEXT);
    QUOTEFORMAT = GM_getValue('mefiquote_quoteformat', QUOTEFORMAT);
}

function mq_save_preferences() {
    var buttontext_el = document.getElementById('mq_buttontext');
    var quoteformat_el = document.getElementById('mq_quoteformat');
    GM_setValue('mefiquote_buttontext', buttontext_el.value);
    GM_setValue('mefiquote_quoteformat', quoteformat_el.value);

    return true; /* So it actually submits, too */
}

/* ======================================================================== */

function mq_escape(str) {
    return str.replace(/"/g, '&quot;');
}

/* ======================================================================== */

function mq_init_preferences() {
    var inputs = document.getElementsByTagName('input');
    var submit_button;

    // Find the submit button
    for (var i=0; i<inputs.length; ++i) {
        if (inputs[i].type == 'submit' && inputs[i].value.match(/Save your Preferences/)) {
            submit_button = inputs[i];
            break;
        }
    }
    if (!submit_button) return;

    // Create the fieldset
    var mefiquote_fieldset = document.createElement('fieldset');
    mefiquote_fieldset.innerHTML = '<legend>MefiQuote preferences</legend>'
        + '<label for="mq_buttontext">Quote button text: </label>'
    	+ '<input type="text" id="mq_buttontext" name="mq_buttontext" value="'
        + mq_escape(BUTTONTEXT)
        + '" maxlength="200" size="30" onfocus="this.style.background=\'#ddd\';" onblur="this.style.background=\'#ccc\';" /><br />'
        + '<label for="mq_quoteformat">Quote format:<br />'
        + '<span class="smallcopy" style="text-align: left">%i - commenter\'s user id<br />%l - url of comment<br />%n - commenter\'s name<br />%p - url of commenter\'s profile<br />%q - comment text<br />%% - an actual percent ("%")</span></label>'
        + '<textarea name="mq_quoteformat" id="mq_quoteformat" cols="60" rows="8" wrap="VIRTUAL" style="width:400px;height:200px;" onfocus="this.style.background=\'#ddd\';" onblur="this.style.background=\'#ccc\';>'
        + mq_escape(QUOTEFORMAT) 
        + '</textarea>';
    submit_button.parentNode.insertBefore( mefiquote_fieldset, submit_button );

    // Add javascript to the form
    submit_button.form.addEventListener( 'submit', mq_save_preferences, true );
}

function mq_init_thread() {
    var commenttextarea = document.getElementById('comment');
    if (!commenttextarea) return;

    var allspans = document.getElementsByTagName('span');

    for (var i=0; i<allspans.length; ++i) {
        var curr = allspans[i];
        if (curr.className == 'smallcopy' && curr.innerHTML.match(/^posted by/) &&
            curr.parentNode.id != 'prevDiv2') {
            // Add the button
            var quotebutton = document.createElement('a');
            quotebutton.href = '#comment';
            quotebutton.innerHTML = BUTTONTEXT;
            quotebutton.target = '_self';
            quotebutton.addEventListener( 'click', mq_quotethis, true );

            curr.appendChild( document.createTextNode('[') );
            curr.appendChild(quotebutton);
            curr.appendChild( document.createTextNode(']') );
        }
    }
}

function mq_init() {
    mq_load_preferences();

    var url = location.href;
    url = url.replace(/https?:\/\/([^\/]*\.)?metafilter.com/, '');

    if (url.match(/^\/(\d+)/)) {
        mq_init_thread();
    } else if (url.match('^/contribute/customize.cfm')) {
        mq_init_preferences();
    }
}

mq_init();