Thursday, April 15, 2010

Greasemonkey as a temporary solution for a rewrite project

We are starting a new project for a client. This client has a suite of websites around their main internet product. Our job is to rewrite one of the existing websites.

BTW, it's not our first rewrite to Rails :) Our biggest rewrite so far was Restaurant Kritik was rewritten from PHP to Rails. This time we work on a website which was created using Flash. In both cases we support migrating data from existing databases. 

The thing is that until we are fully ready with the rewrite we can't release our software for production use. It means that one of the main use cases (scenario which starts on the main product and leads the user with some data to our tool) is not easily possible to be manually testable.

The way the main product brings users to our tool is simple - by using a link with some parameters. We need to parse the parameters and let the user use the imported data.

As a temporary solution we decided to create a simple Greasemonkey script.

For those of you who don't know what is Greasemonkey - it's a tool which lets your browser change the html code on-the-fly. You write a JavaScript snippet of code which is run as if it was part of the webpage JavaScript.

In our case we need to replace all the links pointing to the old tool. The links should now point to the new url. Here is the full code we needed:

// ==UserScript==
// // @name          url modifier
// // @include       http://**
// // ==/UserScript==

function replace_import_link(e) {
  var href = this.getAttribute("href");
  new_href = href.replace("", "");
  this.setAttribute("href", new_href);

var allLinks = document.getElementsByClassName('old_tool_link');
for (var i = 0; i < allLinks.length; i++) {
  link = allLinks[i];
  link.addEventListener('click', replace_import_link, true)

As you see, we create a click event handler in which we replace the existing href attribute.

Possibly, there were other solutions, but this one was quite simple and seems to work fine for us. Having this script, we can test the full importing scenario even though we are not ready for production release yet.

Do you have any other idea how to solve this kind of problems with a rewrite project?