First Greasemonkey Script

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!

FRENCH CONNECTION FCUK Men Slouch Grease Monkey Hulk Jeans NwT 32 W 34 L
FRENCH CONNECTION FCUK Men Slouch Grease Monkey Hulk Jeans NwT 32 W 34 L
$34.95
FRENCH CONNECTION FCUK Men Slouch Grease Monkey Hulk Jeans NwT 34 W 34 L
FRENCH CONNECTION FCUK Men Slouch Grease Monkey Hulk Jeans NwT 34 W 34 L
$34.95
Bo Bunny Scrapbook Alphabet SUPERSIZED ALPHA STICKERS
Bo Bunny Scrapbook Alphabet SUPERSIZED ALPHA STICKERS
$1.47
1 Bo Bunny Boy Mechanic Scrapbook GREASE MONKEY PAPERS
1 Bo Bunny Boy Mechanic Scrapbook GREASE MONKEY PAPERS
$1.00
Grease Monkey Boots Shoes Black
Grease Monkey Boots Shoes Black
$19.99
Warhammer 40k Imperial Guard Colonel Schaeffers Last Chancer Grease Monkey HC4
Warhammer 40k Imperial Guard Colonel Schaeffers Last Chancer Grease Monkey HC4
$1.91
COLONEL SCHAEFFERS LAST CHANCER GREASE MONKEY WARHAMMER 40K Citadel 1990s
COLONEL SCHAEFFERS LAST CHANCER GREASE MONKEY WARHAMMER 40K Citadel 1990s
$3.14
Grease Monkey NEW by Tim Eldred
Grease Monkey NEW by Tim Eldred
$19.37
FLEX JUMBO 4pc MM RATCHET Wrench Automotive Tool Set Grease Monkey Gift Set
FLEX JUMBO 4pc MM RATCHET Wrench Automotive Tool Set Grease Monkey Gift Set
$39.95
FLEX 7pc MM RATCHET Wrench Automotive Auto Tool Set Kit Grease Monkey Gift Set
FLEX 7pc MM RATCHET Wrench Automotive Auto Tool Set Kit Grease Monkey Gift Set
$39.95
FLEX 7pc SAE Ratchet Wrench Automotive Home Tool Set Grease Monkey Gift Set
FLEX 7pc SAE Ratchet Wrench Automotive Home Tool Set Grease Monkey Gift Set
$39.95
Alpinestars Grease Monkey Jacket DARK GREY MED
Alpinestars Grease Monkey Jacket DARK GREY MED
$129.99
GREASEMONKEY HACKS MARK PILGRIM PAPERBACK NEW
GREASEMONKEY HACKS MARK PILGRIM PAPERBACK NEW
$22.93
Graham Masterton Grease Monkey Signed Lettered Edition
Graham Masterton Grease Monkey Signed Lettered Edition
$782.10
Collectible Hot Rod Item Hot Girl Grease Monkey Garage Tin Sign
Collectible Hot Rod Item Hot Girl Grease Monkey Garage Tin Sign
$22.95
GREASE MONKEY 1 2 Image Comics 1998 series NM
GREASE MONKEY 1 2 Image Comics 1998 series NM
$5.99
Grease monkey comic  1 image comics
Grease monkey comic 1 image comics
$0.99
Piston Cross bones grease monkey motor engine mechanic wall decal sticker tattoo
Piston Cross bones grease monkey motor engine mechanic wall decal sticker tattoo
$48.99
Wendy James ex Transvision Vamp CD single Grease Monkey
Wendy James ex Transvision Vamp CD single Grease Monkey
$7.49
HARLEY DAVIDSON  TRUNK LTD DESIGNER GREASE MONKEY WORK SHIRT NWT X LARGE XL
HARLEY DAVIDSON TRUNK LTD DESIGNER GREASE MONKEY WORK SHIRT NWT X LARGE XL
$39.99
Metal Tin SIgn GREASE MONKEY Institute Vintige Sign
Metal Tin SIgn GREASE MONKEY Institute Vintige Sign
$11.99
BROTHER JACK MCDUFF GREASE MONKEY WINKS BLUES PRESTIGE 45 299
BROTHER JACK MCDUFF GREASE MONKEY WINKS BLUES PRESTIGE 45 299
$3.00
Grease Monkey GMA4601 Geo Prizm 90 92 Toyota 88 05
Grease Monkey GMA4601 Geo Prizm 90 92 Toyota 88 05
$7.11
Black XXLarge Grease Monkey T Shirt NEW
Black XXLarge Grease Monkey T Shirt NEW
$9.95
Grease Monkey Logo Long Sleeve T Shirt L
Grease Monkey Logo Long Sleeve T Shirt L
$9.89
Grease Monkey 7 vinyl single UK PIAKS1
Grease Monkey 7 vinyl single UK PIAKS1
$12.39
FOX Racing 43045 Baseline Jean Grease Monkey W38
FOX Racing 43045 Baseline Jean Grease Monkey W38
$48.99
NEW FOX RACING DUSTER JEANS GREASE MONKEY
NEW FOX RACING DUSTER JEANS GREASE MONKEY
$64.50
NEW FOX RACING SALVO JEANS GREASE MONKEY
NEW FOX RACING SALVO JEANS GREASE MONKEY
$69.00
Grease Monkey 1995 Kitchen Sink 2 VF
Grease Monkey 1995 Kitchen Sink 2 VF
$1.00
Bo Bunny GREASE MONKEY 12x12 DS Heavy Papers 8 Sheets
Bo Bunny GREASE MONKEY 12x12 DS Heavy Papers 8 Sheets
$2.85
Bo Bunny GREASE MONKEY 12x12 DS Heavy Papers 12 Pgs
Bo Bunny GREASE MONKEY 12x12 DS Heavy Papers 12 Pgs
$4.27
New FCUK Grease Monkey Jeans Sz 30 34 138 80 OFF MSRP Free US Shipping
New FCUK Grease Monkey Jeans Sz 30 34 138 80 OFF MSRP Free US Shipping
$27.60
Greasemonkey Hacks Tips  Tools for Remixing the Web w
Greasemonkey Hacks Tips Tools for Remixing the Web w
$9.95
NEW FOX BOYS DUSTER JEAN GREASE MONKEY 30
NEW FOX BOYS DUSTER JEAN GREASE MONKEY 30
$46.50
NEW FOX BOYS DUSTER JEAN GREASE MONKEY 28
NEW FOX BOYS DUSTER JEAN GREASE MONKEY 28
$46.50
NEW FOX BOYS DUSTER JEAN GREASE MONKEY 29
NEW FOX BOYS DUSTER JEAN GREASE MONKEY 29
$46.50
NEW FOX BOYS DUSTER JEAN GREASE MONKEY 27
NEW FOX BOYS DUSTER JEAN GREASE MONKEY 27
$46.50
44 LkNEW KNUCKLEHEADS Navy Grease Monkey Auto Mechanic Shirt 12 mos SO ADORABLE
44 LkNEW KNUCKLEHEADS Navy Grease Monkey Auto Mechanic Shirt 12 mos SO ADORABLE
$20.00
Grease Monkey Tim Elred hardcover Graphic comic Novel
Grease Monkey Tim Elred hardcover Graphic comic Novel
$29.99

{ 2 comments… read them below or add one }

Damian Byrne July 25, 2007 at 3:56 pm

Yawn – Geek

bigfootcookie July 27, 2007 at 12:37 pm

You are wasted.

Well, you should be.

Leave a Comment

Previous post:

Next post: