Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created June 5, 2010 10:14
Show Gist options
  • Save thinkphp/426509 to your computer and use it in GitHub Desktop.
Save thinkphp/426509 to your computer and use it in GitHub Desktop.
//show me love to the singleton
//author: Adrian
//@thinkphp
/*
Description:
create a request JSON to Twitter Service API using script tag injection and handles callback for you.
*/
var twitterbadge = function() {
/* config */
var config = {
username: 'thinkphp'
};
/* private method */
function init(options) {
if(options.username) {config.username = options.username;}
badge = document.getElementById('tweets');
if(!badge) {return;}
badge.innerHTML = 'Loading...';
if(badge) {
var root = "http://query.yahooapis.com/v1/public/yql?q=";
var yql = 'select * from html where url="http://twitter.com/'+config.username+'" and xpath="//span[@class=\'entry-content\']"';
var url = root + encodeURIComponent(yql) + '&format=xml&callback=twitterbadge.seed';
var rest = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ftwitter.com%2F"+config.username+"%22%20and%20xpath%3D%22%2F%2Fh2%5B%40class%3D'thumb%20clearfix'%5D%22&format=json&callback=twitterbadge.avatar";
loadScript(rest,function(){loadScript(url,function(){if(window.console){console.log('YQL:' + yql);console.log('Loaded SCRIPT and injected into HEAD');}});});
}
};
/* private method */
function loadScript(url,callback) {
var script = document.createElement('script');
script.setAttribute('type','text/javascript');
if(script.readyState) {
script.onreadystatechange = function() {
if(script.readyState == 'loaded' || script.readyState == 'complete' ) {
script.onreadystatechange = null;
callback();
}
}
} else {
script.onload = function(){
callback();
}
}
script.setAttribute('src',url);
document.getElementsByTagName('head')[0].appendChild(script);
};
/* private method */
/*
@description
- function callback from service twitter that receives data as arguments
@param o (Object) data from services and handles callback
@return none.
*/
function seed(o) {
var data = o.results;
if(data && data.length>0){
var output = '<ul>';
for(var i=0;i<data.length;i++) {
output += clean(data[i]);
}
output += '</ul>';
document.getElementById('tweets').innerHTML = output;
}else{document.getElementById('tweets').innerHTML = 'No results found.';}
};
/* private method */
/*
@param input - text (String) the text is cleaned through replace SPAN with LI tag
@return text (String) text replaced
*/
function clean(text) {
text = text.replace('<span','<li');
text = text.replace('</span>','</li>');
return text;
}//end function
/* private method
@param o (*Object*)- the object received from service twitter through REST GET.
@return none.
*/
function avatar(o) {
if(window.console){console.log(o);}
var template = '<h2 class="thumb clearfix">'+
'<a href="http://twitter.com{account}">'+
'<img alt="" border="0" height="73" id="profile-image" src="{src}" valign="middle" width="73"/>'+
'</a>{username}</h2>';
var data = o.query.results.h2;
var h2 = data[0];
var username = h2.content;
var href = h2.a.href;
var src = h2.a.img.src
document.getElementById('owner').innerHTML = template.replace('{src}',src).replace('{username}',username).replace('{account}',href);
}
/* return an object that points at the private methods which I want to revealed*/
return {init: init, seed: seed,avatar: avatar};
}();//do EXEC
//do initialization of the singleton
twitterbadge.init({username: 'codepo8'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment