Twitter badge

As you can see from the right hand side, I now have a “Currently” section. This is basically a badge from Twitter that automatically updates when I do. If you’ve tried doing the same and haven’t had any success using the code from the Twitter website, you’re not alone. The Javascript code on the website uses .relative_created_at; instead of .created_at; like it should. Without fixing it, the date comes out as “undefined”.
Even after amending that however, the date is in a less than desirable format. I used Jon Aquino’s script and amended it to suit my needs. If you mess around with it you can do all sorts of things like link to individual statuses. All I wanted was to get rid of the list elements and just show one update at a time. You’re free to nick my amended code if you like. Remember to change the user_timeline (in bold) to your own.
<script type=”text/javascript”>
var elapsedTime = function(createdAt) {
var ageInSeconds = (new Date().getTime() – new Date(createdAt).getTime()) / 1000;
var s = function(n) { return n == 1 ? ” : ‘s’ };
if (ageInSeconds < 0) { return 'just now'; } if (ageInSeconds < 60) { var n = ageInSeconds; return n + ' second' + s(n) + ' ago'; } if (ageInSeconds < 60 * 60) { var n = Math.floor(ageInSeconds/60); return n + ' minute' + s(n) + ' ago'; } if (ageInSeconds < 60 * 60 * 24) { var n = Math.floor(ageInSeconds/60/60); return n + ' hour' + s(n) + ' ago'; } if (ageInSeconds < 60 * 60 * 24 * 7) { var n = Math.floor(ageInSeconds/60/60/24); return n + ' day' + s(n) + ' ago'; } if (ageInSeconds < 60 * 60 * 24 * 31) { var n = Math.floor(ageInSeconds/60/60/24/7); return n + ' week' + s(n) + ' ago'; } if (ageInSeconds < 60 * 60 * 24 * 365) { var n = Math.floor(ageInSeconds/60/60/24/31); return n + ' month' + s(n) + ' ago'; } var n = Math.floor(ageInSeconds/60/60/24/365); return n + ' year' + s(n) + ' ago'; } // Make date parseable in IE [Jon Aquino 2007-03-29] function fixDate(d) { var a = d.split(' '); var year = a.pop(); return a.slice(0, 3).concat([year]).concat(a.slice(3)).join(' '); } function twitterCallback(obj) { var html = ''; for (var i = 0; i < 1; i++) { html += '' + obj[i].text + ' (updated ' + elapsedTime(fixDate(obj[i].created_at)) + ')'; window.aaa = obj[i].created_at; } document.getElementById('twitter_list').innerHTML = html; } </script>
<span id=”twitter_list”></span>
<script type=”text/javascript” src=”http://www.twitter.com/t/status/user_timeline/2976491?callback=twitterCallback&count=1″></script>

6 comments

  1. weeeell i gave twitter another shot and all of a sudden the SMS bit seems to work. not impressed with the twitter badge so i think i’ll nick this code, ta very much!

  2. ooh, i know that, but did you make the blog’s 3000th comment or did i? anyway that’s beside the point, innit? 3000 comments! bloody hell, well done!

Comments are closed.