// ==UserScript==
// @name           twitter prepend omg
// @namespace      http://mikejestes.com/greasemonkey/
// @description    Inserts OMG: before messages on twitter
// @include        http://twitter.com/*
// @include        http://quotably.com/*
// @include        http://friendfeed.com/*
// @version        0.5
// ==/UserScript==

(function () {

    var messages, i, mesgEl, OMG, pathMatch, fnc;
    OMG = 'OMG: ';

    if (window.location.host.match(/twitter\.com/)) {
        pathMatch = "//p[@class='entry-title entry-content'] | //span[@class='entry-content']";
        fnc = injectTwitter;
    } else if (window.location.host.match(/quotably\.com/)) {
        pathMatch = "//div[@class='message']";
        fnc = injectQuotably;
    } else if (window.location.host.match(/friendfeed\.com/)) {
        pathMatch = "//div[@class='entry other twitter']/div[@class='link'] | //div[@class='entry owner twitter']/div[@class='link']";
        fnc = injectFriendFeed;
    }

    messages = document.evaluate(pathMatch, 
                                 document, 
                                 null, 
                                 XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, 
                                 null);

    for (i = 0; i < messages.snapshotLength; ++i) {
        mesgEl = messages.snapshotItem(i);
        mesgEl.innerHTML = fnc(mesgEl.innerHTML);
    }

    function injectFriendFeed(content) {
        var newContent;
    
        if (content.match(/@.*@/)) {
            newContent = content.replace(/(@<[^>]+>[^>]+>[, ]*@<[^>]+>[^>]+>)( )/i, "$1 " + OMG);
        } else if (content.match(/@/)) {
            newContent = content.replace(/(@<[^>]+>[^>]+>)( )/i, "$1 " + OMG);
        } else {
            newContent = content.replace(/^(<span>)?“/, '"' + OMG);
        }
        
        return newContent;
    }
    
    function injectTwitter(content) {
        var newContent;
    
        if (content.match(/@.*@/)) {
            newContent = content.replace(/(@<[^>]+>[^>]+>[, ]*@<[^>]+>[^>]+>)( )/i, "$1 " + OMG);
        } else if (content.match(/@/)) {
            newContent = content.replace(/(@<[^>]+>[^>]+>)( )/i, "$1 " + OMG);
        } else {
            newContent = OMG + content;
        }
        
        return newContent;
    }
    
    function injectQuotably(content) {
        var newContent;
    
        if (content.match(/@/)) {
            newContent = content.replace(/(<small.*?<\/small>)/i, "$1 " + OMG);
        } else {
            newContent = content.replace(/(<span.*?<\/span>)/i, "$1 " + OMG);
        }
        
        return newContent;
    }

})();

//
// ChangeLog
// 2008-04-08 - 0.5 - MJE - fix twitter class name update
// 2008-04-08 - 0.4 - MJE - fix OMG before quotes on friendfeed
// 2008-04-07 - 0.3 - MJE - add quotably and friendfeed support
// 2008-04-07 - 0.2 - MJE - include p tags to catch current status
// 

