Skip to content

Instantly share code, notes, and snippets.

@ncammarata
Created August 11, 2010 12:57
Show Gist options
  • Select an option

  • Save ncammarata/518932 to your computer and use it in GitHub Desktop.

Select an option

Save ncammarata/518932 to your computer and use it in GitHub Desktop.
/* Just some basic styling we've provided */
#twitter li {margin-bottom: 10px; font-size: 12px; clear: both}
#twitter li img {float:left; margin:0px 10px 10px 0px;border:1px solid #c2c2c2; -moz-box-shadow: 0px 0px 4px #c2c2c2; -webkit-box-shadow: 0px 0px 4px #c2c2c2; box-shadow: 0px 0px 4px #c2c2c2;}
$(function(){
//note: these parameters work together.
//ie. if you include both from and search, the plugin will get all
//tweets from that user containing the search term
$('#twitter').twitter({ // use on a <ul> as the plugin adds <li>
limit: 5, // number of posts shown, defaults to five
to: 'nicklovescode', // optional, selects tweets mentioning @nicklovescode
from: 'rwaldron', // optional, gets tweets from user @rwaldron
search: 'bocoup', // optional, gets tweets containing 'bocoup'
linkify: true // turn links in tweets into actual links, defaults to yes
}); // returns itself like all plugins should
});
;(function ($) {
var mention = function(str) {
return str.replace(/[@]+[A-Za-z0-9-_]+/ig, function(username) {
return username.link('http://twitter.com/'+username.replace('@',''));
});
},
hashtags = function(str) {
return str.replace(/[#]+[A-Za-z0-9-_]+/ig, function(tag) {
tag = tag.replace('#','%23');
return tag.link('http://search.twitter.com/search?q='+tag);
});
},
twit_format = function(str) {
return mention(hashtags(str));
},
loadTweets = function (params) {
this.each(function () {
var $this = $(this); // Cache `this`
$.getJSON('http://search.twitter.com/search.json?callback=?', {
rpp: params.limit || 5,
q: (params.from?'from:'+params.from + ' ':'')
+ (params.to?'to:'+params.to + ' ':'')
+ (params.search || '')
}, function(tweets) {
var tweet_count = 0;
$.each(tweets.results, function(i, tweet) {
tweet_count++;
$this.append($('<li>', {
class: 'tweet'
})
.append($('<a>', { // avatar
class: 'twit_avatar',
href: 'http://twitter.com/' + tweet.from_user,
html: $('<img>', { src: tweet.profile_image_url })
}))
.append($('<a>', { // @nick
href: 'http://twitter.com/' + tweet.from_user,
class: 'twit_user'
}).text('@' + tweet.from_user))
.append(' : ')
.append($('<p>', { // msg
class: 'twit_msg'
}).html( twit_format(params.linkify ? linkify(tweet.text) : tweet.text) )));
});
if (!tweet_count) { $this.append($('<li>').text('No Tweets Found')); }
if (params.callback) { params.callback(); }
});
});
},
linkifyUrl = 'http://github.com/cowboy/javascript-linkify/raw/master/ba-linkify.min.js';
$.fn.twitter = function (params) {
params.linkify ? $.getScript(linkifyUrl, loadTweets.call(this, params)) : loadTweets.call(this, params); // load linkify
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment