Amazon Package Tracking Greasemonkey Script

Amazon, eBay, and other online retailers are displaying package tracking information on their website for your orders, which is convenient, but how up-to-date the information is? Based on my research, there is always delay that up to hours or even days. Why? Because they are relying on the carrier’s infrastructure to provide the tracking data:

  • Push. This requires carrier to publish event to Amazon, and then Amazon can aggregate the data for display. Usually carriers do not have the technology that publishes the data at real-time, they are not tech companies after all.
  • Pull. This requires carrier to have a scalable infrastructure that can take all Amazon’s requests. It is not just about request volume, but also the speed – Amazon does not want customers to wait for seconds until the information shows up.

Either way, the delay is inevitable. As a customer, I always go to the carrier’s website to acquire the latest tracking information, and this is what I do:

  1. Highlight the tracking number.
  2. Right click and select “Search Google for …”.
  3. Select the right carrier suggested by Google.
  4. Land on the carrier’s website.

To make it easier for me, I should add a hyperlink for the tracking number so that it redirects me to the carrier’s website with one-click. Why not call carrier’s API to retrieve the data directly? Well, it requires registration with all the carriers and I am being lazy…

Okay, in order to achieve that, I need to write a Greasemonkey script.

Step 1: Install Tempormonkey Chrome add-on.

Step 2: Write some code!

// ==UserScript==
// @name Amazon Package Tracking Script
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add link to tracking number that directs to the carrier's tracking website
// @author me
// @match https://www.amazon.com/gp/your-account/ship-track/*
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function() {
'use strict';
// Get the DOM element
var ele = $("div .ship-track-grid-subtext");
if (!ele) return;
var text = ele[0].innerHTML.trim();

// Get carrier's name
var carrierName = getCarrierName(text);
console.log("carrier name: " + carrierName);
// Get tracking number
var trackingNumber = getTrackingNumber(text);
console.log("tracking number: " + trackingNumber);

// Get tracking website's URL
var url = getTrackingWebsiteURL(carrierName, trackingNumber);
if (url) {
  ele.wrap("<a target='_blank' href='" + url + "'></a>");
}

function getCarrierName(text) {
  var index1 = text.indexOf(":") + 1;
  var index2 = text.indexOf(",");
  var carrier = text.substring(index1, index2).trim();
  switch(carrier) {
    case "UPS Mail Innovations":
      return "USPS";
    default:
      return carrier;
  }
}

function getTrackingNumber(text) {
  var index1 = text.lastIndexOf(":") + 1;
  var tracking = text.substr(index1).trim();
  return tracking;
}

function getTrackingWebsiteURL(carrierName, trackingNumber) {
  switch(carrierName) {
    case "USPS":
      return "https://tools.usps.com/go/TrackConfirmAction?tLabels=" + trackingNumber;
    case "ONTRAC":
      return "https://www.ontrac.com/trackingres.asp?tracking_number=" + trackingNumber;
    case "UPS":
      return "https://wwwapps.ups.com/WebTracking/processRequest?tracknum=" + trackingNumber;
    default:
      return "";
    }
  }
}
)();

That’s it!

Download the script from GitHub here.

Advertisements