Created
November 19, 2012 14:36
-
-
Save coolniikou/4110969 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="./jquery.innerfade.js"></script> | |
(function($){ | |
$.fn.jqTweet = function(options) { | |
options = $.extend( {}, $.fn.jqTweet.options, options ); | |
return this.eatch(function() { | |
var elem = $(this) | |
, o = options | |
, api_uri = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' | |
, json_uri = api_uri + o.username + '&count=' + o.count + '&callback=?' | |
, jtweet = '<ul id="tweet-list">'; | |
elem.append('<p id="jqtw_loader" style="display:none;">' + o.loader_text + '</p>'); | |
$("#jqtw_loader").fadeIn('slow'); | |
$.getJSON(json_uri, | |
function(data) { | |
for(var i in data) { | |
var tweet = formatTweet(data[i].text) | |
, username = data[i].user.screen_name | |
, icon = data[i].user.profile_image_url | |
, time = relative_time(data[i].created_at); | |
jtweet += '<li><div class="tweet"><img src="'; | |
jtweet += icon + '" /><p><span class="tweet-text">'; | |
jtweet += tweet + '</span><br/><a href="http://twitter.com/'; | |
jtweet += username + '/status/' + data[i].id + '">'; | |
jtweet += time + '</a></p></div></li>'; | |
} | |
jteet += '</ul>'; | |
elem.empty().html(list); | |
$("#jqtw_loader").fadeOut('fast'); | |
$('#tweet-list').innerfade({ | |
speed: 2000, | |
timeout: 10000, | |
wait: 3000, | |
}); | |
}); | |
}); | |
function formatTweet(str) { | |
var str = ' '+ str; | |
str = str.replace(/(https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/, | |
function(u) { | |
var shortUrl = (u.length > 30) ? u.substr(0, 30) + '...': u; | |
return '<a href="' + u + '">' + shortUrl + '</a>'; | |
}) | |
.replace(/@([a-zA-Z0-9_]+)/g,'@<a href="http://twitter.com/$1">$1</a>') | |
.replace(/(?:^|\s)#([^\s\.\+:!]+)/g, | |
function (a, u) { | |
return ' <a href="http://twitter.com/search?q=' + encodeURIComponent(u) + '">#' + u + '</a>'; | |
}); | |
return str; | |
} | |
function relative_time(dateString) { | |
var rightNow = new Date(); | |
var then = new Date(dateString); | |
var browser = function() { | |
var ua = navigator.userAgent; | |
return {ie: ua.match(/MSIE\s([^;]*)/)}; | |
}; | |
if (browser.ie) { | |
then = Date.parse(dateString.replace(/( \+)/, ' UTC$1')); | |
} | |
var diff = rightNow - then; | |
var second = 1000, | |
minute = second * 60, | |
hour = minute * 60, | |
day = hour * 24, | |
week = day * 7; | |
if (isNaN(diff) || diff < 0) { | |
return ""; // return blank string if unknown | |
} | |
if (diff < second * 7) { | |
return "right now"; | |
} | |
if (diff < minute) { | |
return Math.floor(diff / second) + " seconds ago"; | |
} | |
if (diff < minute * 2) { | |
return "about 1 minute ago"; | |
} | |
if (diff < hour) { | |
return Math.floor(diff / minute) + " minutes ago"; | |
} | |
if (diff < hour * 2) { | |
return "about 1 hour ago"; | |
} | |
if (diff < day) { | |
return Math.floor(diff / hour) + " hours ago"; | |
} | |
if (diff > day && diff < day * 2) { | |
return "yesterday"; | |
} | |
if (diff < day * 365) { | |
return Math.floor(diff / day) + " days ago"; | |
} | |
else { | |
return "over a year ago"; | |
} | |
} | |
} | |
$.fn.jqTweet.options = { | |
username : 'cool_ni_ikou', | |
count : 5, | |
loader_text: 'loading new tweets' | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment