venkman69 / RedFin WalkScore Popup

// RedFin WalkScore Popup
// version 0.1 
// 2014-05-27
// Author: Narayan Natarajan
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// Notes:
// - Only shows walkscore in the property page
// - An unobstrusive mouse-hover popup is used
// - A link is also setup if in case you wish to see the walkscore page
// - It would be nice to show it in the map/browse page as well but that needs a bit more work
//
// ==UserScript==
// @name          RedFin WalkScore Popup
// @namespace     nnat.redfin.walkscore
// @description   Walkscore Popup on mouse hover in the property page
// @include       http://www.redfin.com/*home*
// @include       http://www.redfin.com/*real-estate*
// ==/UserScript==

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { alert('nohead');return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}


function makeFrame(src) {
   ifrm = document.createElement("IFRAME");
   ifrm.setAttribute("src", src);
   ifrm.style.width = 500+"px";
   ifrm.style.height = 500+"px";
   return ifrm;
   
} 

function getElByClassTxt(elClass){
		if (document.getElementsByClassName(elClass)[0]==undefined){
//			alert('notthere:'+elClass);
			return false;
		}
    return document.getElementsByClassName(elClass)[0].innerHTML;
}

function getAddressSearchPage(){
	var address="";
	var add1 = getElByClassTxt('street_anchor listing_address_info');
	if (add1 == false){
		return false;
	}
	address=add1;
	var add2 = getElByClassTxt('listing_address_info');
	address=address + " " + add2;
	alert(add2);
	return address;

}
function getAddressHomePage(){
	var address="";
	var add1 = getElByClassTxt('street-address');
	if (add1 == false){
		return false;
	}
	address=add1;
	var add2 = getElByClassTxt('locality');
	if (add2 != false )
		address=address + " " + add2;
	var add3 = getElByClassTxt('region');
	if (add3 != false )
		address=address + " " + add3;
	var add4 = getElByClassTxt('postal-code');
	if (add4 != false )
		address=address + " " + add4;
	return address;

}
function getAddress(){
	return getAddressHomePage();
	/*
	TODO: include walkscore in map with right side panel - will need a little different popup etc.
	if (document.location.toString().toLowerCase().search("home")>=0){
		return getAddressHomePage();
	}else{
		return getAddressSearchPage();
	}
	*/
}

function showWalkScoreFrame() {
	var address=getAddress();
	/*
	The page sometimes loads slow so keep checking for address
 */
	if (address == false){
		setTimeout(showWalkScoreFrame,1000);
		return;
	}
	var loc = "http://www.walkscore.com/get-score.php?street=" + address + "&go=Go";
    var iff=makeFrame(loc);
	var div = document.createElement('div');
    var diviff = document.createElement('div');
		var anc = document.createElement('a');
				anc.target = '_new';
				anc.href = loc;
				anc.innerHTML = "Show Walkability";
		diviff.id="wsiff";
		diviff.className="wsbox";
    diviff.appendChild(iff);
		div.appendChild(anc);
    div.appendChild(diviff);
	var sibling = "";
		sibling= document.getElementsByClassName('street-address')[0];
	sibling.parentNode.insertBefore(div, sibling.nextSibling);
}

addGlobalStyle('.wsbox{display:none !important;width:100%;}a:hover+.wsbox,.wsbox:hover{display:block !important;position:relative;z-index:100;}');

showWalkScoreFrame();