Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created February 27, 2011 18:06
Show Gist options
  • Save basicxman/846374 to your computer and use it in GitHub Desktop.
Save basicxman/846374 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en-us" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Twitter Badge</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<a href="http://twitter.com/basicxman" id="basicxman">Get tweets from basicxman</a><div id="loading" style="float:left;"></div>
<script type="text/javascript">
twitterBadge = function() {
// Configuration
var badgeId = "basicxman";
var outputId = "tweets";
var loadingId = "loading";
var numTweets = 5;
var timeoutDelay = 2000;
var link = null;
return {
addEvent: function(elm, evType, fn, useCapture) {
// Event listener function, http://www.scottandrew.com/weblog/jsjunk#events
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
},
init: function() {
// Check browser capabilities
if (document.getElementById && document.createTextNode) {
link = document.getElementById(badgeId);
if (link && link.href)
twitterBadge.addEvent(link, 'click', callData, false);
}
},
retrieveData: function(dataset) {
window.clearTimeout(twitterBadge.timeout); // Stop the timer
displayData(dataset);
},
failure: function() {
window.clearTimeout(twitterBadge.timeout);
//window.location = link.getAttribute("href"); // Redirect to Twitter.
}
};
function createLoadingIcon() {
var loading = document.createElement("img");
loading.setAttribute("src", "loading.gif");
loading.setAttribute("alt", "Loading Tweets...");
return loading;
};
function callData(eventObj) {
if (!document.getElementById(outputId)) {
twitterBadge.timeout = window.setTimeout('twitterBadge.failure()', timeoutDelay);
var user = badgeId;
document.getElementById(loadingId).appendChild(createLoadingIcon());
var twitterAPI = document.createElement("script");
var APIurl = "http://api.twitter.com/1/statuses/user_timeline.json?id=" + user + "&count=" + numTweets + "&include_entities=false&include_rts=false&callback=twitterBadge.retrieveData";
twitterAPI.setAttribute("src", APIurl);
document.getElementsByTagName("head")[0].appendChild(twitterAPI);
cancelClick(eventObj);
}
};
function displayData(dataset) {
var output = document.createElement("ul");
output.id = outputId;
for (var i = 0; dataset[i]; ++i) {
var entry = document.createElement("li");
var entryText = document.createElement("span");
entryText.appendChild(document.createTextNode(dataset[i].text));
entry.appendChild(entryText);
output.appendChild(entry);
}
link.parentNode.insertBefore(output, link.nextSibling);
document.getElementById(loadingId).style.display = "none";
output.getElementsByTagName("span")[0].focus();
};
function cancelClick(e) {
if (window.event) {
window.event.cancelBubble = true;
window.event.returnValue = false;
return;
}
if (e) {
e.stopPropagation();
e.preventDefault();
}
};
}();
twitterBadge.addEvent(window, "load", twitterBadge.init, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment