Load tweet from user and make links, hashtags, usernames active links
Here's the JS function to load specified number of tweets and parse first one:
/**
* Get latest tweets
* @param {string} username
* @param {int} num Number of tweets to get
*/
function getTweets(username, num) {
$.getJSON("http://api.twitter.com/1/statuses/user_timeline/"+username+".json?count="+num+"&include_rts=1&callback=?", function(tweets) {
$('.js-get-tweet').html(parseTweet(tweets[0].text));
});
} // getTweets
/**
* Change user names, hashtags and links to active links
* @param {string} text Just a tweet
* @return {string} Parsed tweet
*/
function parseTweet(text) {
var patterns = {
link: /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
user: /(^|\s)@(\w+)/g,
hash: /(^|\s)#(\w+)/g
};
return text
.replace(patterns.link,'<a href="$1" target="_blank">$1</a>')
.replace(patterns.user, '$1@<a href="http://www.twitter.com/$2" target="_blank">$2</a>')
.replace(patterns.hash, '$1#<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
} // parseTweet
How to use it?
Create an html tag with js-get-tweet class and run this code:
getTweets('Idered', 1);
More useful code snippets here
Written by Kasper Mikiewicz
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Twitter
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#