Hack 19. Convert UPS and FedEx Tracking Numbers to Links

 < Day Day Up > 

Make it easier to track packages.

All major package-delivery companies have web sites that allow you to track the status of packages. This is especially useful for online shoppers. Unless you're buying downloadable software, pretty much everything you buy online needs to be shipped one way or another. Unfortunately, not all online retailers are as web-savvy as one might hope.

This hack scans web pages for package tracking numbers and then converts them to links that point to the page on the delivery company's web site that shows the shipment's current status.

2.8.1. The Code

This user script runs on all pages. It is similar to "Turn Naked URLs into Hyperlinks" [Hack #13]. It scans the page for variations of package numbers that are not already contained in an <a> element and then constructs a link that points to the appropriate online tracking site.

These patterns are converted into links to UPS (http://www.ups.com):

  • 1Z 999 999 99 9999 999 9

  • 9999 9999 999

  • T999 9999 999

This pattern is converted into a link to FedEx (http://www.fedex.com):

  • 9999 9999 9999

The following patterns are converted into links to the United States Postal Service (http://www.usps.com):

  • 9999 9999 9999 9999 9999 99

  • 9999 9999 9999 9999 9999

Save the following user script as tracking-linkify.user.js:

 // ==UserScript== // @name UPS/FedEx Tracking Linkify // @namespace http://scripts.slightlyinsane.com // @description Link package tracking numbers to appropriate site // @include * // ==/UserScript== // Based on code by Justin Novack and Logan Ingalls // and included here with their gracious permission // Originally licensed under a Create Commons license // Visit http://creativecommons.org/licenses/by-sa/2.0/ for details var UPSRegex = new RegExp('/\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{'+ '2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\\dT]\\d\\d\\d ?\\d\\d\\d\\d '+ '?\\d\\d\\d)\\b', 'ig'); var FEXRegex = new RegExp('\\b(\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\'+ 'd)\\b', 'ig'); var USARegex = new RegExp('\\b(\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\'+ 'd ?\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d|\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d'+ '\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d)\\b', 'ig'); function UPSUrl(t) { return 'http://wwwapps.ups.com/WebTracking/processInputRequest?sor'+ 't_by=status&tracknums_displayed=1&TypeOfInquiryNumber=T&loc=e'+ 'n_US&InquiryNumber1=' + String(t).replace(/ /g, '') + '&track.x=0&track.y=0'; } function FEXUrl(t) { return 'http://www.fedex.com/cgi-bin/tracking?action=track&languag'+ 'e=english&cntry_code=us&initial=x&tracknumbers=' + String(t).replace(/ /g, ''); } function USAUrl(t) { return 'http://trkcnfrm1.smi.usps.com/netdata-cgi/db2www/cbd_243.d'+ '2w/output?CAMEFROM=OK&strOrigTrackNum=' + String(t).replace(/ /g, ''); } // tags we will scan looking for un-hyperlinked urls var allowedParents = [ 'abbr', 'acronym', 'address', 'applet', 'b', 'bdo', 'big', 'blockquote', 'body', 'caption', 'center', 'cite', 'code', 'dd', 'del', 'div', 'dfn', 'dt', 'em', 'fieldset', 'font', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'iframe', 'ins', 'kdb', 'li', 'object', 'pre', 'p', 'q', 'samp', 'small', 'span', 'strike', 's', 'strong', 'sub', 'sup', 'td', 'th', 'tt', 'u', 'var']; var xpath = '//text( )[(parent::' + allowedParents.join(' or parent::') + ')]'; var candidates = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); //var tO = new Date( ).getTime( ); for (var cand = null, i = 0; (cand = candidates.snapshotItem(i)); i++) { // UPS Track if (UPSRegex.test(cand.nodeValue)) { var span = document.createElement('span'); var source = cand.nodeValue; cand.parentNode.replaceChild(span, cand); UPSRegex.lastIndex = 0; for (var match = null, lastLastIndex = 0;  (match = UPSRegex.exec(source)); ) { span.appendChild(document.createTextNode( source.substring(lastLastIndex, match.index))); var a = document.createElement('a'); a.setAttribute('href', UPSUrl(match[0])); a.setAttribute('title', 'Linkified to UPS'); a.appendChild(document.createTextNode(match[0])); span.appendChild(a); lastLastIndex = UPSRegex.lastIndex; } span.appendChild(document.createTextNode( source.substring(lastLastIndex))); span.normalize( ); } // USPS Track if (USARegex.test(cand.nodeValue)) { var span = document.createElement('span'); var source = cand.nodeValue; cand.parentNode.replaceChild(span, cand); USARegex.lastIndex = 0; for (var match = null, lastLastIndex = 0;  (match = USARegex.exec(source)); ) { span.appendChild(document.createTextNode( source.substring(lastLastIndex, match.index))); var a = document.createElement('a'); a.setAttribute('href', USAUrl(match[0])); a.setAttribute('title', 'Linkified to USPS'); a.appendChild(document.createTextNode(match[0])); span.appendChild(a); lastLastIndex = USARegex.lastIndex; } span.appendChild(document.createTextNode( source.substring(lastLastIndex))); span.normalize( ); } // FedEx Track if (FEXRegex.test(cand.nodeValue)) { var span = document.createElement('span'); var source = cand.nodeValue; cand.parentNode.replaceChild(span, cand); FEXRegex.lastIndex = 0; for (var match = null, lastLastIndex = 0;  (match = FEXRegex.exec(source)); ) { span.appendChild(document.createTextNode( source.substring(lastLastIndex, match.index))); var a = document.createElement('a'); a.setAttribute('href', FEXUrl(match[0])); a.setAttribute('title', 'Linkified to FedEx'); a.appendChild(document.createTextNode(match[0])); span.appendChild(a); lastLastIndex = FEXRegex.lastIndex; } span.appendChild(document.createTextNode( source.substring(lastLastIndex))); span.normalize( ); }   } 

2.8.2. Running the Hack

Before installing the script, create a file called testlinkify.html with the following contents:

 <html> <head> <title>Test Linkify</title> </head> <body> <p>UPS tracking numbers:</p> <ul> <li>Package 1Z 999 999 99 9999 999 9 sent</li> <li>Package 9999 9999 999 sent </li> <li>Package T999 9999 999 sent</li> </ul> <p>FedEx tracking numbers:</p> <ul> <li>Package 9999 9999 9999 sent</li> </ul> <p>USPS tracking numbers:</p> <ul> <li>Package 9999 9999 9999 9999 9999 99 sent</li> </ul> </body> </html> 

Save the file and open it in Firefox (File Open…). It lists a number of variations of (fake) package tracking numbers in plain text, as shown in Figure 2-12.

Figure 2-12. Package tracking numbers


Now, install the user script (Tools Install This User Script), and refresh the test page. The script has converted the package tracking numbers to links to their respective online tracking sites, as shown in Figure 2-13.

Figure 2-13. Package tracking links


If you hover over a link, you will see a tool tip that lets you know that the tracking number was automatically converted to a link.

     < Day Day Up > 


    Greasemonkey Hacks
    Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox
    ISBN: 0596101651
    EAN: 2147483647
    Year: 2005
    Pages: 168
    Authors: Mark Pilgrim

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net