Skip to content

Instantly share code, notes, and snippets.

@ncammarata
Created August 15, 2010 00:46
Show Gist options
  • Save ncammarata/524894 to your computer and use it in GitHub Desktop.
Save ncammarata/524894 to your computer and use it in GitHub Desktop.
;(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