toandersongmail.com / Magento - Whitepages Identity Check - Order Listing

// ==UserScript==
// @name         Magento - Whitepages Identity Check - Order Listing
// @namespace    http://pro.whitepages.com
// @version      0.1
// @description  Embeds a button within Magento order listing pages to verify shipping information with Whitepages Pro Identity Check 
// @author       Trevor Anderson <tanderson@whitepages.com>
// @match        http://*/index.php/admin/sales_order/index/key/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
// @grant        none
// ==/UserScript==

//first, identify the order details link. Step 1: find the table 
function getOrderTable(numTries){
    numTries++;
    var orderTable = document.getElementById("sales_order_grid_table");
    if(orderTable !== null){
        if(orderTable.rows !== null)
            return orderTable;
    }
    if(numTries >= 20)
        return null;
    return setInterval(getOrderTable(numTries),500);  
}

function generateVerificationLink(orderURL, cellToPlaceLink){
    $.get(orderURL, function( order_content ) {
        var custEmail, shippingName, shippingStreet, shippingStreet2, shippingCity, shippingState, shippingZip, shippingPhone = '';
        
        //emails should follow the 'Email' label, with some other tags in between.
    	var emailPattern = /<label>Email<\/label>(?:\s*<[^>]+>)+(\w+[^<]*\@[\w\.]+\.[a-z]{2,4})</;
	    var emailResults = emailPattern.exec(order_content);
	    if(emailResults.length > 1)
	        custEmail = emailResults[1];
	        
	    	/*** PULL BILLING DATA ***/
		//after the 'Billing Address' h4 tag are some tags, then an 'edit' link, some more tags, and then is the billing info blob, contained in an '<address></address>' block
		var billingInfoPattern = />Billing Address<\/h4>(?:\s*<[^>]+>)+\s*Edit(?:\s*<[^>]+>)+\s*<address>((?:[^<]+<[^>]+>)*[^<]+)<\/address>/;
		//billing info is arranged on a number of lines, that are always in the same order. If a particular piece of data is missing, the line is blank.
		var billingInfo = [];
		var billingInfoResults = billingInfoPattern.exec(order_content);
		if(billingInfoResults.length > 1)
			billingInfo = billingInfoResults[1].replace(/<[^<]+>/g,"").split(/\n/);

		billingName = billingInfo[0];
		billingStreet = billingInfo[2];
		billingStreet2 = billingInfo[3];
		var billingCityStateZip = billingInfo[6].split(", ");
		if(billingCityStateZip.length > 2){
			billingCity = billingCityStateZip[0];
			billingState = billingCityStateZip[1];
			billingZip = billingCityStateZip[2];
		}
		//phone is always on the 8th line and prefixed with "T: "
		billingPhone = billingInfo[8].substring(3);

		/*** PULL SHIPPING DATA ***/
		//after the 'Shipping Address' h4 tag are some tags, then an 'edit' link, some more tags, and then is the shipping info blob, contained in an '<address></address>' block
		var shippingInfoPattern = />Shipping Address<\/h4>(?:\s*<[^>]+>)+\s*Edit(?:\s*<[^>]+>)+\s*<address>((?:[^<]+<[^>]+>)*[^<]+)<\/address>/;
		//shipping info is arranged on a number of lines, that are always in the same order. If a particular piece of data is missing, the line is blank.
		var shippingInfo = [];
		var shippingInfoResults = shippingInfoPattern.exec(order_content);
		if(shippingInfoResults.length > 1)
			shippingInfo = shippingInfoResults[1].replace(/<[^<]+>/g,"").split(/\n/);

		shippingName = shippingInfo[0];
		shippingStreet = shippingInfo[2];
		shippingStreet2 = shippingInfo[3];
		var shippingCityStateZip = shippingInfo[6].split(", ");
		if(shippingCityStateZip.length > 2){
			shippingCity = shippingCityStateZip[0];
			shippingState = shippingCityStateZip[1];
			shippingZip = shippingCityStateZip[2];
		}
		//phone is always on the 8th line and prefixed with "T: "
		shippingPhone = shippingInfo[8].substring(3);

		/*** BUILD ID CHECK WEB URL ***/

		var WPPURL = "https://pro.lookup.whitepages.com/identity_checks?";
		if(custEmail !== '')
			WPPURL += "email_address="+encodeURIComponent(custEmail)+"&";
		if(billingName !== '')
			WPPURL += "billing_name="+encodeURIComponent(billingName)+"&";
		if(billingStreet !== '')
			WPPURL += "billing_address_street_line_1="+encodeURIComponent(billingStreet)+"&";
		if(billingStreet2 !== '')
			WPPURL += "billing_address_street_line_2="+encodeURIComponent(billingStreet2)+"&";
		if(billingCity !== '')
			WPPURL += "billing_address_city="+encodeURIComponent(billingCity)+"&";
		if(billingState !== '')
			WPPURL += "billing_address_state="+encodeURIComponent(billingState)+"&";
		if(billingZip !== '')
			WPPURL += "billing_address_postal_code="+encodeURIComponent(billingZip)+"&";
		if(billingPhone !== '')
			WPPURL += "billing_phone="+encodeURIComponent(billingPhone)+"&";
		if(shippingName !== '')
			WPPURL += "shipping_name="+encodeURIComponent(shippingName)+"&";
		if(shippingStreet !== '')
			WPPURL += "shipping_address_street_line_1="+encodeURIComponent(shippingStreet)+"&";
		if(shippingStreet2 !== '')
			WPPURL += "shipping_address_street_line_2="+encodeURIComponent(shippingStreet2)+"&";
		if(shippingCity !== '')
			WPPURL += "shipping_address_city="+encodeURIComponent(shippingCity)+"&";
		if(shippingState !== '')
			WPPURL += "shipping_address_state="+encodeURIComponent(shippingState)+"&";
		if(shippingZip !== '')
			WPPURL += "shipping_address_postal_code="+encodeURIComponent(shippingZip)+"&";
		if(shippingPhone !== '')
			WPPURL += "shipping_phone="+encodeURIComponent(shippingPhone)+"&";

    	//cut the trailing '&'
    	WPPURL = WPPURL.substring(0,WPPURL.length-1);
       
	    //build button 
        var span = document.createElement("span");
        span.innerHTML = "<button id=\"wpp_idcheck_button\" title=\"Verify with Whitepages Pro\" class=\"scalable\" type=\"button\" onclick=\"window.open('"+WPPURL+"','_blank')\">Verify with Whitepages Pro</button>";
	    cellToPlaceLink.appendChild(span);
    });
}

var orderTable = getOrderTable(0);

if(orderTable !== null){
    var orderTableRows = orderTable.rows;
    
    for(var i = 0; i < orderTableRows.length; i++){
        var cols = orderTableRows[i].cells;
        var viewCol = cols[8];
        var text = viewCol.innerHTML;
        var urlPattern = /"http:\/\/[^\/]+(\/[^\"]+)"/;
        var urlResults = urlPattern.exec(text);
        if(urlResults){
            var orderURL = urlResults[1];
            generateVerificationLink(orderURL,viewCol);
        }
    }
}