Access to Lyricwiki.org’s REST API with pure JavaScript

July 1st, 2008 by  |  Published in JavaScript, Lyricwiki  |  4 Comments

My objective of today was to get rid of these ugly warnings issued by IE while accessing lyricwiki.org’s REST API using a small Flash thingy. This SWF file is needed to circumvent JavaScript’s cross domain limitation. This security feature avoids that JavaScript from host A loads files from host B. This absolutely makes sense, just imagine how a client connected to the internet and a company’s intranet could be used to steal confidential data. However, this limitation does not apply to JavaScript files loaded with the script tag. Therefore, I suggested to add a JavaScript format to the REST API in order to dynamically access the API using a technique called “On-Demand JavaScript“.

First, I wanted to make the API extension output JavaScript code. However, I found out – know this i probably the right moment to admit that I am not a JavaScript guru ;) – that JSON files can be included by the script tag as well, which makes them suitable for On-Demand JavaScript as well. However, both formats are implemented (JSON used by default) and transparently interchangeable.

The result of my work are 4 files (download), hosted on my company’s website:

(Code is tested with IE6, IE7, FF3 – download now! ;) – and FF2, Public Domain Dedication)

EDIT: updated files to optionally use JSONP. (see comments) I did not use the proposed json_encode() method, as it’s only available with PHP versions >= 5.2.0.

EDIT2: Yay! My proposal finally made its way into production. I therefore changed the files to use the production API instead of my little test script. Additionally, we already moved from the pure txt to the json format on Songtexte.com. This is really cool, as we got rid of this little flash thingy that caused warnings in IE.

EDIT3: Forgot to mention that JSONP wasn’t implemented. However, I’m supposed to get access to Lyricwiki’s API source repository real soon. I will definetely try to implement the optional callback parameter.

Responses

  1. Ash Searle says:

    July 8th, 2008 at 11:40 am (#)

    Did you consider following the jsonp model instead of this “on demand” idea? With jsonp the REST call would be something like “…&fmt=js&callback=someFunc”The PHP output would be:someFunc({..javascript object literal…});And the javascript object literal could be output really simply with PHP’s built-in json_encode function.The difference is that the on-demand stuff creates a new function which you need to add more code to check for later: if (typeof window.lyricWiki == ‘function’) ….With jsonp (json with callback), you define your handler than add new <script> tags for the REST calls whenever you feel like. If the calls are successful, the callback’s automatically called.In fact, it’s something mediawiki already implements.

  2. Stefan Fußenegger says:

    July 8th, 2008 at 1:21 pm (#)

    Hi Ash, thanks for your comments!I did consider using jsonp when I stumbled upon the exact same page from mediawiki. However, that was after I wrote that stuff ;) It does not change the on demand idea, as it still is what is called “on demand javascript”, as this idea si about dynamically adding script tags when they are needed (i.e. on demand). Using JSONP does however simplify the client code as the browser itself takes care of calling the callback method. I have to admit, that my PHP skills are a little bit rusted, since I last wrote PHP code when 4.3 was the latest and greatest. If I remember correctly, JSON was still spelled Jason at that time ;) Using json_encode() is definitely the way to go, thanks for the hint!So, I’ll go and add an optional callback agrument (e.g. “&callback=writeLyrics”) and rewrite my code to use json_encode rather than my homegrown code.Best regardsStefan

  3. Jim says:

    February 24th, 2009 at 6:02 pm (#)

    Hello, could you please give a quick example on getting song lyrics with JSONP. I can’t seem to get it to use the callback argument. Here’s what I’m trying:

    http://lyricwiki.org/api.php?artist=leonard_cohen&song=everybody_knows&fmt=json&callback=test

    If you could let me know how to get this working, that’d be awesome.

    Thanks!

  4. Stefan Fußenegger says:

    February 25th, 2009 at 11:20 am (#)

    Hi Jim,

    I just added “EDIT3″ to the post. I didn’t mention that JSONP isn’t implemented at the moment.

Leave a Response