toandersongmail.com / Magento - Whitepages Identity Check Integration

// ==UserScript==
// @name         Magento - Whitepages Identity Check Integration
// @namespace    http://pro.whitepages.com
// @version      0.1
// @description  Embeds a button within Magento order details pages to verify contact information integration with Whitepages Pro Identity Check 
// @author       Trevor Anderson <tanderson@whitepages.com>
// @match        http://*/index.php/admin/sales_order/view/order_id/*
// @grant        none
// ==/UserScript==

//First thing we need to do is wait until required elements are loaded.
//We'll wait in periods of 500ms, but will only wait up to 10 seconds to prevent any infinite loop from occurring.

function getContactText(numTries){
    numTries++;
    //all the stuff we're looking for is within div tags having the class 'entry-edit'
    var divs = document.getElementsByClassName("entry-edit");
    var contactText = "";
    for(var i = 0; i < divs.length; i++){
        contactText += divs[i].innerHTML;   
    }
    //if sourceText doesn't contain a billing address, it doesn't have what we need yet.
    if(contactText.indexOf(">Billing Address<") >= 0){
        return contactText;
    }else if(numTries >= 20){
        return null;
    }else{
        return setInterval(getContactText(numTries),500);  
    }
}

var contactText = getContactText(0);

if(contactText !== null){
	   
	/*** COLLECT THE CONTACT INFORMATION FROM THE PAGE ***/

	var custEmail, billingName, billingStreet, billingStreet2, billingCity, billingState, billingZip, billingPhone, 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(contactText);
	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(contactText);
	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(contactText);
	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>";
	//get button list
	var buttonList = document.getElementsByClassName("form-buttons")[0];
    
	buttonList.appendChild(span);
}