Skip to content

Instantly share code, notes, and snippets.

@wookiehangover
Created December 11, 2013 15:40
Show Gist options
  • Save wookiehangover/7912621 to your computer and use it in GitHub Desktop.
Save wookiehangover/7912621 to your computer and use it in GitHub Desktop.
/*global _, moment, JST*/
jQuery.fn.tweeeeet = function(options){
"use strict";
var defaultOptions = {
screen_name: 'quickleft',
count: 10,
page: 1,
include_rts: true,
include_entities: true
};
var settings = $.extend({}, defaultOptions, options);
var querystring = $.param( settings );
return this.each(function(){
var apiUrl = 'http://api.twitter.com/1/statuses/user_timeline.json?'+ querystring;
var params = {
url: apiUrl,
dataType: 'jsonp',
type: 'get'
};
var $el = $(this);
var linkRegex = /(https?\S+)/g;
var mentionRegex = /(@\S+)/g;
// transform @replies, links and hashtags into links
var enhance = function( tweet ){
// link-ify links
var linkCounter = 0;
var urls = tweet.entities.urls || [];
var linkify = function(){
var link = urls[ linkCounter ];
if( link ){
linkCounter += 1;
return '<a href="'+ link.expanded_url +'">'+ link.display_url +'</a>';
} else {
return '';
}
}
// link-ify mentions
var mentionsCounter = 0;
var mentions = tweet.entities.user_mentions || [];
var mentionify = function(){
var mention = mentions[ mentionsCounter ];
if( mention ){
mentionsCounter += 1;
return '<a href="http://twitter.com/'+ mention.screen_name +'">@'+ mention.screen_name +'</a>';
} else {
return '';
}
};
var ret = tweet.text
.replace( linkRegex, linkify )
.replace( mentionRegex, mentionify );
return ret;
};
// create an HTML string to append to the DOM
var render = function( tweet ){
tweet.timestamp = moment( tweet.created_at );
if( settings.include_entities ){
tweet.text = enhance( tweet );
}
return JST.twitter( tweet );
};
$.ajax( params ).done(function( data ){
$.each(data, function( i, tweet ){
$el.append( render( tweet ) );
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment