forked from elektronaut/jquery.livetwitter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.livetwitter.min.js
1 lines (1 loc) · 3 KB
/
jquery.livetwitter.min.js
1
(function(a){if(!a.fn.reverse){a.fn.reverse=function(){return this.pushStack(this.get().reverse(),arguments)}}a.fn.liveTwitter=function(c,b){a(this).each(function(){var d={};if(this.twitter){d=jQuery.extend(this.twitter.settings,b);this.twitter.settings=d;if(c){this.twitter.query=c}this.twitter.limit=d.limit;this.twitter.mode=d.mode;if(this.twitter.interval){this.twitter.refresh()}}else{d=jQuery.extend({mode:"search",rate:15000,limit:10,refresh:true},b);if(typeof d.showAuthor=="undefined"){if(d.mode=="search"){d.showAuthor=true}else{d.showAuthor=false}}window.twitter_callback=function(){return true};this.twitter={settings:d,query:c,limit:d.limit,mode:d.mode,interval:false,container:this,lastTimeStamp:0,relativeTime:function(g){var f=Date.parse(g);var i=(Date.parse(Date())-f)/1000;var h="";if(i<60){h=i+" seconds ago"}else{if(i<120){h="a minute ago"}else{if(i<(45*60)){h=(parseInt(i/60,10)).toString()+" minutes ago"}else{if(i<(90*60)){h="an hour ago"}else{if(i<(24*60*60)){h=""+(parseInt(i/3600,10)).toString()+" hours ago"}else{if(i<(48*60*60)){h="a day ago"}else{h=(parseInt(i/86400,10)).toString()+" days ago"}}}}}}return h},refreshTime:function(){var f=this;a(f.container).find("span.time").each(function(){a(this).html(f.relativeTime(this.timeStamp))})},refresh:function(f){var i=this;if(this.settings.refresh||f){var h=encodeURIComponent(this.query);var g="";if(i.mode=="search"){g="http://search.twitter.com/search.json?q="+h+"&callback=?"}else{if(i.mode=="user_timeline"){g="http://twitter.com/statuses/user_timeline/"+h+".json?count="+i.limit+"&callback=?"}}a.getJSON(g,function(k){var j=null;if(i.mode=="search"){j=k.results}else{j=k}a(j).reverse().each(function(){var p="";var n="";if(i.mode=="search"){p=this.from_user;n=this.profile_image_url}else{p=this.user.screen_name;n=this.user.profile_image_url}var l=this.user;var o=this.text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/,function(r){return r.link(r)});o=o.replace(/@[A-Za-z0-9_]+/,function(r){return r.link("http://twitter.com/"+r.replace(/^@/,""))});if(Date.parse(this.created_at)>i.lastTimeStamp){var q='<div class="tweet tweet-'+this.id+'">';if(i.settings.showAuthor){q+='<img width="24" height="24" src="'+n+'" /><p class="text"><span class="username"><a href="http://twitter.com/'+p+'">'+p+"</a>:</span> "}else{q+='<p class="text"> '}q+=o+' <span class="time">'+i.relativeTime(this.created_at)+"</span></p></div>";a(i.container).prepend(q);var m=this.created_at;a(i.container).find("span.time:first").each(function(){this.timeStamp=m});if(!f){a(i.container).find(".tweet-"+this.id).hide().fadeIn()}i.lastTimeStamp=Date.parse(this.created_at)}});a(i.container).find("div.tweet:gt("+(i.limit-1)+")").remove()})}},start:function(){var f=this;if(!this.interval){this.interval=setInterval(function(){f.refresh()},f.settings.rate);this.refresh(true)}},stop:function(){if(this.interval){clearInterval(this.interval);this.interval=false}}};var e=this.twitter;this.timeInterval=setInterval(function(){e.refreshTime()},5000);this.twitter.start()}});return this}})(jQuery);