Twitter widget changes

Below is the source code for my twitter widget as you see on the top of the page.  This makes a simple JSONP call to Twitter with JQuery.

The change I made today was to read Mentions, Lists, and URLs and convert them to respective URLs. 

 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var last_tweet;
    var twitterAccount = "dannygnj";
    var divClass = "twitterStatus"
    
    $(function() {
        $.getScript("http://twitter.com/statuses/user_timeline/"+twitterAccount+".json?callback=twitterCallback2&amp;;count=1");
    });
    
    function twitterCallback2(data) {    
        last_tweet = twitterize(data[0].text);
        $("." + divClass).html("<img src='/dannyg/pics/twitter_icon.png' style='vertical-align:text-top;'><a href='http://twitter.com/dannygnj' target='_blank'>Last Tweet:</a> " + last_tweet);            
    }
    
    function twitterize(str) {
        var final = str;
        var re = /http://[A-Za-z0-9_./]*/g;
        var result = final.match(re);    
        if (result != null) {
            for (var i = 0; i < result.length; i++) {
                final = final.replace(result[i], "<a href='" + result[i] + "' target='_blank'>" + result[i] + "</a>");                        
            }
        }
        var re = /@[A-Za-z0-9_]*/g;
        var result = final.match(re);
        if (result != null) {
            for (var i = 0; i < result.length; i++) {
                var xword = result[i].replace("@","");
                final = final.replace(result[i], "@<a href='http://twitter.com/" + xword + "' target='_blank'>" + xword + "</a>");                        
            }
        }
        var re = /#[A-Za-z0-9_]*/g;
        var result = final.match(re);    
        if (result != null) {
            for (var i = 0; i < result.length; i++) {
                var xword = result[i].replace("#","");
                final = final.replace(result[i], "#<a href='http://twitter.com/#search?q=%23" + xword + "' target='_blank'>" + xword + "</a>");                        
            }    
        }
        return final;    
    }    
    </script>     

Leave a Reply