Christian Heilmann

Posts Tagged ‘bug’

Small change in the flickr API output breaking my bad code

Wednesday, December 31st, 2008

During my absence I got an email that the first version of my unobtrusive Flickr badge has stopped working. The reason was that Flickr changed the output of the API. I the old version the JSON object contained an HTML description that was run through htmlspecialchars() first. I always considered this a bit of a nuisance and wondered about the reason. Now the Flickr feed does not have the encoding any longer as you can see for example in the JSON output of my latest photos:


{

“title”: “Terminal 5 animated discs thingamabob *”,
“link”: “http://www.flickr.com/photos/codepo8/3141566868/”,
“media”: {m“},
“date_taken”: “2008-12-27T08:10:35-08:00”,
“description”: “

codepo8 posted a video:

Terminal 5 animated discs thingamabob *

* technical term, really

“,
“published”: “2008-12-27T16:10:35Z”,
“author”: “nobody@flickr.com (codepo8)”,
“author_id”: “11414938@N00”,
“tags”: “london art heathrow awesome animation t5 ba britishairways terminal5”
},

Before the change this would have been:


{

“title”: “Terminal 5 animated discs thingamabob *”,
“link”: “http://www.flickr.com/photos/codepo8/3141566868/”,
“media”: {m“},
“date_taken”: “2008-12-27T08:10:35-08:00”,
“description”: “

codepo8 posted a video:

"Terminal

* technical term, really

“,
“published”: “2008-12-27T16:10:35Z”,
“author”: “nobody@flickr.com (codepo8)”,
“author_id”: “11414938@N00”,
“tags”: “london art heathrow awesome animation t5 ba britishairways terminal5”
},

This broke my code as I was relying on regular expressions to get the photos. Taught me a lesson, and as we have a media property now there is no need for this any longer. The fix for the badge code was:


// buggy, bad
temp=obj.items[i].description.match(/src="([^&]*)"/)[1];”
// works :)
temp=obj.items[i].media.m;

Lesson learned: Don’t trust regular expressions and data scraping.

Thanks to Jennifer Forman Orth for flagging this up!

Opera, REST APIs, Module Patterns and generated script nodes

Friday, April 11th, 2008

I just came across an annoying thing in Opera. I love using the Module Pattern for designing my JavaScripts and I love generating script nodes on the fly for REST APIs that give me back JSON. The following example using the twitter API works swimmingly on Firefox and Safari (have to check IE later, but I’d be surprised if it didn’t):



However, if you test this in Opera you get an error:


JavaScript – file://localhost/Applications/MAMP/htdocs/operacallbackfail.html
Inline script thread
Error:
name: ReferenceError
message: Statement on line 1: Reference to undefined variable: twitterbadge
Backtrace:
Line 1 of linked script http://twitter.com/statuses/user_timeline/codepo8.json?callback=twitterbadge.show&count=10
twitterbadge.show([{user : {screen_name : “codepo8”,...08”}]);

It seems like the newly generated script node calls the method of the module before the module has been created. In other words, newly generated script nodes halt the execution of the code that generated them. The following example works across browsers, including Opera:



Not too daunting a fix, but it feels wrong to have to have an extra method and another line calling it.

Try it out for yourself: Opera bug with dynamic script nodes and module pattern