O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  



Google phonebook hack
An HTML page to save on your machine that facilitates Google phonebook searches.

Contributed by:
David Crossman
[03/14/04 | Discuss (4) | Link to this hack]

The hack consists of an HTML page with a form containing select lists for search Type (residential, business or all) and for State and text inputs for Name and City. When you select a state, the form restores the type, name and city values (if any) you last used for that state (but only for the current page load).

To use this hack, save the following in an html file, then load the file in your browser whenever you want to search Google for phone numbers. (The page has preselected type [residential], city [Newton] and state [Massachusetts]. Change these as desired before you save the file.) When you load it the first time, bookmark it!

<html>
<head><title>Google Phonebook</title></head>
<body 
onload="with(document.forms[0]){types[s.selectedIndex]=t.selectedIndex;cities[s.selectedIndex]=c.value;names[s.selectedIndex]=n.value}">
<script language="JavaScript">
var types = new Array();
var names = new Array();
var cities = new Array();

function instate (obj, arr) {
	arr[obj.form.s.selectedIndex] = obj.value;
}

function restate (obj) {
	var i = obj.selectedIndex;
	var f = obj.form;
	f.c.value = !!cities[i] ? cities[i] : '';
	if (!!names[i])
		f.n.value = names[i];
	f.t.selectedIndex = typeof types[i] == 'number' ? types[i] : 0;
}

function wrapIt(str) {
	str = str.replace(/^\s*/, '').replace(/\s*$/, '');
	return /\s/.test(str) ? '"' + str + '" ' : str.length ? str + ' ' : '';
}

function googleIt(){
	var f = document.forms;
	var n = wrapIt(f[0].n.value);
	if (n == '') {
		alert('You must enter a name');
		f[0].n.focus();
	} else {
		f[1].q.value = f[0].t.options[f[0].t.selectedIndex].value + 'phonebook:' 
		+ wrapIt(f[0].n.value)
		+ wrapIt(f[0].c.value)
		+ f[0].s.options[f[0].s.selectedIndex].value;
		f[1].submit();
	}
}
</script>
<form>
<table>
<tr><td>Type:</td><td><select name="t" onchange="types[this.form.s.selectedIndex]=this.selectedIndex">
<option value="r" selected>residential
<option value="b">business
<option value="">any</td></tr>
</select></td></tr>

<tr><td>Name:</td><td><input type="text" name="n" onchange="instate(this, names)"></td></tr>

<tr><td>City:</td><td><input type="text" name="c" value="Newton" onchange="instate(this, cities)"></td></tr>

<tr><td>State:</td><td><select name="s" onchange="restate(this)">
<option value="AL">Alabama
<option value="AK">Alaska
<option value="AZ">Arizona
<option value="AR">Arkansas
<option value="CA">California
<option value="CO">Colorado
<option value="CT">Connecticut
<option value="DE">Delaware
<option value="DC">District of Columbia
<option value="FL">Florida
<option value="GA">Georgia
<option value="GU">Guam
<option value="HI">Hawaii
<option value="ID">Idaho
<option value="IL">Illinois
<option value="IN">Indiana
<option value="IA">Iowa
<option value="KS">Kansas
<option value="KY">Kentucky
<option value="LA">Louisiana
<option value="ME">Maine
<option value="MD">Maryland
<option value="MA" selected>Massachusetts
<option value="MI">Michigan
<option value="MN">Minnesota
<option value="MS">Mississippi
<option value="MO">Missouri
<option value="MT">Montana
<option value="NE">Nebraska
<option value="NV">Nevada
<option value="NH">New Hampshire
<option value="NJ">New Jersey
<option value="NM">New Mexico
<option value="NY">New York
<option value="NC">North Carolina
<option value="ND">North Dakota
<option value="OH">Ohio
<option value="OK">Oklahoma
<option value="OR">Oregon
<option value="PA">Pennsylvania
<option value="PR">Puerto Rico
<option value="RI">Rhode Island
<option value="SC">South Carolina
<option value="SD">South Dakota
<option value="TN">Tennessee
<option value="TX">Texas
<option value="UT">Utah
<option value="VT">Vermont
<option value="VA">Virginia
<option value="WA">Washington
<option value="WV">West Virginia
<option value="WI">Wisconsin
<option value="WY">Wyoming
</select></td></tr>
<tr><td colspan="2">Change state first, then other inputs.</td></tr>
<tr><td> </td><td><input type="button" value="Submit" onclick="googleIt()"></td></tr>
</table>
</form>
<form action="http://www.google.com/search" TARGET="googlePhone" method="get">
<input type="hidden" name="hl" value="en">
<input type="hidden" name="lr" value="">
<input type="hidden" name="ie" value="UTF-8">
<input type="hidden" name="oe" value="UTF-8">
<input type="hidden" name="newwindow" value="1">
<input type="hidden" name="safe" value="off">
<input type="hidden" name="q" value="">
</form>
</body>
</html>


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.