Greasemonkey (improving ServiceDesk QMgr)

Sat at work I find myself frequently scanning through the list of tickets that are assigned to me so I should know what to be getting on with. A bunch of tickets on my queue are “stale”—I might be waiting for a machine to become free, I’m waiting on some big thing to happen, etc. Some are in a state where I’m still actively working on it, but I happen to be waiting on some feedback from the ticket owner (i.e. the engineer that opened it), while the rest all require work to be done.

It’s possible to change the status of a ticket, but this is a bit of a pain—it requires changing status to “Waiting on user input”, etc. The tickets can then be ordered by status… no biggie, but surely there must be a “better way”.

Enter Greasemonkey—an extension for Firefox that allows the end-user a massive degree of control over how websites are presented. I’d describe Greasemonkey as a way of “post-processing” websites once they’ve loaded. This is done by hijacking the JavaScript and providing the Greasemonkey script with full access to the DOM for the page.

In the course of my really basic script I learned a lot about how Firefox must render pages. This DOM stuff is totally awesome.

What I wanted to do was assign each and every ticket a “status”—I wanted three statuses: stale, awaiting feedback and active. Luckily for me three colours came to mind: red, orange and green :)

I started by intercepting a mouseclick with some JavaScript stuff… I then checked whether the “modifier” key was held (on my Linux box this is the Windows key, on Sun keyboards it’s usually the little diamond thing) and proceeded to change the colour of the HTML element that the mouse pressed. This required figuring out how JavaScript worked—it’s not my favourite language in the world, but I suppose it does the job.

Once I’d got the colour cycling working (this took a little while as I’ve never played with Greasemonkey, JavaScript or this DOM stuff before) I decided to start investigating how I could begin to uniquely identify the “ticket number”—turned out this was fairly easy: the ticket number link has an HREF containing: “Work on Ticket”, once I’d got this I could extract the ticket number itself. This ticket number is, by definition, unique (or at least I damn well hope it is!), which means I can use it as a key for storing variables. My variable is simple: status.

Greasemonkey provides two really nifty functions: GM_getValue(key, value) and GM_setValue(key, value). These work in a similar way to cookies. So now when the user cycles through the available statuses, I store the value using GM_setValue(ticketno, status). This value is persistent, so I can close the browser and come back later and the value will be the same.

At this point all that was left was to read in the stored values and update the tickets with the appropriate colour: to do this I added an EventListener for the “load” event, found all of the ticket links (XPath provides some easy ways to do this), read the stored value (defaulting to an active status (green)) and updated the field. Easy.

So, here’s the end result:

qmgr.png

I wasn’t too sure what details I should/shouldn’t make available, so I blurred a lot of it out. So, it’s not the most incredibly useful script in the world, but it should save me some time, and it was an interesting learning experience.

Since switching to Wordpress I’d been annoyed by the fact that I have to enter the Site admin interface just to create a new blog entry. I fixed this by using Greasemonkey to add an “Admin options” section to the top of my page:

wordpress.png

Really basic stuff, for sure, but certainly a time-saver for me. I knocked this one up really quickly: if I wanted to do it properly I’d check if I was actually logged in before displaying the admin options. Since I tend to stay logged in all of the time, I figured this was a fairly pointless exercise.

So, Greasemonkey is good. We Love It.

Tags:

Leave a Reply