Friday, 17 April 2009

Cross-site XMLHttpRequest in Javascript

You know how sometimes you put together a simple bit of code and things seem fine but in the back of your mind you think there might be an issue. So you try another scenario/browser and sure enough it doesn't work and you remember why? Well that's what happened a few days ago.

I decided to put together a fairly straightforward site that would let users search for jobs across a range of Craigslist sites. My first impulse was to do this in ASP.Net, doing the scraping on the server side. But Craigslist takes a dim view of people scraping their content and repackaging it.

The next solution I explored was a simple bit of Javascript that would run client-side. The user would fill in a simple form, press go and the Javascript would use XMLHttpRequest to grab search results from Craigslist. It sounds simple enough and worked in IE7.

I had a nagging feeling that I had been reading something recently about cross-site XMLHttpRequests. Sure enough, in the latest incarnation of Firefox, the code didn't work at all, and in VS2008 in the debugger it threw security exceptions.

Cross-site XMLHttpRequest is now severely restricted/forbidden.

I put together a C# WinForms app to do it all for me. It works like a charm. I can now search for telecommuting opportunities across a range of Craigslist sites. The tool is a convenience app that federates user queries. It's a classic case of developers writing the tools that they want for themselves.

I will make the tool available for others shortly. I'll use it for a few days before releasing it. It's always a good idea to let things sit for a day or two.

I spent two hours today bringing the code into conformance with StyleCop. I have mixed feelings about StyleCop. It can be persnickety (Boolean properties with getters and setters have to have XML documentation that starts with the exact words "Gets or sets a value indicating whether...") but a lot of the advice is good. I particularly like the warnings about the correct implementation of IEnumerable<> and IDisposable. By now I have internalized the normative rules concerning casing of variables, methods etc and the ordering of members of a class according to visibility. The added benefit that I derive from using StyleCop is similar to a code-review -- it forces me to reexamine some pieces of code that in hindsight could do with reworking.