First Greasemonkey Script

by taoski on July 25, 2007

The Helpdesk system we use here is far from great. In fact, its slow, crashy and down right bad. Unless you add new columns to the screen layout and monitor the contents every refresh, you cannot even see that a call has been updated 20 times when the customer calls for an update! Especially when the people taking those irate calls from the aforementioned customer don’t tell you they are chasing either..

Greasemonkey is a Firefox extension that allows you to change web pages by executing Javascript code after they have loaded. A simple explanation would be that you can force Google, for example to have larger fonts for the links and no pictures. You can pretty much change webpages to suit your whims.

All I needed to do was to get the script to parse a table of information, identify the time that a record on the screen was updated and if it had been updated in the last 30 minutes, make the update time turn red or green if it had been updated within the last hour.

After learning XPath statements and getting through the security models behind XPCNativeWrapper, I came up with this:

 

  1. function $x(p, context) {

  2. if (!context) context = document;

  3. var i, arr = [], xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

  4. for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);

  5. return arr;

  6. }

  7.  

  8. function do_rows(r) {

  9. var headings = document.evaluate(“id(’rltr_”+r+“‘)/td[3]“, document, null, XPathResult.ANY_TYPE,null);

  10. var thisHeading = headings.iterateNext();

  11. var row_hours = thisHeading.textContent.substr(9,2);

  12. var row_mins = thisHeading.textContent.substr(12,2);

  13. var row_day = thisHeading.textContent.substr(0,2);

  14. var current_date = new Date()

  15. var current_day = current_date.getDate();

  16. var current_mins = current_date.getMinutes();

  17. var current_hour = current_date.getHours();

  18. var time_diff = (current_hour*60 + current_mins) - (parseInt(row_hours)*60 + parseInt(row_mins))

  19.  

  20. if (time_diff <= 30 && (current_day == row_day))

  21. {

  22. var paragraphs = $x(“id(’rltr_”+r+“‘)/td[3]“);

  23. paragraphs.forEach(function(paragraph)

  24. { // Loop over every paragraph

  25. paragraph.innerHTML = + thisHeading.textContent + ” “ + time_diff + “;

  26. });

  27. }

  28.  

  29. if ((current_hour == row_hours) && ((current_mins - row_mins)<=60) && ((current_mins - row_mins)>=31) && (current_day == row_day))

  30. {

  31. var paragraphs = $x(“id(’rltr_”+r+“‘)/td[3]“);

  32. paragraphs.forEach(function(paragraph)

  33. { // Loop over every paragraph

  34. paragraph.innerHTML = + thisHeading.textContent + ” “ + time_diff + “;

  35. });

  36. }

  37.  

  38. }

  39.  

  40. // do the main loop for each row

  41. var i=0

  42. for (i=0;i<=30;i++)

  43. {

  44. do_rows(i)

  45. }

For the technical gimps out there, it reads the XPath data from the table into a variable called headings and then grabs the data into another variable called thisHeading. A few calculations later, it rewrites the data with bold tags either side if it meets certain criteria.

Not the prettiest code in the world, but it works!

{ 2 comments… read them below or add one }

1 Damian Byrne 07.25.07 at 3:56 pm

Yawn - Geek

2 bigfootcookie 07.27.07 at 12:37 pm

You are wasted.

Well, you should be.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>