Created
January 18, 2016 01:18
-
-
Save yakovsh/bff527b3b2540cf166a2 to your computer and use it in GitHub Desktop.
Tracking UPS Packages via JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A post at TechDigits [https://web.archive.org/web/20050311025802/http://techdigits.blogspot.com/2004/12/ups-package-tracking-with-rss.html] | |
* about tracking UPS packages via RSS and web services got me thinking if the same is possible via Javascript and | |
* the XmlHttpRequest object (in IE and Mozilla). Since Google's Gmail and Google Suggest started using that object, | |
* it has become more popular. So after some thinking, I put together the following quick and dirty code snippet | |
* | |
* I don't know how useful this can be since the security information is exposed, but it is a nice hack. | |
*/ | |
req = new XMLHttpRequest(); | |
var strXML = '<?xml version='1.0'?>' + | |
'<AccessRequest xml:lang='en-US'>' + | |
'<AccessLicenseNumber>YOURLICENSEKEY</AccessLicenseNumber>' + | |
'<UserId>YOURUSERNAME</UserId>' + | |
'<Password>YOURPASSWORD</Password>' + | |
'</AccessRequest>' + | |
'' + | |
'<?xml version='1.0'?>' + | |
'<TrackRequest xml:lang='en-US'>' + | |
'<Request>' + | |
'<TransactionReference>' + | |
'<CustomerContext>sample</CustomerContext>' + | |
'<XpciVersion>1.0001</XpciVersion>' + | |
'</TransactionReference>' + | |
'<RequestAction>Track</RequestAction>' + | |
'<RequestOption>activity</RequestOption>' + | |
'</Request>' + | |
'<TrackingNumber>1Z12345E1512345676</TrackingNumber>' + | |
'</TrackRequest>'; | |
req.open('POST', 'https://www.ups.com/ups.app/xml/Track', false); | |
req.send(strXML); | |
document.write(req.responseText); | |
/* | |
* This code on Mozilla browsers will retreive and post the raw XML information (you do need the | |
* security information for UPS's website to make it work which is a bit of a security flaw). | |
* Once you got the information, then you can parse it via Mozilla's or IE's XSLT processors, | |
* or via regular DOM methods after parsing it into a DOM tree as follows | |
*/ | |
var parser = new DOMParser(); | |
var doc = parser.parseFromString(req.responseText, "text/xml"); | |
/* | |
* For Mozilla browsers, you also need to request a security permission from the user in order to access UPS's website. | |
* A web proxy might be an answer to that problem but in any case here is the code | |
*/ | |
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment