Skip to content

Instantly share code, notes, and snippets.

@kgn
Created October 19, 2010 07:35
Show Gist options
  • Select an option

  • Save kgn/633781 to your computer and use it in GitHub Desktop.

Select an option

Save kgn/633781 to your computer and use it in GitHub Desktop.
This is the twitter plugin developed for inscopeapps.com
(function($){
$.fn.twitter = function(options){
options = $.extend({
//twitter url variables
'screen_name': null, //required
'count': null,
//plugin variables
'displayCount': null, //number of posts to display, regardless of how many were pulled
'blockSource': null, //A search string to block the diaplay of posts based on what submitted them
'loadingImg': null //An image to be shown while the content is loading, usually a loading gif
}, options);
//if no screen name early exit
if(options.screen_name === null){
return this;
}
var proto = ('https:' == document.location.protocol ? 'https:': 'http:');
var url = proto+'//api.twitter.com/1/statuses/user_timeline.json?&callback=?&trim_user=true&screen_name='+options.screen_name;
if(options.count){
url += '&count='+options.count;
}
var $root = $(this);
var $loading = null;
if(options.loadingImg){
$loading = $('<img class="loading" src="'+options.loadingImg+'" />').appendTo($root);
}
//load the data from twitter
function displayError(){
$root.fadeOut(function(){
$loading.remove();
$root.append('<em class="error">Twitter feed is currently unavailable.</em>');
$root.fadeIn();
});
}
$.getJSON(url, function(data){
if(data && data.error === undefined){
var count = 0;
var tweets = [];
$(data).each(function(){
if(options.blockSource && this.source.indexOf(options.blockSource) >= 0){
return;//continue
}
if(options.displayCount && ++count > options.displayCount){
return false;//break
}
//find links
var text = this.text.replace(/((https?|ftp):\/\/(\S)+)/gi, '<a href="$1">$1</a>');
//find @screen_names
text = text.replace(/(@)(\w+)/gi, '<a href="http://twitter.com/$2">@$2</a>');
//find #hashtags
text = text.replace(/(#\S+)/gi, '<em>$1</em>');
tweets.push(text);
});
$root.fadeOut(function(){
$loading.remove();
$(tweets).each(function(){
$root.append('<li>'+this+'</li>');
});
$root.fadeIn();
});
}else{
displayError();
}
});
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment