Archive for the 'JSONP' Category

416th Wordpress Twitter Plugin

Today my Recently on Twitter wordpress plugin became the 416th plugin to join the wordpress Twitter plugin directory. I’ve been told that the 415th previous attempts didn’t load asynchronously or otherwise caused the page to stall during render. Hopefully, this one does a slightly better job, though I haven’t actually investigated the competition. This plugin took about 30 minutes to develop, mostly because I have created around 10 different iterations of the same concept for previous clients and employers.

Let me know if you find it useful. You can find details on usage and installation here. Some of the features include:

  • Loads Twitter data asynchronously after the DOM ready event
  • Automatically links Urls
  • Automatically links Twitter Usernames
  • Automatically links hash tags
  • Always fresh data, even with WP-cache & Super Cache
  • Works with both Widget enabled and regular wordpress themes

You can see it in action at the bottom of the ride side of this blog. Enjoy!

My Life Anywhere, the jQuery FriendFeed Plugin

Take your life stream anywhere on the web with my recently completed jQuery FriendFeed plugin. It’s a simple little JavaScript plugin that displays everything you pipe into FriendFeed plus comments made via FriendFeed. It’s super simple, and super useful.

The implementation is very easy. Just include jQuery 1.3.2 and the FriendFeed plugin on the page via a script tag. You can get both files on the jQuery site linked above. Then you just need to instantiate it. The entire thing might looking something like this.

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.friendfeed.js"></script>
 
<div id="activityStream"></div>
 
<script type="text/javascript">
var options = {}; // optional second parameter to override $.friendfeed.defaults values
jQuery('#activityStream').friendfeed( 'username', options );
</script>

Check out this demo I am using on my homepage. In the release on the jQuery site, I have also included some very simple CSS. The way the JavaScript is developed, you will be able to override everything. I make it simple by setting up a defaults object that can be manipulated via the second argument.

Additionally, you can replace or alter any method or value found in the $.friendfeed object by merely leveraging $.extend(); Look for more in depth documentation and usage in the near future. Or, if you want to share your samples, leave a comment below.

Last.fm JSONP Recent Tracks Widget

I wanted to add a recent tracks on Last.fm widget to my blog. The caveat is that I use wp-cache, meaning, a standard RSS feed widget would be often out of date. I opted for a JSONP widget, the implementation is fairly straight forward. After signing up for a Last.fm API account, I hooked into the recent tracks API, which is capable of providing a JSONP response.

I ended up using jQuery, since it comes with Wordpress. Though a library isn’t entirely necessary for such a simple widget, it’s already on the page, so why not use it. Also, it gave me a good reason to play around and test out that library. For the most part, jQuery is like all the other mainstream libraries in terms of syntax. The no-conflict mode is very nice for pages where name space collision might be an issue, but it did have a few odd behaviors that I’m not sure I understand. One that really confused me was jQuery’s getElementById() equivalent.

    var elem = jQuery('#my_id')

For some reason, this returns an array. To actually access the element above, you need to say elem[0]. By definition getElementById() returns only one element. There is probably a good reason for this (at least I hope so), but I’m not sure why yet.

Take a look at my implementation in you’re interested. Help yourself to the code, but please get your own api key. It’s not much more than a simple for-loop that creates a handful of elements. I could probably make it more plug-and-play, but it suits my needs for now.

Update: I have turned this code into a full fledged Wordpress Plugin / Widget. You can download the plugin here. Perfect for both widget-enabled themes and non-widget-enabled. Themes. Get it now.

Launch: Asylum User Photos

On Thursday, the new Asylum User Photos Experience phase 1 launched into production. I have been working for over 5 months on this product, needless to say – I am very excited!

The Asylum editorial staff kicked off the launch with this awesome comic book tattoo gallery, which despite limited promotion is already engaging users.

This application, which is code named UPP, is by far the most complex application I have launched for AOL. It has a ton of moving parts, from an intricate backend system using AOL proprietary technology stack called Dynapub to a front end API server that provides a JSONP API in order to create a rich, responsive JavaScript application.  The platform is extremely scalable. We are hoping to engage around 30,000  users per month uploading photographs and serve up 60 million page views per month.

As we iterate on the project, we are also considering a publicly available API in order to share the application with developers and partners around the world who want to contribute to the growing Asylum community.

I hope you get a chance to check out the tattoo gallery, if you have a comic book tattoo to show off – you totally should! :)

Digg Widget, A Lightweight, DOM Friendly JavaScript Alternative

The website I have been working with, Asylum.com, recently expressed interest in adding a Digg Most Popular widget to their site. Asylum.com asked me to apply some CSS to resolve the display issues they were having. After I spent a little while investigating the HTML markup in the standard Digg widget, I decided it wouldn’t be in Asylum’s best interest to use it in it’s current form.

While the widget is well done, it used a few techniques that are frowned upon at AOL. The most disconcerting issue was the use of document.write(). If you are familiar with JavaScript, then you are aware that document.write() causes issues on a few levels. First off, document.write() blocks while it is working. This blocking prevents other items on the page, such as CSS or images, from loading while the write occurs. Additionally, elements and text created via document.write() are not added to the DOM ( Document Object Model ) and cannot be manipulated in the same way. I envisioned code that was more friendly to both the page load and the DOM.

In addition to document.write(), the Digg Widget was calling in the entire jQuery framework. This seemed like overkill. I didn’t want to include an entire library just to create this widget.

The Digg API lives on the Digg server, I couldn’t use classic Ajax because of the same origin policy. However, Digg provides JSONP (JavaScript Object Notation with Padding). This allows you to wrap the JSON with a callback function that will be executed once the script loads. To use JSONP, you have to dynamically create a script tag and assign the Digg API url to the src attribute. Most modern browsers support dynamic creation of script tags, it would look as follows.

var s = document.createElement( "script" );
s.setAttribute('src', apiURLAndArguements );
s.setAttribute( 'type', 'text/javascript' );
var h = document.getElementsByTagName( 'head' );
h[0].appendChild( s );

The Asylum team asked that the Digg widget have both the most popular articles and the upcoming articles contained within the widget. Since I didn’t want to write the same code twice, I sought an Object Oriented, self-contained widget that could use the same JavaScript blueprint to use both calls.

The biggest obstacle to creating this was JavaScript scope. If I instantiated an object to contain the Digg data, I would lose scope the moment the callback function was triggered. The callback would be called in the scope of the window and not the scope of the object that first created the script tag.

The Digg API further complicated this because it does not allow periods, square brackets or curly brackets in the name of the callback function. I spent some time researching JavaScript inheritance and closures hoping they would provide the resolution I needed.

In the end, I used a combination of JavaScript closure and JavaScript function pointers to provide the solution. The key lay within providing the callback function a correlation to the original object. Since Digg doesn’t allow periods and square brackets in the name of the callback function, I couldn’t create a simple global array with a reference to the object. Instead, I had to generate a function name appended with a random string that would live in the global scope. The function interior would look as follows.

var method = this; // a reference to the calling object
function wrapperFunc( obj )
{
    method.returnDataHandler( obj ); // the function to be triggered within the original calling function
}
var rand = Math.ceil( Math.random() * 100000000000 );
window['DiggAsylumCallback' + rand] = wrapperFunc; // store this pointer in the global scope
var urlBase = "http://digg.com/tools/services?type=javascript&callback=DiggAsylumCallback"+rand+"&endPoint="+ finalEndPoint +"&domain="+ yourDomain +"&sort=digg_count-desc&count=" + numberDisplayed
return urlBase;

Once the callback function triggers, it stores the JSON inside the original calling function and the prototype methods can be called in the correct scope. Here is a look at how the object is triggered.

var popular = new diggAsylum( 5, "asylum.com", "/stories/popular", "diggDivId", true, "desc" );

The diggAsylum constructor signature looks as follows.

function diggAsylum( count, domain, endPoint, htmlId, showDesc, sortType ) { .... }

Get the complete JavaScript file here, see it in action here, get a zip file of the CSS, JS and HTML here.