Skip to content

Instantly share code, notes, and snippets.

@amwelles
Created June 13, 2013 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amwelles/5776750 to your computer and use it in GitHub Desktop.
Save amwelles/5776750 to your computer and use it in GitHub Desktop.
Simple twitter widget with new OAuth & [codebird-js](https://github.com/mynetx/codebird-js). Uses a bit of jQuery, but you could just as easily use vanilla JS.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twitter widget</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script src="sha1.js" type="text/javascript"></script>
<script src="codebird.js" type="text/javascript"></script>
<script>
// function for parsing links
function parseLinks(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(exp,'<a href="$1" target="_blank">$1</a>');
}
// function for parsing @username
function parseUsers(text) {
var exp = /@(\w+)/ig;
return text.replace(exp,'<a href="https://twitter.com/$1" target="_blank">@$1</a>');
}
// function for parsing #hashtags
function parseHashtags(text) {
var exp = /#(\w+)/ig;
return text.replace(exp,'<a href="https://twitter.com/search?q=%23$1" target="_blank">#$1</a>');
}
// create a new codebird
var cb = new Codebird;
// YOU MUST SET THESE KEYS!
// create a new app here: https://dev.twitter.com/apps/new
cb.setConsumerKey('YOUR_KEY', 'YOUR_SECRET');
cb.setToken('YOUR_TOKEN', 'YOUR_SECRET');
// returns the user's timline
cb.__call(
'statuses_userTimeline',
{},
function (reply) {
var i = 0;
for (var key in reply) {
i++;
// limit to 5
if (i <= 5 && reply[key].text) {
// add these to the #tweets ul
$('#tweets').append('<li>'+ parseHashtags(parseUsers(parseLinks(reply[key].text))) +'</li>');
};
}
}
);
</script>
</head>
<body>
<ul id="tweets"></ul>
</body>
</html>
@CodeNinja89
Copy link

Hi, I referred to your code in one of my projects here. Is it right if I try to access something like
cb.__call(
"statuses_mentionsTimeline", {"count": "1"},
function (reply) {
if(reply){
for(var key in reply){
localStorage.tweetId = reply[key].id;
alert(localStorage.tweetId);
//autoReply(reply[key].id, reply[key].user["screen_name"]);
}
}
}
); // see reply[key].id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment